0
I did this function for the game of old:
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 which he chooses a position(row, column) randomly in the matrix, and if it is false goes through the while check, but it never ends.
So: I started all matrix 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’s not passing that method here:
bool localVazio(int m[][3], int lin, int col)
{
return m[lin][col] == ' ';
}
Which by default would return true to empty.
What programming language are you using? C?
– Woss
The problem is probably in the
localVazio()
that never returnstrue
. This code can certainly be simplified, but we can only help knowing the part where there is error. See how to make a [mcve].– Maniero
And why row and column are obtained from a split rest by 2? The variable
m
is 2x2?– Woss