Good, I created a main for a program and the thing is that before adding a bit of code I was doing everything right, when I reached the end of the program I finished the program and everything OK. However, now when the program ends, it does not end, it remains as in an infinite loop from which it does not exit. I leave the main in question:
public static void main( String [] arg)
{
int empezar = (int) (Math.random() * 2);
boolean solucion = false;
boolean estado;// para saber a quien le toca jugar
Tablero tablero = new Tablero();
boolean continuar = true;
Scanner sca=new Scanner(System.in);
while (continuar==true)
{
if(empezar%2==0)
{
estado=false;
}
else
{
estado=true;
}
while(!solucion)
{
if(!estado)
{
jugador(tablero);
tablero.ver();
estado = true;
}
else
{
maquina(tablero);
tablero.ver();
estado = false;
}
if(tablero.esSolucion()=='X')
{
System.out.println("FELICIDADES JUGADOR 'X'. HAS GANADO A UN SISTEMA DE INTELIGENCIA ARTIFICIAL");
solucion=true;
}
else if(tablero.esSolucion()=='O')
{
System.out.println("HAS PERDIDO. LA MAQUINA 'O' TE HA GANADO. TAL VEZ NO SEAS TAN BUENO COMO UN SISTEMA DE INTELIGENCIA ARTIFICIAL");
solucion=true;
}
else if(tablero.esSolucion()==' ')
{
System.out.println("HAS EMPATADO CON UN SISTEMA DE INTELIGENCIA ARTIFICIAL. VAS POR EL BUEN CAMINO JOVEN PADAWAN");
solucion=true;
}
}
System.out.println("Desea jugar otra vez? (1=SI//0=NO)");
if(sca.nextInt()%2==0)
{
System.out.println("NOS VEMOS EN OTRA OCASION. HASTA LUEGO");
continuar=false;
}
else
{
System.out.println("Perfecto, empecemos de nuevo.");
tablero = new Tablero();
solucion = false;
}
}
}
Thanks in advance.
EDIT: I attached the operation of the function 'esSolution ()' since several have told me that it is possible that the error is there.
public char esSolucion()
{
char solucion = ' ';
for(int i = 0; i < 3; i++)
{
if(tablero[i][0]=='X'&&tablero[i][1]=='X'&&tablero[i][2]=='X'||tablero[0][i]=='X'&&tablero[1][i]=='X'&&tablero[2][i]=='X')
{
solucion='X';
}
else if(tablero[i][0]=='O'&&tablero[i][1]=='O'&&tablero[i][2]=='O'||tablero[0][i]=='O'&&tablero[1][i]=='O'&&tablero[2][i]=='O')
{
solucion='O';
}
}
if(solucion!='X'&&solucion!='O')
{
if (tablero[0][0] == 'X' && tablero[1][1] == 'X' && tablero[2][2] == 'X' || tablero[0][2] == 'X' && tablero[1][1] == 'X' && tablero[2][0] == 'X') {
solucion = 'X';
} else if (tablero[0][0] == 'O' && tablero[1][1] == 'O' && tablero[2][2] == 'O' || tablero[0][2] == 'O' && tablero[1][1] == 'O' && tablero[2][0] == 'O') {
solucion = 'O';
}
}
if(solucion!='X'&&solucion!='O')
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(tablero[i][j]==' ')
{
solucion='_';
}
}
}
}
return solucion;
}