#include <iostream>
#include<string>
using namespace std;
struct ficha{
int posicion;
string xo;
};
struct Tablero{
ficha fichas [9];
bool tieneganador;
string ganador;
};
Tablero T1 = {
1," ", 2," ",3," ",4," ",5," ",6," ",
7," ",8," ",9," ",false, "No defindo aún"
};
bool fganador();
void InicioTablero(){
cout << "Tablero: -> Jugador 1 = X; Jugador 2 = O " << endl;
cout << "========================================" << endl;
cout << " 1 " << "\t"<<" 2 "<< "\t" << " 3 " << endl;
cout << " 4 " << "\t"<<" 5 "<< "\t" << " 6 " << endl;
cout << " 7 " << "\t"<<" 8 "<< "\t" << " 9 " << endl;
cout <<"\n\n";
}
void actualizaTablero(int pos, int jugador){
string etiqueta= " ";
if(jugador==1){etiqueta="X";} else {etiqueta="O";};
T1.fichas[pos-1].xo= etiqueta;
}
void mostrarTablero(){
for (int i = 0 ; i<9; i++ ){
if (T1.fichas[i].xo==" ") {cout << i+1;}
cout << T1.fichas[i].xo << "\t";
if((i==2)||(i==5)||(i==8)){cout << endl;}
}
cout << endl;
cout << "Ganador: " << T1.ganador << endl;
}
bool fganador(){
int count1=0, count2=0, count3=0,count4=0 , k=0;
for (int i = 0 ; i<3; i++ ){
for (int j =0; j< 3 ; j++){
if (T1.fichas[k].xo=="X") {count1++;}
if (T1.fichas[k].xo=="O") {count2++;}
k++;
if (count1 == 3 || count2==3){
return true;
}
}
}
k=-3;
for (int i = 0 ; i<3; i++ ){
for (int j =0; j< 3 ; j++){
if (T1.fichas[k+3].xo=="X") {count3++;}
if (T1.fichas[k+3].xo=="O") {count4++;}
k++;
if (count3 == 3 || count4==3){
return true;
}
}
}
if ((T1.fichas[0].xo=="X") && (T1.fichas[4].xo=="X")&&(T1.fichas[8].xo=="X"))
return true;
if ((T1.fichas[0].xo=="O") && (T1.fichas[4].xo=="O")&&(T1.fichas[8].xo=="O"))
return true;
return false;
}
int main(){
int pos=-1, jugador=1;
int numjugada=0;
InicioTablero();
do{
jugador = numjugada%2 +1;
cout << "jugador "<< jugador << " - escoja su posicion:";
cin>> pos;
actualizaTablero(pos, jugador);
mostrarTablero();
T1.tieneganador=fganador();
if (T1.tieneganador){
cout << "Jugador " << jugador << ": ganaste!"<< endl;;
cout << "ingresa tu nombre"<< endl;
cin.ignore(1);
getline(cin, T1.ganador);
mostrarTablero();
break;
}
numjugada++;
}while(pos>0);
return 0;
}