【问题标题】:Saving Data in a Basic Tic-Tac-Toe Game在基本井字游戏中保存数据
【发布时间】:2012-01-11 19:28:17
【问题描述】:

我对 C 语言比较陌生。在 Kochan 的“C 语言编程”中,我目前正在使用 if-else 语句。我正在尝试编写一个基本的井字游戏,但我遇到了一些困难。一旦玩家放置了 x 或 o,我不确定如何保存棋盘。这是我到目前为止的代码:

#include <stdio.h>

int main (void)
{
    int board = "_|_|_\n
                 _|_|_\n
                  | | \n";
    int player1, player2;

    printf ("Print %i", board)

    printf("Player 1, it's your move:")
    scanf("%i", player1)

    if(player1 == "upLeft")
        printf("x|_|_\n
                _|_|_\n
                _|_|_\n
etc.

我还是初学者,无法实现此功能吗?

【问题讨论】:

  • 您真的打算对每个情况进行编码吗?我会先尝试用铅笔和纸解决问题。先写一个算法。您可以使用数组来存储数据。
  • 这就是我的计划。有更简单的解决方案吗?
  • 阅读数组 - 它会让您的生活更轻松。 en.wikibooks.org/wiki/C_Programming/Arrays
  • 你还是初学者吗?对不起,是的。即使您将第一个语句编写为 char* 而不是 int,C 也不接受文字返回。即使有,板子也不会正确显示,因为第二行和第三行的开头空间比第一行多。而且您不能将像 player1 这样的整数与像“upleft”这样的字符串进行比较。而且您不应该为没有参数的函数编写 (void) 。还有,他们说了什么。
  • Lister 先生,您的错误清单有何帮助? OP 是新的,应该鼓励并指出正确的方向,而不是被告知代码无法编译的所有原因。另外,我刚刚查了一下,int main (void) 在 C 语言中是可以接受和首选的!你每天都会学到新东西!

标签: c


【解决方案1】:

首先,这没有意义:

int board = "_|_|_\n
             _|_|_\n
              | | \n";

int 不是字符串,您不能将字符串分配给 int。在 C 中,字符串是字符数组:

char board[] = "_|_|_\n_|_|_\n | | \n";

这更有意义。但是存储井字棋的状态确实不是一个好办法。你应该做的是为板上的每个位置存储一些值。不用担心板子的实际格式,显示的时候可以随意格式化。所以以这种方式存放你的电路板:

char board[9] = "---------";

“-”表示空格为空。当玩家移动时,您将数组中适当位置的角色替换为“X”或“O”(或 1 和 2,或任何其他适合您的值)。当您从用户那里获得一些输入时,您将只更改板数组中的相应值。例如,如果从左上角开始将位置编号为 0-8,则中间行中最右边的位置将是位置 5。 (记住,C 数组是从零开始的,所以第一个索引是 0。)所以如果用户想在那个位置放一个 X,你会说:

board[5] = 'X';

接下来,您可能想要编写一个打印板的函数。 是你可以插入任何你喜欢画板的字符的地方。

最后,您将需要使用某种循环来重复读取用户的输入、修改板的状态、打印板,并可能显示提示。

【讨论】:

  • 需要编辑:char board[] = int board = "_|_|_\n_|_|_\n | | \n";
【解决方案2】:

第一件事,你不能存储

"_|_|_\n
 _|_|_\n
  | | \n";

在一个整数变量中。您需要其他类型的变量(如char *char a[][] 等)。

OTOH,伪代码如下。请尝试并按照此内容自行编写C 程序。

Let row = 3 and column = 3
Declare an array[row][column] and fill it all with 0
Let 1 represent the input of user-1 and 2 represent the input of user-2 (in the array)
i.e. if a[2][2] = 1 means, user-1 marked that location.
while ( ! all_locations_filled() ) {
    take input from user-1
    if user-1 chooses a valid_location(location) to mark, then mark_location(user-1, location)
    check if user-1 won_the_game(user-1), if so break and congratulate user-1! 

    take input from user-2
    if user-2 chooses a valid_location(location) to mark, then mark_location(user-2, location)
    check if user-2 won_the_game(user-2), if so break and congratulate user-2! 

}

valid_location(location l)
{
    return array[l.row][l.column] == 0;
}

mark_location(user u, location l)
{
    array[l.row][l.column] = (u==user-1) ? 1 : 2;
}

display_board()
{
    for i=0 to row
       for j=0 to col
           if array[i][j] == 0 print "" 
           else print array[i][j]
    /* print blank when that location is not yet marked */
}

all_locations_filled()
{
    for i=0 to row
       for j=0 to col
           if array[i][j] == 0
               return false    
    return true
}

won_the_game(user u)
{
    /* You need to write the logic here */
    :P
}

【讨论】:

  • 是的,我想知道这一点。但是char变量不是只有一个字符才用吗?
  • 是的,char 只能存储一个大小为一字节的字符!例如,char a = 'a';
【解决方案3】:

您可以使用二维数组来表示棋盘,也可以使用一些小的 int,其中 0 表示什么都没有,1 表示 X,2 表示 0。

你不能保存这样的东西

 int board = "X|_|_\n
              _|_|_\n
               | | \n";

你可以有类似 board[0][0]=1;

然后您可以迭代该数组,如果它的 1 打印 X。

【讨论】:

    【解决方案4】:

    这是一个初学者的问题,但你是初学者,所以没关系!让我们提出一些主要问题:

    所以,您是说要保存棋盘状态。你想保存什么?在程序中的任何时候,您希望能够查找什么?动作的历史?板子长什么样?每个角落包含什么?当您从用户那里获得一些输入时,这些中的每一个都建议了一种不同的方式来更改变量

    正如其他人所说,你不能用int 类型做所有这些事情,即使你可以,这个程序仍然太难和令人沮丧,除非你的工具箱里有更多的工具。第 7 章是数组,这将非常有用,第 10 章是字符串,它将向您展示如何在 C 中以正确的方式处理所有这些字符串。所以我对您的建议是多读几章这本书大局将开始变得更有意义。快乐的黑客攻击!

    【讨论】:

      【解决方案5】:

      这是一个小模板,就像我给出的一样,在 GetBestMove() 函数中为计算机移动(如果您正在构建 AI)编写您自己的逻辑,否则在 StartGame 函数中用 GetMove 函数替换对 GetBestMove 的调用

      #include<stdio.h>
      #define HUMAN_WIN 1
      #define DRAW 0
      #define HUMAN_LOSE -1
      #define HUMAN_COIN 'X'
      #define COMP_COIN 'O'
      #define DEFAULT 0xFFF
      
      int main(void)
      {
         StartGame();
      }
      
      enum turnOf{ Human = 0, Computer};
      enum gameEndState{Lose = -1, Draw, Win};
      int humanMove = 1;
      int computerMove = 1;
      char _board[3][3] = {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
      
      void StartGame()
      {
         enum turnOf turn = 0;
         enum gameEndState state = -2;
      
         while(1)
         {
            turn = 1 - turn;
            if(turn == Human)
            {
               GetMove();
               UpdateBoard(humanMove, turn);
            }
            else if(turn == Computer)
            {
               GetBestMove(turn);
               UpdateBoard(computerMove, turn, _board);
            }
            state = win(_board);
            switch(state)
            {
               case Win:
                  NotifyUser("You win.");exit(0);
               case Draw:
                  NotifyUser("Match draw.");exit(0);
               case Lose:
                  NotifyUser("You Lose.");exit(0);
            }
         }
      }
      
      int depth = 0;
      int GetBestMove(enum turnOf turn, int *x, int *y)
      {
         depth++;
         int i, j, MOV = -10, BESTMOVx, BESTMOVy;
         enum turnOf now = turn;
         char pebble = (now == Human) ? HUMAN_COIN : COMP_COIN;
      
         for(i=0; i<3; i++)
         {
            for(j=0; j<3; j++)
            {
               if(_board[i][j] == '-')
               {
                  _board[i][j] = pebble;
                  now = 1 - now;
      
                  int condition = win(_board);
      
                  if(condition != DRAW || condition != DEFAULT)
                  {
                     return (condition == HUMAN_LOSE) ? (depth - 10) : (10 - depth);
                  }
                  else
                  {
                     int state = GetBestMove(now, BESTMOVx, BESTMOVy);
                     if(state > MOV)
                     {
                        MOV = state;
                     }
                  }
               }
            }
         }
      }
      
      int win(char a[3][3])
      {
         char pebble = HUMAN_COIN;
         int i, j, p = 0;
      
         i=0;
         for(j = 0; j < 3; j++)
            if(a[i][j] == pebble && a[i+1][j] == pebble && a[i+2][j] == pebble) return HUMAN_WIN;
         j=0;
         for(i = 0; i < 3; i++)
            if(a[i][j] == pebble && a[i][j+1] == pebble && a[i][j+2] == pebble) return HUMAN_WIN;
         if(a[0][0] == pebble && a[1][1] == pebble && a[2][2] == pebble) return HUMAN_WIN;
         else if(a[0][2] == pebble && a[1][1] == pebble && a[2][0] == pebble) return HUMAN_WIN;
      
         /// Recheck for lose
         pebble = COMP_COIN;
         i=0;
         for(j = 0; j < 3; j++)
            if(a[i][j] == pebble && a[i+1][j] == pebble && a[i+2][j] == pebble) return HUMAN_LOSE;
         j=0;
         for(i = 0; i < 3; i++)
            if(a[i][j] == pebble && a[i][j+1] == pebble && a[i][j+2] == pebble) return HUMAN_LOSE;
         if(a[0][0] == pebble && a[1][1] == pebble && a[2][2] == pebble) return HUMAN_LOSE;
         else if(a[0][2] == pebble && a[1][1] == pebble && a[2][0] == pebble) return HUMAN_LOSE;
      
         for(i = 0; i < 3; i++)
            for(j = 0; j < 3; j++)
               if(a[i][j] == '-') p++;
      
         if(p == 0) return DRAW;
      
         return DEFAULT;
      }
      
      void GetMove()
      {
         int x, y;
         LoadFrame(_board);
         printf("\nEnter your move : ");
         humanMove = getche() - 48;
      
         if(!(humanMove > 0 && humanMove < 10))
         {
            NotifyUser("Enter a valid location.");
            GetMove();
         }
         GetCordinates(&x, &y, humanMove);
      
         if(_board[x][y] != '-')
         {
            NotifyUser("The place is nonEmpty.");
            GetMove();
         }
      }
      
      
      void UpdateBoard(int move, enum turnOf player, char board[3][3])
      {
         int x, y;
         char pebble = (player == Human) ? HUMAN_COIN : COMP_COIN;
      
         GetCordinates(&x, &y, move);
      
         board[x][y] = pebble;
         LoadFrame(board);
      }
      
      void LoadFrame(char board[3][3])
      {
         int x, y;
         system("cls");
         for(x = 0; x < 3; x++)
         {
            printf("\n\t  ");
            for(y = 0; y < 3; y++)
            {
               printf(" %c ", board[x][y]);
            }
         }
      }
      
      void GetCordinates(int *x, int *y, int move)
      {
         switch(move)
         {
            case 1: *x = 0; *y = 0; break;
            case 2: *x = 0; *y = 1; break;
            case 3: *x = 0; *y = 2; break;
            case 4: *x = 1; *y = 0; break;
            case 5: *x = 1; *y = 1; break;
            case 6: *x = 1; *y = 2; break;
            case 7: *x = 2; *y = 0; break;
            case 8: *x = 2; *y = 1; break;
            case 9: *x = 2; *y = 2; break;
         }
      }
      
      void NotifyUser(const char* message)
      {
         printf("\n\n%s\a", message);
         getch();
         system("cls");
         LoadFrame(_board);
      }
      

      【讨论】:

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