Older Version Newer Version

GerdIsenberg GerdIsenberg Nov 27, 2014

**[[Home]] * [[Engines]] * FirstChess**
|| [[image:firstchess.png link="http://devwebcl.atarionline.pl/cc65/firstchess.png"]] ||~ || **FirstChess**,
a very simple [[Open Source Engines|open source chess program]] with a [[CLI|command line interface]] written by [[Pham Hong Nguyen]] in [[C]] for didactic purpose, introduced in 2002 <ref>[[http://www.stmintz.com/ccc/index.php?id=242289|FirstChess - a crazy project!]] by [[Pham Hong Nguyen]], [[CCC]], July 24, 2002</ref>. Since FirstChess lacks [[En passant|en passant]] and [[Castling|castling]], there is implicit invitation to improve it. ||
|| FirstChess screen <ref>firstchess.png  from [[http://devwebcl.atarionline.pl/cc65/|Index of /cc65]]</ref> ||~ ||^ ||
[[toc]]
=Description=
==Board==
The [[8x8 Board|8x8 board]] consists of two [[Array|arrays]] for [[Pieces#PieceTypeCoding|piece type]] and color.
[[code format="cpp"]]
#define	PAWN    0x0
#define	KNIGHT  0x1
#define	BISHOP  0x2
#define	ROOK    0x3
#define	QUEEN   0x4
#define	KING    0x5
#define	EMPTY   0x6
#define	WHITE   0x0
#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 [[Negamax|negamaxed]] [[Alpha-Beta|alpha-beta]] lacks any [[Move Ordering|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|evaluation]] considers [[Material|material]] with following [[Point Value|point values]]:
[[code format="cpp"]]
#define   VALUE_PAWN      100
#define   VALUE_KNIGHT    300
#define   VALUE_BISHOP    300
#define   VALUE_ROOK      500
#define   VALUE_QUEEN     900
#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=
* [[http://www.stmintz.com/ccc/index.php?id=242289|FirstChess - a crazy project!]] by [[Pham Hong Nguyen]], [[CCC]], July 24, 2002 
* [[http://www.open-aurec.com/wbforum/viewtopic.php?f=2&t=50206|Beginner programmer Winboard and chess computing advice]] by tr2, [[Computer Chess Forums|Winboard Forum]], June 09, 2009

=External Links=
* [[http://devwebcl.atarionline.pl/cc65/|Index of /cc65]] from [[http://devwebcl.atarionline.pl/|atarionline.pl]] <ref>oups, better replace "unsigned char" by "int"</ref>

=References= 
<references />
=What links here?= 
[[include component="backlinks" page="FirstChess" limit="40"]]
**[[Engines|Up one level]]**