关于C语言中的Tic-Toc-Toe游戏

关于C语言中的Tic-Toc-Toe游戏,c,C,我用C语言编写了这个游戏的完整代码。。但我停在一个点上,我不知道如何键入一个代码来决定比赛是否平局 谢谢 这是我的密码 #include <stdio.h> #include <stdlib.h> char matrix[3][3]; char check(void); void init_matrix(void); void get_player1_move(void); void get_player2_move(void); void disp_matrix(v

我用C语言编写了这个游戏的完整代码。。但我停在一个点上,我不知道如何键入一个代码来决定比赛是否平局

谢谢

这是我的密码

#include <stdio.h>
#include <stdlib.h>

char matrix[3][3]; 
char check(void);
void init_matrix(void);
void get_player1_move(void);
void get_player2_move(void);
void disp_matrix(void);

int main(void)
{
  char done,choise;

  printf("Welcome to the tic-tac-toe game!!!\n\n");
  printf("Rule for playing the game is:\n\n");
  printf("Each player must put the value of raw and column like: 1 2 to put his symbol in\nthe tic-tac-toe board.\n\n");
  printf("The tic-tac-toe board looks like as follows:\n\n");
  init_matrix();
      disp_matrix();
    printf("Are you ready to start the game?   ");
    scanf(" %c",&choise);
    if (choise == 'y'){

  do {

    get_player1_move();
    done = check(); /* if winner or not */
    if(done!= ' ') break; /* winner!*/
    disp_matrix();
    get_player2_move(); 
    disp_matrix();
    done = check(); /* if winner or not */
    if(done!= ' ') break; /* winner!*/

  } while(done== ' ');

     if(done=='X') printf("Player 1 won the game!!!!!\n");
     else printf("Player 2 won the game!!!!!\n");
    }

    else {
    printf("\n\nThank you!!!\n\n");
    printf("We hope you will play the game anther time....");
    }
  return 0;
}

/****************************************************/
void init_matrix(void)
{
  int i, j;

  for(i=0; i<3; i++)
    for(j=0; j<3; j++) matrix[i][j] =  ' ';
}

/****************************************************/
void get_player1_move(void)
{
  int x, y;

  printf("Enter row and column input for player 1: ");
  scanf("%d%*c%d", &x, &y);

  x--; y--;

  if(matrix[x][y]!= ' '){
    printf("You can not choose this row and clumn!! Try again\n");
    get_player1_move();
  }
  else matrix[x][y] = '1';
}

/****************************************************/
void get_player2_move(void)
{
  int x, y;

  printf("Enter row and column input for player 2:  ");
  scanf("%d%*c%d", &x, &y);

  x--; y--;

  if(matrix[x][y]!= ' '){
    printf("You can not choose this row and clumn!! Try again\n");
    get_player2_move();
  }
  else matrix[x][y] = '2';
}

/****************************************************/
void disp_matrix(void)
{
  int t;

  for(t=0; t<3; t++) {
    printf("                         :    :  ");
    printf("\n                       %c :  %c : %c",matrix[t][0],matrix[t][1], matrix [t][2]);
    if(t!=2) printf("\n                     ----:----:----\n");
  }
  printf("\n\n");
}

/****************************************************/

char check(void)
{
  int i;

  for(i=0; i<3; i++)  /* check rows */
    if(matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2])
     return matrix[i][0];

  for(i=0; i<3; i++)  /* check columns */
    if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i])
     return matrix[0][i];

  /* test diagonals */
  if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
     return matrix[0][0];

  if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0])
     return matrix[0][2];

  return ' ';
}
#包括
#包括
字符矩阵[3][3];
字符检查(无效);
void init_矩阵(void);
无效获得玩家1移动(无效);
无效获得玩家2移动(无效);
void disp_矩阵(void);
内部主(空)
{
做得好,选择;
printf(“欢迎参加井字游戏!!!\n\n”);
printf(“玩游戏的规则是:\n\n”);
printf(“每位玩家必须输入raw和column的值,如:1 2,才能将其符号放入tic tac趾板。\n\n”);
printf(“tic-tac趾板看起来如下:\n\n”);
init_矩阵();
disp_矩阵();
printf(“你准备好开始游戏了吗?”);
scanf(“%c”和选择);
如果(选项=='y'){
做{
获取玩家1移动();
完成=检查();/*是否获胜*/
如果(完成!='')中断;/*赢家*/
disp_矩阵();
让玩家2移动();
disp_矩阵();
完成=检查();/*是否获胜*/
如果(完成!='')中断;/*赢家*/
}while(完成=“”);
如果(完成=='X')printf(“玩家1赢得了游戏!!!\n”);
else printf(“玩家2赢得了游戏!!!\n”);
}
否则{
printf(“\n\n谢谢!!!\n\n”);
printf(“我们希望你下次再玩这个游戏……”);
}
返回0;
}
/****************************************************/
void init_矩阵(void)
{
int i,j;
对于(i=0;i将布尔变量“draw”设置为false,如果没有玩家获胜且所有9个移动都完成且没有可用的挡块,则将“draw”设置为true。并根据“draw”计算结果,即游戏是否为平局。

如果(完成='X')
应为
如果(完成='1'))因为
矩阵
包含
1
2
,而不是
X
O

要判断这场比赛是否是平局,请记下移动的次数,当移动到9时打破循环。你只需在玩家1移动后检查,因为玩家2总是以偶数移动

没有必要使用
do{…}while(done='')
因为当
done!=''
时,代码总是从循环中断开。所以只需使用
while(1)
来创建一个无限循环

int count = 0;
while (1) {
    get_player1_move();
    done = check(); /* if winner or not */
    disp_matrix();
    if(done!= ' ' || ++count == 9) break; /* winner or draw */
    get_player2_move(); 
    disp_matrix();
    done = check(); /* if winner or not */
    if(done!= ' ') break; /* winner!*/
    ++count;

}
if (done == ' ') {
    printf("It's a draw!\n");
} else {
    printf("Player %c won the game!!\n", done);
}

如果你不发布代码,那就没办法了。“但我不知道如何键入代码来决定游戏是否被绘制……”
#define MyGameLooksBoss#ifdef MyGameLooksBoss draw()##endif
。你真的认为这个问题可以认真对待吗?你是否编写了测试任何一位玩家是否获胜的代码?我猜你每次移动都会测试,如果形成三行,则结束游戏。如果没有形成三行,并且棋盘已满:这是平局。欢迎来到Stack Overflow Where developerrs学习、共享和建立职业生涯!请不要破坏您的帖子。通过在Stack Exchange网络上发布,您已授予SE分发该内容的不可撤销的权利(根据)。根据SE政策,任何故意破坏行为都将被恢复。如果您想将此帖子与您的帐户解除关联,请参阅@Chemist练习2:在董事会满之前预测抽签;)您必须在
检查()中执行此操作
函数。如果每一行、每一列和每一对角线都有两个不同的玩家编号,则没有人可以进入。在这种情况下,您可以返回
d
,并更改为
If(done='''.| done='d')
谢谢Barmar先生,我可以使用(If stations for and while)转换我的代码吗如果你想在多个地方使用相同的代码,最好使用函数。
check
的代码最好保存在一个函数中。哦,谢谢,但是可以转换吗?@Barmar