Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Checkers for Pocket PC using a Recursive Min-Max Tree

, 19 Jun 2008
Checkers/Draughts game for Pocket PC, using the recursive Min-Max tree to calculate moves, coded with C# over the .NET Compact Framework
checkers20_cab.zip
GamesPackage
GamesPackage.csdproj
GamesPackage.csdproj.user
GamesPackage.csproj.user
GamesPackage.resharperoptions
GamesPackage.suo
SoundPPCLibrary
SoundPPCLibrary.csdproj
SoundPPCLibrary.csdproj.user
SoundPPCLibrary.csproj.user
SoundPPCLibrary.resharperoptions
SoundPPCLibrary.suo
BoardEngine
BoardEngine.csdproj
BoardEngine.csdproj.user
BoardEngine.csproj.user
Checkers
001_select_checker_0.wav
002_select_destination_0.wav
003_player_move_0.wav
004_eat_a_player_0.wav
005_end_of_the_board_0.wav
006_game_over_0.wav
007_win_game_0.wav
CHECKER.ico
Checkers.csdproj
Checkers.csdproj.user
Checkers.csproj.user
Checkers.resharper.user
Checkers.resharperoptions
Checkers.suo
checkersico.ico
Thumbs.db
checkers_source.zip
Checkers.csdproj
Checkers.csdproj.user
Checkers.csproj.user
checkersico.ico
001_select_checker_0.wav
002_select_destination_0.wav
003_player_move_0.wav
004_eat_a_player_0.wav
005_end_of_the_board_0.wav
006_game_over_0.wav
007_win_game_0.wav
CHECKER.ico
using System;
using GamesPackage;
using BoardEngine;
using System.Collections;

namespace Checkers
{
	/// <summary>
	/// Summary description for CheckersModerator.
	/// </summary>
	public class CheckersModerator : Moderator
	{

		public CheckersModerator(CheckersBoard board, CheckersPlayer whiteplayer, CheckersPlayer blackplayer) : base(new IPlayer[2]{whiteplayer,blackplayer})
		{
			this.CurrentGameState = board;
			//
			// TODO: Add constructor logic here
			//
		}

		protected override void InvokePlayMethod(IPlayer player)
		{
			try 
			{
				player.Play((IGameStateForPlayer)this.CurrentGameState);
			}
			catch(Exception ex) 
			{
				HaltGame(ex);
			}

		}

		public override bool isWinner(IPlayer player)
		{
			if (!isGameFinished()) return false;
			CheckersPlayer myplayer = (CheckersPlayer)player;
			CheckersBoard MyBoard = (CheckersBoard)CurrentGameState;
			return (MyBoard.RightMoves(myplayer.Color).Count>0);
		}

		private int GetCloser(int k, int goal) 
		{
			if (k<goal) return k+1;
			else if (k>goal) return k-1;
			else return k;
		}

		protected override void PerformMove(IGameState state, IMove move)
		{
			CheckersBoard MyBoard = (CheckersBoard)state;
			CheckersMove MyMove = (CheckersMove)move;
			CheckersPiece piece = (CheckersPiece)MyBoard.GetPieceAt((BoardPosition)MyMove.MovePath[0]);
			if (piece!=null) 
			{
				MyBoard.RemovePiece(piece.X,piece.Y);
				
				//Clear all piece in the middle of the path (assuming that this method is called when the move is valid.
				for(int i=0;i<MyMove.MovePath.Length - 1; i++) 
				{
					//get the current position
                    BoardPosition current = (BoardPosition)MyMove.MovePath[i];
					BoardPosition next =  (BoardPosition)MyMove.MovePath[i+1];
					
					int cx = current.X;
					int cy = current.Y;
					
					//move close to reach the next position
					while(!(cx==next.X && cy == next.Y)) 
					{
						cx = GetCloser(cx,next.X);
						cy = GetCloser(cy,next.Y);
						bool removed = MyBoard.RemovePiece(cx,cy);
						if (removed && (piece.Color == PieceColor.Black)) white--;
						else if (removed && (piece.Color == PieceColor.White)) black--;

					}
				}

				BoardPosition pos=MyMove.MovePath[MyMove.MovePath.Length-1];

				if(pos.Y==0 && piece.Color==PieceColor.White)
				{
					//System.Windows.Forms.MessageBox.Show("Queen");
					MyBoard.PutPieceAt(pos, new Queen(MyBoard, pos.X, pos.Y, PieceColor.White));
				}
				else
					if(pos.Y==7 && piece.Color==PieceColor.Black)
				{
					//System.Windows.Forms.MessageBox.Show("Queen");
					MyBoard.PutPieceAt(pos, new Queen(MyBoard, pos.X, pos.Y, PieceColor.Black));
				}
				else
					MyBoard.PutPieceAt((BoardPosition)MyMove.MovePath[MyMove.MovePath.Length-1],piece);

			}
		}

