Código
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
int M[3][3];
int fila[] = {0, 2, 2, 2, 1, 1, 1, 0, 0, 0};
int col[] = {0, 0, 1, 2, 0, 1, 2, 0, 1, 2};
int incf[] = {0, 1, 1, -1};
int incc[] = {1, 1, 0, 1};
int check() {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
for (int d = 0; d < 4; ++d) {
string s;
int f = i, c = j;
for (int h = 0; h < 3; ++h) {
if (f < 0 or f >= 3 or c < 0 or c >= 3) break;
s += char(M[f][c] + '0');
f += incf[d];
c += incc[d];
}
if (s == "111") return 1;
else if (s == "222") return 2;
}
}
}
return -1;
}
void draw() {
for (int i = 0; i < 50; ++i) cout << endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (M[i][j] == 0) cout << " ";
else if (M[i][j] == 1) cout << " X";
else cout << " O";
if (j != 2) cout << " |";
}
cout << endl;
if (i != 2) cout << "------------" << endl;
}
}
int rec(int &x, int &y, int torn) {
int best = -2;
int z, t;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (M[i][j] != 0) continue;
M[i][j] = torn;
if (check() == torn) {
x = i;
y = j;
M[i][j] = 0;
return 1;
}
int aux = rec(z, t, (torn == 1)?2:1);
if (aux == -2 or aux == 0) {
if (best < 0) {
best = 0;
x = i;
y = j;
}
}
else if (aux == -1) {
if (best < 1) {
best = 1;
x = i;
y = j;
M[i][j] = 0;
return best;
}
}
else if (aux == 1) {
if (best < -1) {
best = -1;
x = i;
y = j;
}
}
M[i][j] = 0;
}
}
return best;
}
void tira_pc() {
int x, y;
int aux = rec(x, y, 1);
M[x][y] = 1;
}
void tira_jug() {
int pos = -1;
while (pos < 1) {
cin >> pos;
if (pos < 1 or pos > 9) pos = -1;
else if (M[fila[pos]][col[pos]] != 0) pos = -1;
}
M[fila[pos]][col[pos]] = 2;
}
int main() {
memset(M, 0, sizeof(M));
int win = -1, torn = 0;
int qtt = 0;
draw();
while ((win = check()) < 0 and qtt < 9) {
if (torn == 1) tira_pc();
else tira_jug();
torn = 1 - torn;
draw();
++qtt;
}
if (win == 1) cout << "Gana el ordenador" << endl;
else if (win == 2) cout << "Ganas tu" << endl;
else cout << "Empate" << endl;
}
Se juega con los números:
7 8 9
4 5 6
1 2 3
El ordenador juega con X, el jugador humano con O. Empieza jugando el humano, aunque todo esto son detalles que se arreglan cambiando una o dos líneas.


 
  




 Autor
 Autor
		




 En línea
									En línea
								



 
						 
						