I made this function for the old game:
void jogadaPC(char m[][3], char opc)
{
puts("\nVez do computador.");
sleep(1);
int linha = rand() % 2;
int coluna = rand() % 2;
bool vazio = localVazio(m, linha, coluna);
bool valido = false;
if(!vazio)
{
while(!valido)
{
linha = rand() % 2;
coluna = rand() % 2;
if(localVazio(m, linha, coluna))
{
m[linha][coluna] = opc;
valido = true;
}
}
return;
}
m[linha][coluna] = opc;
}
In that it chooses a position (row, column) randomly in the array, and if it is false it passes the while check, but it never ends.
So: I started all array values with '':
void iniciar(char m[][3])
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
m[i][j] = ' ';
}
}
}
But he is not going through this method here:
bool localVazio(int m[][3], int lin, int col)
{
return m[lin][col] == ' ';
}
Which by default would return true to empty.