搜索
查看: 427|回复: 6
打印 上一主题 下一主题

为什么不行

[复制链接]
跳转到指定楼层
楼主
 楼主| 发表于 2018-9-19 22:48:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5啊哈币
// program 5.8 tic-tac-toe
#include <stdio.h>

int main(void)
{
  int player = 0;     //current player number - 1 or 2
  int winner = 0;     //the winning player number
  int choice = 0;     //chosen square
  unsigned int row = 0;     //row index for a square
  unsigned int column = 0;     //column index for a square
  char board[3][3] = {     //the board
  {'1','2','3'},     //initial values are characters '1' to'9'
  {'4','5','6'},     //used to select a vacant square
  {'7','8','9'}     //for a player's turn  
  };

//the main game loop.the game continues for up to 9 turns
//as long as there is no winner
for(unsigned int i = 0; i < 9 && winner == 0; ++i)
{
  //display the board
  printf("\n");
  printf(" %c | %c| %c\n", board[0][0], board[0][1],board[0][2]);
  printf("---+---+---\n");
  printf(" %c | %c | %c\n",board[1][0],board[1][1],board[1][2]);
  printf("---+---+---\n");
  printf(" %c | %c | %c\n",board[2][0],board[2][1],board[2][2]);
  player = i%2 + 1; // select selection
  do
  {
    printf("player %d, please enter a valid square number "
           "for where you want to place your %c: ",
            player,(player == 1) ? 'x' : 'o');
    scanf("%d, &choice);

    row = --choice/3;     //get row index of square
    column = choice % 3;     //get column index of square
}while(choice < 0 ||choice > 8 || board[row][column] > '9');

// insert player symbol
board[row][column] = (player == 1) ? 'x' : 'o';

//check for a winning line - diagonals first
if((board[0][0]==board[1][1] && board[0][0]==board[2][2]||
   (board[0][2]==board[1][1] && board[0][2]==board[2][0]))
winner = player
elsa
{
  //check rows and columns for a winning line
  for(unsigned int line = 0; line<=2;++line)
{
  if((board[line][0] ==board[line][1] && board[line][0] ==board[line][2] ||
     (board[0][line]==board[1][line] &&board[0][line] ==board[2][line]))
winner = player
}
}
}
// game is over so display the final board
printf("\n");
  printf(" %c | %c| %c\n", board[0][0], board[0][1],board[0][2]);
  printf("---+---+---\n");
  printf(" %c | %c | %c\n",board[1][0],board[1][1],board[1][2]);
  printf("---+---+---\n");
  printf(" %c | %c | %c\n",board[2][0],board[2][1],board[2][2]);

  //display result message
  if(winner)
    printf("\ncongratulations, player %d, you are the winner!|n", winner);
    else
      printf("\nhow boring, it is a draw\n");
  return 0;
}

楼主新帖
楼主热帖
沙发
发表于 2018-9-19 22:48:57 | 只看该作者

  1. // program 5.8 tic-tac-toe
  2. #include <stdio.h>

  3. int main(void)
  4. {
  5.   int player = 0;     //current player number - 1 or 2
  6.   int winner = 0;     //the winning player number
  7.   int choice = 0;     //chosen square
  8.   unsigned int row = 0;     //row index for a square
  9.   unsigned int column = 0;     //column index for a square
  10.   char board[3][3] = {     //the board
  11.   {'1','2','3'},     //initial values are characters '1' to'9'
  12.   {'4','5','6'},     //used to select a vacant square
  13.   {'7','8','9'}     //for a player's turn  
  14.   };
  15. unsigned int i,line;
  16. //the main game loop.the game continues for up to 9 turns
  17. //as long as there is no winner
  18. for(i = 0; i < 9 && winner == 0; ++i)
  19. {
  20.   //display the board
  21.   printf("\n");
  22.   printf(" %c | %c| %c\n", board[0][0], board[0][1],board[0][2]);
  23.   printf("---+---+---\n");
  24.   printf(" %c | %c | %c\n",board[1][0],board[1][1],board[1][2]);
  25.   printf("---+---+---\n");
  26.   printf(" %c | %c | %c\n",board[2][0],board[2][1],board[2][2]);
  27.   player = i%2 + 1; // select selection
  28.   do
  29.   {
  30.     printf("player %d, please enter a valid square number "
  31.            "for where you want to place your %c: ",
  32.             player,(player == 1) ? 'x' : 'o');
  33.     scanf("%d", &choice);

  34.     row = --choice/3;     //get row index of square
  35.     column = choice % 3;     //get column index of square
  36. }while(choice < 0 ||choice > 8 || board[row][column] > '9');

  37. // insert player symbol
  38. board[row][column] = (player == 1) ? 'x' : 'o';

  39. //check for a winning line - diagonals first
  40. if((board[0][0]==board[1][1] && board[0][0]==board[2][2]||
  41.    board[0][2]==board[1][1] && board[0][2]==board[2][0]))
  42. winner = player;
  43. else
  44. {
  45.   //check rows and columns for a winning line
  46.   for( line = 0; line<=2;++line)
  47. {
  48.   if((board[line][0] ==board[line][1] && board[line][0] ==board[line][2] ||
  49.      board[0][line]==board[1][line] &&board[0][line] ==board[2][line]))
  50. winner = player;
  51. }
  52. }
  53. }
  54. // game is over so display the final board
  55. printf("\n");
  56.   printf(" %c | %c| %c\n", board[0][0], board[0][1],board[0][2]);
  57.   printf("---+---+---\n");
  58.   printf(" %c | %c | %c\n",board[1][0],board[1][1],board[1][2]);
  59.   printf("---+---+---\n");
  60.   printf(" %c | %c | %c\n",board[2][0],board[2][1],board[2][2]);

  61.   //display result message
  62.   if(winner)
  63.     printf("\ncongratulations, player %d, you are the winner!|n", winner);
  64.     else
  65.       printf("\nhow boring, it is a draw\n");
  66.   return 0;
  67. }

复制代码
板凳
发表于 2018-9-19 23:16:11 | 只看该作者
编译运行代码呀,然后看出错提示,一行一行地来修改。比如第20行的:
for(unsigned int i = 0; i < 9 && winner == 0; ++i)
不能在循环里定义变量,为了不改变你原代码的行号,把 unsigned int i; 移到第17行,然后第20行改成:
for(i = 0; i < 9 && winner == 0; ++i)

继续编译运行提示第35行有错,scanf("%d, &choice); 应改为 scanf("%d", &choice);       漏了一个双引号
地板
发表于 2018-9-19 23:18:35 | 只看该作者
继续,第47、55行的 winner = player 漏了分号,应写成   winner = player ;
5#
发表于 2018-9-20 00:21:55 | 只看该作者
第46行,多了最前的括号,把 ( 去掉。
第48行的 else 错写成 elsa 了,更正回来。
第51行,把 unsigned int line 的定义放在循环之外。
第54行,把最前面的括号 ( 去掉。

OK,这回可以编译通过并运行了,敲代码能错这么多,也真是服了。
6#
 楼主| 发表于 2018-9-20 20:39:06 | 只看该作者
没事 66666666666666666666666666666666666666666666666666666
7#
 楼主| 发表于 2018-9-20 20:39:57 | 只看该作者
能把我的代码再改一下发回来吗??????????????????????????????????
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表