Reputation: 678
I am currently writing a checker program which uses alpha-beta pruning and a heuristic to play the game. I am attempting to print out the board at evaluation of the board and I am running into problems getting it to work. My teacher gave us all the code we need to run the program minus the evaluation function and the alpha-beta pruning function. The method for printing the board is in a different class than the methods for finding the best move for the checker game.
Below are par of the search function class header (it doesn't include all of the method calls) and the evalBoard method which is where we want to print the board
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
Below this is the checkers.h file (it doesn't not include all other method calls beside PrintBoard()) and the checkers.c file (only includes the printBoard method and the Struct square call)
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
I attempted to just call state->PrintBoard() but the program couldn't recognize the call. I also tried adding a Square struct to the State struct in the computer header file but that also created an error. I also created a new PrintBoard method in the computer.c file but it wouldn't know which color state was in each square. Any help would be appreciated and I can post more code that I have if needed.
Upvotes: 1
Views: 2213
Reputation: 9319
Put the method PrintBoard()
inside the State
struct and use C++ instead. Then you can call 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;
If you don't want to do that, then call it like PrintBoard(state)
and change the PrintBoard
function.
And also, decide if you are doing Object-Oriented or not. You sound confused.
Upvotes: 1
Reputation: 32540
With the way your code is currently written, PrintBoard
is a stand-alone function as it should be in a C-program ... it's not a method of the State
class like you would have with C++. Therefore if you want to call state->PrintBoard()
, you are going to have to make that function a method of the State
class and use a C++ compiler to compiler your program... otherwise, if you want to keep it as a stand-alone function and use a C-compiler, you're going to have to add a State*
argument to PrintBoard
, and then call it like PrintBoard(state)
.
Upvotes: 4
Reputation: 44250
Most (if not all) game programmers use a one dimentional board. Also, for checkers you can omit half of the squares, since they are not used.
Upvotes: -2