Click here to Skip to main content
13,344,016 members (95,938 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 19 Jun 2008

Checkers for Pocket PC using a Recursive Min-Max Tree

, 19 Jun 2008
Rate this:
Please Sign up or sign in to vote.
Checkers/Draughts game for Pocket PC, using the recursive Min-Max tree to calculate moves, coded with C# over the .NET Compact Framework


One day, I just typed "checkers" here on The Code Project and found no results for a checkers game, so I decided to put the code here for a checkers game which I coded some time ago and share with you what I did at that time to learn programming for .NET Compact Framework. It was a game meant to be simple and considering that I wanted it to run on a pocket PC, it had to be small and "think" quick, and hopefully, a good player. Otherwise it would be boring (although I think that an invincible player won't be so much fun either).

Basically I needed a recursive approach, so I decided to use a min max game tree instead of reinventing the wheel. But like every coder, you always want to make things perfect. So the next second, I wanted to make more board games and more games (yet simple ones) using the min max method, all of them turn based games.

So... I decided to create reusable modules for those simple turn based games, and also a module for the board games. And the modules finally fit well on other games that I coded later, such as Chess and Othello or Reversi.

Basically the games have three main modules. The game was initially coded for .NET Compact Framework 1.1, so those "modules" are .NET assemblies.

BoardEngine Library

The idea behind this library is just to refactor the classes used and to share that library across the rest of the games coded later.

BoardEngine, the library for the board work has the following classes:

  • Board
  • BoardPosition
  • Piece
  • PieceColor (enum)

GamesPackage Library

GamesPackage, the library for turn based games has the following classes:

  • IPlayer (interface)
  • Moderator (abstract class, but with important base implementation)
  • Delegates and other useful items

The Moderator class is constructed by receiving N objects implementing the IPlayer, interface, and it manages to control how they play. In this case, we can use moderator to allow two different implementations of the game to play (maybe to see who coded the best Checker or Chess playing machine) or you can create a player that only reacts to user input on the device.

The UI

The Graphics are not so fancy but they are good and the game also has some skins (easy ones). This project contains:

The Game UI

  • Implementation for a computer player
  • Implementation for a human player (in its turn, waits for user input)

The Player...

protected double SelectMove(CheckersBoard board, PieceColor color,
   int level, double alpha, double beta)
           System.Collections.ArrayList moves = board.RightMoves(color);
           if (level == 0 || moves.Count == 0)
               return Eval(board, level);
               if (level == LEVELS + (int)this.Color && (moves.Count == 1))
                   sMove = moves[0] as CheckersMove;
                   return 0;
               double max = (this.Color == color) ? double.MinValue : double.MaxValue;
               for (int i = 0; i < moves.Count; i++)
                   CheckersMove move = moves[i] as CheckersMove;
                   CheckersBoard newBoard = PerformMove(board, move);
                   double eval = SelectMove(newBoard, (PieceColor)
               (((int)color + 1) % 2), level - 1, alpha, beta);
                   if (color == this.Color)
                       if (max < eval)
                           max = eval;
                           if (level == LEVELS + (int)this.Color) sMove = move;
                           if (beta <= max) break; else alpha = Math.Max(alpha, max);
                       else if (max == eval && (level == LEVELS + (int)this.Color) &&
                               (r.Next(9) % 2 == 0))
                           sMove = move;

                       if (max > eval) max = eval;
                       if (alpha >= max) break; else beta = Math.Min(beta, max);
               return max;

The Evaluation Function

The evaluation function used on this min max tree gives importance to how many Queens exist and how close the Pawns are to be promoted. Here is the code for that function:

protected double Eval(CheckersBoard board, int level)
    double myval = 0;
    double enemyval = 0;
    for(int x=0; x<board.Width; x++)
    for(int y=0; y<board.Height; y++)
        CheckersPiece piece=board.GetPieceAt(x, y) as CheckersPiece;
            int factor  = (piece.Color == PieceColor.White)?(7-y):(y);
            if (piece.Color == this.Color) 
                if (piece is Pawn) myval+=100 + (factor*factor);
                    if (y==0) 
                        if (x==0) myval-=40;
                        else myval-=20;
                    else if (y == 7) 
                        if (x==7) myval-=40;
                        else myval-=20;                                                                        
                if (piece is Pawn) enemyval+=100 + (factor*factor);
                    if (y==0) 
                        if (x==0) enemyval-=40;
                        else enemyval-=20;
                    else if (y == 7) 
                        if (x==7) enemyval-=40;
                        else enemyval-=20;                                                                        

    if (enemyval == 0) return 100000 + level*level;
    else if (myval == 0) return -100000 - level*level;
    return (myval - enemyval);

The game offers four levels of intelligence that analyze(2, 4, 6, and 8 levels) perfect values on the PDA. On a desktop computer, you can compute a lot more levels so it will be harder to beat.


Both the BoardEngine and the GamesPackage saved a lot of time when coding turn based games. Although the library needs to be polished and prepared for CF 2.0 and newer (Generics), it works fine and I have coded backgammon and dominoes games using them too.
Hope you like it!


  • 20th June, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Leonardo Paneque
United States United States
Leonardo loves to code with C# on any platform and OS.
He has a Master degree in Computer Sciences and likes to share code and ideas.

You may also be interested in...


Comments and Discussions

Questioncan you help me with that erorr please Pin
Mohammed Khalil AbaNdeh23-Nov-14 5:23
memberMohammed Khalil AbaNdeh23-Nov-14 5:23 
AnswerRe: can you help me with that erorr please Pin
Leonardo Paneque23-Nov-14 13:50
memberLeonardo Paneque23-Nov-14 13:50 
GeneralVGA Pin
joubertvasc25-Jan-09 6:59
memberjoubertvasc25-Jan-09 6:59 
GeneralThanks Pin
Gaj-IT20-Jun-08 23:44
memberGaj-IT20-Jun-08 23:44 
GeneralMissing Code Pin
Steven Bralovich20-Jun-08 2:51
memberSteven Bralovich20-Jun-08 2:51 
GeneralRe: Missing Code Pin
Leonardo Paneque20-Jun-08 3:10
memberLeonardo Paneque20-Jun-08 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 20 Jun 2008
Article Copyright 2008 by Leonardo Paneque
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid