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

时间:2017-11-01 18:49:57

标签: c

我使用 do和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 ' ';
}

2 个答案:

答案 0 :(得分:1)

if(done=='X')应为if(done == '1'),因为matrix包含12,而不是XO

要判断游戏是否为平局,请保持移动次数的计数,并在达到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);
}

答案 1 :(得分:0)

将布尔变量“draw”设置为false,如果没有玩家获胜并且所有9个移动都已完成并且没有空闲块可用,则将“draw”设置为true。并根据“平局”计算结果,即游戏是否平局。