		public override bool isValidMove(IPlayer player, IMove move)
		{
			CheckersBoard MyBoard = (CheckersBoard)this.CurrentGameState;
			CheckersMove MyMove = (CheckersMove)move;
			CheckersPlayer MyPlayer = (CheckersPlayer)player;
			CheckersPiece piece = (CheckersPiece)MyBoard.GetPieceAt((BoardPosition)MyMove.MovePath[0]);

			if (piece==null) return false;
			if (piece.Color != MyPlayer.Color) return false;
			
			int max=0;
			bool eatMove=false;

//			ArrayList tmp = (ArrayList)MyBoard.WhitePiecesList.Clone();
//			if (MyPlayer.Color == PieceColor.Black) tmp = (ArrayList)MyBoard.BlackPiecesList.Clone();

//			for(int i=0;i<tmp.Count;i++ ) {
//				CheckersPiece aux = (CheckersPiece)tmp[i];
//				System.Collections.ArrayList list=aux.PossibleMoves;
//				if(list.Count>0) {
//					CheckersMove move2=list[0] as CheckersMove;
//					if(max < move2.MovePath.Length) {
//						max=move2.MovePath.Length;
//						System.Windows.Forms.MessageBox.Show("Max="+max);
//					}
//					if(move2.EatMove)
//						eatMove=true;
//				}
//			}
			

			for(int x=0; x<MyBoard.Width; x++)
				for(int y=0; y<MyBoard.Height; y++) {
					CheckersPiece aux=MyBoard.GetPieceAt(x, y) as CheckersPiece;
					if(aux!=null && aux.Color==piece.Color) {
						System.Collections.ArrayList list=aux.PossibleMoves;
						if(list.Count>0) {
							CheckersMove move2=list[0] as CheckersMove;
							if(max < move2.MovePath.Length) {
								max=move2.MovePath.Length;
								//System.Windows.Forms.MessageBox.Show("Max="+max);
							}
							if(move2.EatMove)
								eatMove=true;
						}
					}

				}

			//System.Windows.Forms.MessageBox.Show("Must eat: "+eatMove+" It eat:"+MyMove.EatMove);
			//System.Windows.Forms.MessageBox.Show("Max="+max+" move="+MyMove.MovePath.Length);
			bool canMakeIt=Contains(piece.PossibleMoves, MyMove);//bool canMakeIt=piece.CanMakeMove(MyMove);
			//System.Windows.Forms.MessageBox.Show("Can Make It: "+canMakeIt);
			return eatMove==MyMove.EatMove && MyMove.MovePath.Length==max && canMakeIt;
		}

		protected bool Contains(System.Collections.ArrayList list, CheckersMove move)
		{
			//System.Windows.Forms.MessageBox.Show(list.Count+"");
			foreach(CheckersMove m in list)
			{
				//System.Windows.Forms.MessageBox.Show(m.ToString()+'\n'+move.ToString());
				if(m.Equals(move))
					return true;
			}
			return false;
		}
		
		int white = 12;
		int black = 12;

		public override bool isGameFinished()
		{
			CheckersBoard board = (CheckersBoard)_CurrentGameState;
			CheckersPlayer player=(CheckersPlayer)this.CurrentPlayer;
			//return (white == 0 || black == 0);
			return board.RightMoves(player.Color).Count==0;
		
		}




	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Leonardo Paneque
Team Leader
United States United States
Leonardo loves to code with C# and thinks .NET platforms rocks.
He has a Master degree in Computer Sciences and likes to share code and ideas.
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 20 Jun 2008
Article Copyright 2008 by Leonardo Paneque
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid