【问题标题】:Printing a checker board打印棋盘
【发布时间】:2012-11-02 06:38:14
【问题描述】:

我目前正在编写一个检查程序,它使用 alpha-beta 修剪和启发式来玩游戏。我试图在评估板时打印出板,但我遇到了让它工作的问题。我的老师给了我们运行程序所需的所有代码,减去了评估函数和 alpha-beta 修剪函数。打印棋盘的方法与寻找棋盘游戏最佳走法的方法不同。

下面是搜索函数类头的部分(它不包括所有方法调用)和我们要打印板子的 evalBoard 方法

 double evalBoard1(State *state) {
      int x, y;
      double rval = 0.0;
      double rval2 = 0.0;
      evals++;
      for (x = 0; x < 8; x++)
           for (y = 0; y < 8; y++) {
                if (x % 2 != y % 2 && !empty(state->board[y][x])) {
                     if (king(state->board[y][x])) { /* King */
                          if (((state->board[y][x] & White) && !player1) 
                                  || (!(state->board[y][x] & White) && player1))
                               rval += 2.0;
                else
                    rval2 += 2.0;
                } else if (piece(state->board[y][x])) { /* Piece */
                      if (((state->board[y][x] & White) && !player1)
                             || (!(state->board[y][x] & White) && player1))
                           rval += 1.0;
                      else
                           rval2 += 1.0;
                }
            }
       }
     state->PrintBoard(); //should print the board
     fprintf(stderr,"Value = %g\n",rval); //prints the evaluation of that board
     if(rval <= 0.0) return -10000.0;
     if(rval2 <= 0.0) return 10000.0;
     return rval - rval2;
}

 #ifndef COMPUTER_H
 #define COMPUTER_H

 #define Empty 0x00
 #define Piece 0x20
 #define King 0x60
 #define Red 0x00
 #define White 0x80

 #define number(x) ((x)&0x1f)
 #define empty(x) ((((x)>>5)&0x03)==0?1:0)
 #define piece(x) ((((x)>>5)&0x03)==1?1:0)
 #define king(x) ((((x)>>5)&0x03)==3?1:0)
 #define color(x) ((((x)>>7)&1)+1)

 #define Clear 0x1f

 typedef struct{
     int player;
     char board[8][8];
     char movelist[48][12];  
     int numLegalMoves;
 }State;
 //all method calls occur after here
 #endif

下面是 checkers.h 文件(不包括 PrintBoard() 之外的所有其他方法调用)和 checkers.c 文件(仅包括 printBoard 方法和 Struct square 调用)

struct Square square[16][16];


void PrintBoard() {
int board[8][8];
int x,y;
char ch = 127;

for(y=0; y<8; y++)
{
   for(x=0; x<8; x++)
   {
       if(x%2 != y%2) {
           if(square[y][x].state) {
               if(square[y][x].col) 
               {
                  if(square[y][x].state == King) board[y][x] = 'B';
                  else board[y][x] = 'b';
               }
               else
               {
                  if(square[y][x].state == King) board[y][x] = 'A';
                  else board[y][x] = 'a';
               }
           } else board[y][x] = ' ';
       } else board[y][x] = ch;
       printf("%c",board[y][x]);
   }
   printf("\n");
}
}


 #ifndef CHECKERS_H
 #define CHECKERS_H

 #define Empty 0
 #define Piece 1
 #define King 2

 #define HUMAN 1
 #define COMPUTER 2

 struct Square {
         Widget widget;
         int val;
         int state;
         int col;
         int hilite;
 };

 void PrintBoard();
 #endif

我试图只调用 state->PrintBoard() 但程序无法识别该调用。我还尝试将 Square 结构添加到计算机头文件中的 State 结构,但这也产生了错误。我还在 computer.c 文件中创建了一个新的 PrintBoard 方法,但它不知道每个方块中的颜色状态。任何帮助将不胜感激,如果需要,我可以发布更多代码。

【问题讨论】:

  • 为什么您认为 PrintBoard 与状态相关联?这是 C 还是 C++?我觉得你很困惑。 C 中没有类或方法之类的东西。
  • 我们的老师给了我们运行这个程序的所有代码,除了实际的搜索功能,但要求我们使用不同类的 PrintBoard() 方法打印出每个评估状态的板。我只是在试图弄清楚如何将该方法用于另一个需要访问 Square 结构的类时遇到问题
  • 一种肮脏的方法是在任何类之外定义 PrintBoard() 函数,并在您需要的每个类中使用它。

标签: c struct


【解决方案1】:

根据您当前编写代码的方式,PrintBoard 是一个独立函数,因为它应该在 C 程序中......它不是 State 类的方法,就像您在 C++ 中那样。因此,如果您想调用state-&gt;PrintBoard(),则必须将该函数设为State 类的方法并使用C++ 编译器来编译您的程序……否则,如果您想保留它作为一个立场-alone 函数并使用 C 编译器,您必须将 State* 参数添加到 PrintBoard,然后像 PrintBoard(state) 一样调用它。

【讨论】:

    【解决方案2】:

    将方法PrintBoard() 放在State 结构中,并改用C++。然后你可以拨打state-&gt;PrintBoard()

     typedef struct{
       void PrintBoard() { // <---this is what you need to do
         ...
       }
       int player;
       char board[8][8];
       char movelist[48][12];  
       int numLegalMoves;
     }State;
    

    如果您不想这样做,则将其称为 PrintBoard(state) 并更改 PrintBoard 函数。

    另外,决定你是否在做面向对象。你听起来很困惑。

    【讨论】:

    • 我没有写任何代码。它是我们老师给我们的,我们只需要实现实际的搜索功能。但是,他希望我们使用他预先编写的代码并对其进行修改,以便您可以在每个评估状态打印板,以确保启发式函数是正确的。
    • 然后将该函数也放入 State 类中。另外,您使用的是什么语言? C 还是 C++?
    • 我们在这个程序中使用 C
    • 那你为什么要使用面向对象的东西呢?即使State 结构有一个名为PrintBoard() 的方法,你也不能在C 中执行state-&gt;PrintBoard()
    • 这就是我的老师,当我在他的办公时间问他时,他说我应该能够对他的代码进行一些小的调整。
    【解决方案3】:

    大多数(如果不是全部)游戏程序员使用一维板。此外,对于跳棋,您可以省略一半的方格,因为它们没有被使用。

    【讨论】:

    • 是的,我知道这是相关的,但仍然不是答案...您应该在评论中说...
    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 2013-08-08
    • 2013-11-21
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多