打印棋盘格

时间:2011-10-19 20:37:06

标签: c struct

我目前正在编写一个使用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方形调用)

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

3 个答案:

答案 0 :(得分:4)

根据您当前编写代码的方式,PrintBoard是一个独立的函数,因为它应该在C程序中......它不像State类的方法一样有C ++。因此,如果要调用state->PrintBoard(),则必须使该函数成为State类的方法,并使用C ++编译器编译程序...否则,如果要保留它作为一个独立的函数并使用C编译器,您将不得不向State*添加PrintBoard参数,然后将其称为PrintBoard(state)

答案 1 :(得分:1)

将方法PrintBoard()放在State结构中,然后使用C ++。然后你可以拨打state->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功能。

而且,还要决定你是否正在进行面向对象。你听起来很困惑。

答案 2 :(得分:-2)

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

相关问题