0
I am trying to make an old game in C language but in player 1’s first move depending on where to put the 'X' the program is already ending declaring the player a winner. Following this my code:
#include <stdio.h>
#include <string.h>
int main(){
int x, y, cont;
char m[3][3], velha[3][3];
for(x=0; x<3; x++){
for(y=0; y<3; y++){
m[x][y]='.';
}
}
for(x=0; x<3; x++){
for(y=0; y<3; y++){
printf("%c\t", m[x][y]);
}
printf("\n");
}
while(cont<9){
if(cont%2==0){
printf("Jogador 1\n");
printf("Digite qual a linha preencher ");
scanf("%d", &x);
printf("Digite qual coluna preencher");
scanf("%d", &y);
m[x][y]='X';
velha[x][y]='X';
if(velha[0][0]==velha[1][1] && velha[1][1]==velha[2][2] ||
velha[0][0]==velha[0][1]&& velha[0][1]==velha[0][2] ||
velha[1][0]==velha[1][1] && velha[1][1]==velha[1][2] ||
velha[2][0]==velha[2][1] && velha[2][1]==velha[2][2]||
velha[0][0]==velha[1][0] && velha[1][0]==velha[2][0] ||
velha[0][1]==velha[1][1] && velha[1][1]==velha[1][2] ||
velha[0][2]==velha[1][2] && velha[1][2]==velha[2][2] || velha[0][2]==velha[1][1] && velha[1][1]==velha[3][0] ){
printf("Jogador 1 venceu \n");
cont=10;
}
}
else if(cont%2!=0){
printf("Jogador 2\n");
printf("Digite qual linha preencher ");
scanf("%d", &x);
printf("Digite qual coluna preencher ");
scanf("%d", &y);
m[x][y]='0';
velha[x][y]='0';
if(velha[0][0]==velha[1][1] && velha[1][1]==velha[2][2] ||
velha[0][0]==velha[0][1]&& velha[0][1]==velha[0][2] ||
velha[1][0]==velha[1][1] && velha[1][1]==velha[1][2] ||
velha[2][0]==velha[2][1] && velha[2][1]==velha[2][2]||
velha[0][0]==velha[1][0] && velha[1][0]==velha[2][0] ||
velha[0][1]==velha[1][1] && velha[1][1]==velha[1][2] ||
velha[0][2]==velha[1][2] && velha[1][2]==velha[2][2] || velha[0][2]==velha[1][1] && velha[1][1]==velha[3][0] ){
printf("Jogador 2 venceu \n");
cont=10;
}
}
cont++;
for(x=0; x<3; x++){
for(y=0; y<3; y++){
printf("%c\t", m[x][y]);
}
printf("\n");
}
}
if(cont==9){
printf("Nao teve vencedores ");
}
}
You do not start the value of
cont
before arriving at thewhile
. Since you didn’t start this value, it can come with any memory junk. Docont = 0
before you get into the loop– Jefferson Quesado
Another thing, on the board print, inside the
while
, it will print the size of theX
as distinct lines, while theY
will be the columns. Usually it is the other way around, if you are aware of this behavior and it is desired, it is great– Jefferson Quesado