【问题标题】:Tic tac toe program in C, winning conditions not workingC中的井字游戏程序,获胜条件不起作用
【发布时间】:2015-12-15 23:26:42
【问题描述】:

这是一个程序,可让您在计算机上玩 5x5 井字游戏。我将为计算机实施更智能的移动,但首先我需要修复获胜的支票。如果您以列或对角线获胜,则有效,但以行获胜仅适用于第 2+3 行,而不适用于第 4 或 5 行。谁能告诉我为什么?我只使用 C。

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

char matrix[5][5];  /* the tic tac toe matrix */

char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);

int main(void)
{
  char done;

  printf("This is the game of Tic Tac Toe.\n");
  printf("You will be playing against the computer.\n");

  done =  ' ';
  init_matrix();

  do {
    disp_matrix();
    get_player_move();
    done = check(); /* see if winner */
    if(done!= ' ') break; /* winner!*/
    get_computer_move();
    done = check(); /* see if winner */
  } while(done== ' ');

  if(done=='X') printf("You won.\n");
  else printf("Computer won.\n");
  disp_matrix(); /* show final positions */

  return 0;
}

/* Initialize the matrix. */
void init_matrix(void)
{
  int i, j;

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

/* Get a player's move. */
void get_player_move(void)
{
  int x, y;

  printf("Enter X,Y coordinates for your move: ");
  scanf("%d%*c%d", &x, &y);

  x--; y--;

  if(matrix[x][y]!= ' '){
    printf("Invalid move, try again.\n");
    get_player_move();
  }
  else matrix[x][y] = 'X';
}

/* Get a move from the computer. */
void get_computer_move(void)
{
  int i, j;
  for(i=0; i<5; i++){
    for(j=0; j<5; j++)
      if(matrix[i][j]==' ') break;
    if(matrix[i][j]==' ') break;
  }

  if(i*j==25)  {
    printf("Draw.\n");
    exit(0);
  }
  else
    matrix[i][j] = 'O';
}

/* Display the matrix on the screen. */
void disp_matrix(void)
{
  int t;

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

/* See if there is a winner. */
char check(void)
{
  int i;

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

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

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

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

  return ' ';
}

【问题讨论】:

  • 哪个井字游戏需要 5 X 5 盒?有没有提到逻辑?
  • get_computer_move() 中的第二个if(matrix[i][j]==' ') break; 可能会导致越界访问。
  • “它不起作用”不是错误描述。您预计会发生什么以及会发生什么?
  • 抱歉,当您完成该行时,它不会宣布获胜者,而是让您继续游戏。然后随机时间之后它会宣布你是赢家,我想这可能是你在下面的行中放了更多的 X,但我不确定。
  • 你对休息有什么建议?如果我删除了中断,那么会有一个空的 if 语句,它不会在棋盘上放置任何动作

标签: c


【解决方案1】:

它不起作用,因为它会在完全空的行上过早地返回 ' '。所以你必须实施额外的检查:

if(matrix[i][0]==matrix[i][1] &&
   matrix[i][0]==matrix[i][2] &&
   matrix[i][0]==matrix[i][3] &&
   matrix[i][0]==matrix[i][4] &&
   matrix[i][0] != ' ')
      return matrix[i][0];

这也适用于其他检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多