关于此代码的一个问题:如果玩家选择的选项已经被选中,我如何让他/她再次选择?
如果我现在编译代码,它将改变用户选择的位置,这不会发生。
Obs:井字游戏的“绘图”应该出现在此之前("\nInform your choice (1-9): "
)
这是代码:
char mat[9];
int main(void)
{
int i,jogada,casa,jogo=0;
for(i=0; i<= 9; i++)
mat[i] = ' ';
do{
printf("\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[1],mat[2],mat[3]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[4],mat[5],mat[6]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n\n",mat[7],mat[8],mat[9]);
printf("\nJOGADOR X ");
printf("\nInforme uma posicao livre (1-9): ");
scanf("%d",&casa);
}while((casa>=1)&&(casa<=9));
if((casa>=1)&&(casa<=9))
{
if(mat[casa] == 'X' || mat[casa] == 'O')
printf(" ");
else
mat[casa] = 'X';
}
printf("\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[1],mat[2],mat[3]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[4],mat[5],mat[6]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n\n",mat[7],mat[8],mat[9]);
if(mat[1] != ' ' && mat[1] == mat[2] && mat[2] == mat[3]) {
jogo++;
printf("X Ganhou!\n");
break; }
else if(mat[4] != ' ' && mat[4] == mat[5] && mat[5] == mat[6]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if(mat[7] != ' ' && mat[7] == mat[8] && mat[8] == mat[9]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if(mat[1] != ' ' && mat[1] == mat[4] && mat[4] == mat[7]) {
printf("X Ganhou!\n");
jogo++;
break;}
else if(mat[2] != ' ' && mat[2] == mat[5] && mat[5] == mat[8]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if(mat[3] != ' ' && mat[3] == mat[6] && mat[6] == mat[9]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if(mat[1] != ' ' && mat[1] == mat[5] && mat[5] == mat[9]){
printf("X Ganhou!\n");
jogo++;
break;}
else if(mat[7] != ' ' && mat[7] == mat[5] && mat[5] == mat[3]) {
printf("X Ganhou!\n");
jogo++;
break; }
else if((mat[1] != ' ') && (mat[2] != ' ') && (mat[3] != ' ') && (mat[4] != ' ') && (mat[5] != ' ') && (mat[6] != ' ') && (mat[7] != ' ') && (mat[8] != ' ') && (mat[9] = ' ')){
printf("Velha!\n");
jogo++;
break;
}
printf("\nJOGADOR O ");
printf("\nInforme uma posicao livre (1-9): ");
scanf("%d",&casa);
if((casa>=1)&&(casa<=9))
{
if(mat[casa] == 'X' || mat[casa] == 'O')
;
else
mat[casa] = 'O';
}
printf("\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[1],mat[2],mat[3]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n",mat[4],mat[5],mat[6]);
printf("--------|-------|-------\n");
printf("\t|\t|\n");
printf("\t|\t|\n");
printf("%c\t|%c\t|%c\n\n",mat[7],mat[8],mat[9]);
if(mat[1] != ' ' && mat[1] == mat[2] && mat[2] == mat[3]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[4] != ' ' && mat[4] == mat[5] && mat[5] == mat[6]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[7] != ' ' && mat[7] == mat[8] && mat[8] == mat[9]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[1] != ' ' && mat[1] == mat[4] && mat[4] == mat[7]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[2] != ' ' && mat[2] == mat[5] && mat[5] == mat[8]) {
printf("O Ganhou!\n");
jogo++;
break;}
else if(mat[3] != ' ' && mat[3] == mat[6] && mat[6] == mat[9]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if(mat[1] != ' ' && mat[1] == mat[5] && mat[5] == mat[9]){
printf("O Ganhou!\n");
jogo++;
break;}
else if(mat[7] != ' ' && mat[7] == mat[5] && mat[5] == mat[3]) {
printf("O Ganhou!\n");
jogo++;
break; }
else if((mat[1] != ' ') && (mat[2] != ' ') && (mat[3] != ' ') && (mat[4] != ' ') && (mat[5] != ' ') && (mat[6] != ' ') && (mat[7] != ' ') && (mat[8] != ' ') && (mat[9] = ' ')){
printf("Velha!\n");
jogo++;
break;
}
}
system("PAUSE");
return 0;
}