Basically i want the variable 'End' to control the while loop. In the while loop there is a function called to check multiple things (Is the game tic tac toe) when the function see a tris or the table full (tie) it should change the variable 'End' to -1 if someone wins or -2 if is a draw so the game should end. (This doesn't happen)
printf("TURN %u\n", COUNT); //conta turno (inglese)
printf("Player %d:\n", Start_Num); //giocata player1
printf("ROW: ");
scanf("%u", &ROW);
if (ROW > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)"); //decisione mossa
scanf("%u", &ROW);
}
printf("COLUMN: ");
scanf("%u", &COLUMN);
if (COLUMN > 2){
puts("ENTER A VALID VALUE (BETWEEN 0 AND 2)");
scanf("%u", &COLUMN);
}
if (Start_Num == 1){ //assegnazione mossa all'array
TABLE[ROW][COLUMN] = P1_SIGN;
}
else {
TABLE[ROW][COLUMN] = P2_SIGN;
}
Print_Table_Full(TABLE); //funzioni
Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, End, Counter_Obj);
COUNT++;
}
The while loop is incomplete but it should work anyway
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN){ //prima riga
puts("PLAYER 1 WINS\n");
End = -1; //valore di vincita
}
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[1][0] && TABLE[1][1] && TABLE[1][2] == P1_SIGN){ //seconda riga
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[1][0] && TABLE[1][1] && TABLE[1][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[2][0] && TABLE[2][1] && TABLE[2][2] == P1_SIGN){ //terza riga
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[2][0] && TABLE[2][1] && TABLE[2][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][0] && TABLE[2][0] == P1_SIGN){ //prima colonna
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][0] && TABLE[2][0] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][1] && TABLE[1][1] && TABLE[2][1] == P1_SIGN){ //seconda colonna
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][1] && TABLE[1][1] && TABLE[2][1] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][2] && TABLE[2][2] == P1_SIGN){ //terza colonna
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][2] && TABLE[2][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][1] && TABLE[2][2] == P1_SIGN){ //diagonale 1
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][0] && TABLE[1][1] && TABLE[2][2] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][1] && TABLE[2][0] == P1_SIGN){ //diagonale 2
puts("PLAYER 1 WINS\n");
End = -1;
}
if(TABLE[0][2] && TABLE[1][1] && TABLE[2][0] == P2_SIGN){
puts("PLAYER 2 WINS\n");
End = -1;
}
for(size_t i = 0; i < 3; i++){
for(size_t j = 0; j < 3; j++){
if(TABLE[i][j] == 'X' || TABLE[i][j] == 'O'){
Counter_Obj++;
}
}
}
if (Counter_Obj == 9){
puts("DRAW!");
End = -2;
}
return End;
}
I even tried to use pointers. The variable End was
int End = 0;
As someone already pointed out in the comments, I'm not sure if your if
conditions are doing what you think they're doing.
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN){ //prima riga
puts("PLAYER 1 WINS\n");
End = -1; //valore di vincita
}
At the moment this piece of code, for example, is checking if TABLE[0][0]
and TABLE[0][1]
are (EDIT:) !=0
and if TABLE[0][2]
is equal to P1_SIGN
. If this is how it supposed to work it's fine, either way you should change all your if
like suggested in the comments (by @MikeCAT).
Another important thing is how you're using your function. You're returning a value (End
) but not storing it anywhere, so you should do:
int result = Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, End, Counter_Obj);
Or, if you prefer to use pointers:
/* function now returns void */
void Verify_Win_Or_Tie(char TABLE[][T], char P1_SIGN, char P2_SIGN, int* End, unsigned int Counter_Obj) // now End is a pointer to int
{
if(TABLE[0][0] && TABLE[0][1] && TABLE[0][2] == P1_SIGN){ //prima riga
puts("PLAYER 1 WINS\n");
// updating pointed value
*End = -1; //valore di vincita
}
/* rest of code */
}
How to use it on main:
Verify_Win_Or_Tie(TABLE, P1_SIGN, P2_SIGN, &End, Counter_Obj); // passing address of End