我是编程新手,我正在打井字游戏。 当我编码时,我意识到我对scanf有疑问。 scanf是否将char和'\ 0'带到一个索引?我尝试了get和fgets函数。唯一的问题是程序无法从播放器中得到答案。我需要您的帮助。
// TIC TAC TOE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main ()
{
puts("WELCOME TO TIC TAC TOE\n\n\n");
int round=0,ctr=1,index;
char location[9]="012345678";
char player[7];
while (round<9)
{
printf("\n\n\n");
printf("\t %c | %c | %c\n",location[0],location[1],location[2]);
printf("\t-------------------\n");
printf("\t %c | %c | %c\n",location[3],location[4],location[5]);
printf("\t-------------------\n");
printf("\t %c | %c | %c\n",location[6],location[7],location[8]);
printf("\n\n\n");
if (ctr%2)
{
strcpy(player,"First");
}
else
{
strcpy(player,"Second");
}
printf("\n\n\n");
printf("Which index do you want to play %s Player\n",player);// i will add game rules
scanf("%d",&index);
printf("X-O? \n");// the answers will enum
scanf("%c",&location[index]);
printf("\n\n\n");
ctr++;
round++;
//printf("\nround %d\n",round);
if (round==8)
break;
}
getchar();
}