Firstchess

a very simple open source chess program with a command line interface written by Pham Hong Nguyen in C for didactic purpose, introduced in 2002. Since FirstChess lacks en passant and castling, there is implicit invitation to improve it. || toc =Description=
 * Home * Engines * FirstChess**
 * [[image:firstchess.png link="http://devwebcl.atarionline.pl/cc65/firstchess.png"]] ||~ || **FirstChess**,
 * FirstChess screen ||~ ||^ ||

Board
The 8x8 board consists of two arrays for piece type and color. code format="cpp"
 * 1) define	PAWN   0x0
 * 2) define	KNIGHT 0x1
 * 3) define	BISHOP 0x2
 * 4) define	ROOK   0x3
 * 5) define	QUEEN  0x4
 * 6) define	KING   0x5
 * 7) define	EMPTY  0x6
 * 8) define	WHITE  0x0
 * 9) define	BLACK  0x1

int piece[64] = { ROOK, KNIGHT,BISHOP,QUEEN, KING,  BISHOP,KNIGHT,ROOK, PAWN, PAWN,  PAWN,  PAWN,  PAWN,  PAWN,  PAWN,  PAWN, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, PAWN, PAWN,  PAWN,  PAWN,  PAWN,  PAWN,  PAWN,  PAWN, ROOK, KNIGHT,BISHOP,QUEEN, KING,  BISHOP,KNIGHT,ROOK };

int color[64] = { BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE }; code

Search
The negamaxed alpha-beta lacks any move ordering techniques: code format="cpp" static int Search(int alpha, int beta, int depth, MOVE * pBestMove) { int i, value, havemove, movecnt; MOVE moveBuf[200], tmpMove; nodes++; /* visiting a node, count it */ havemove = 0; pBestMove->type = MOVE_TYPE_NONE; movecnt = Gen(side, moveBuf); /* generate all moves for current position */ /* loop through the moves */ for (i = 0; i < movecnt; ++i) { mm2 = moveBuf[i]; if (!MakeMove) { TakeBack; continue; }   havemove = 1; if (depth - 1 > 0) /* If depth is still, continue to search deeper */ value = -Search(-beta, -alpha, depth - 1, &tmpMove); else /* If no depth left (leaf node), go to evalute that position */ value = Eval; TakeBack; if (value > alpha) { /* This move is so good and caused a cutoff */ if (value >= beta) return beta; alpha = value; *pBestMove = moveBuf[i]; /* so far, current move is the best reaction * for current position */ } }  if (!havemove) { /* If no legal moves, that is checkmate or                    * stalemate */ if (IsInCheck(side)) return -MATE + ply; /* add ply to find the longest path to lose or shortest path to win */ else return 0; } return alpha; } code

Evaluation
FirstChess' evaluation considers material with following point values: code format="cpp"
 * 1) define  VALUE_PAWN      100
 * 2) define  VALUE_KNIGHT    300
 * 3) define  VALUE_BISHOP    300
 * 4) define  VALUE_ROOK      500
 * 5) define  VALUE_QUEEN     900
 * 6) define  VALUE_KING      10000

int Eval { int value_piece[6] = {VALUE_PAWN, VALUE_KNIGHT, VALUE_BISHOP, VALUE_ROOK, VALUE_QUEEN, VALUE_KING}; int i, score = 0; for (i = 0; i < 64; i++) { if (color[i] == WHITE) score += value_piece[piece[i]]; else if (color[i] == BLACK) score -= value_piece[piece[i]]; } if (side == WHITE) return score; return -score; } code

=See also=
 * Ax

=Forum Posts=
 * [|FirstChess - a crazy project!] by Pham Hong Nguyen, CCC, July 24, 2002
 * [|Beginner programmer Winboard and chess computing advice] by tr2, Winboard Forum, June 09, 2009

=External Links=
 * [|Index of /cc65] from [|atarionline.pl]

=References= =What links here?= include component="backlinks" page="FirstChess" limit="40"
 * Up one level**