public static bool IsEatMove(CheckersBoard board, BoardPosition[] movePath) { if(board!=null && movePath!=null && movePath.Length>=2) { int x1=movePath[0].X; int y1=movePath[0].Y; //System.Windows.Forms.MessageBox.Show(x1+" "+y1); if(board.IsInsideBoard(x1, y1)) { CheckersPiece piece=board.GetPieceAt(x1, y1) as CheckersPiece; if(piece!=null) { int x2=movePath[1].X; int y2=movePath[1].Y; //System.Windows.Forms.MessageBox.Show(x2+" "+y2); if(board.IsInsideBoard(x2, y2)) { if(Math.Abs(x1-x2)==Math.Abs(y1-y2)) { int incX=Inc(x1, x2); int incY=Inc(y1, y2); //System.Windows.Forms.MessageBox.Show(incX+" "+incY); x1+=incX; y1+=incY; //System.Windows.Forms.MessageBox.Show(x1+" "+y1); while(x1!=x2 || y1!=y2) { CheckersPiece piece2=board.GetPieceAt(x1, y1) as CheckersPiece; if(piece2!=null) { //System.Windows.Forms.MessageBox.Show(x1+" "+y1); //System.Windows.Forms.MessageBox.Show(piece.Color+" - "+piece2.Color); return piece.Color!=piece2.Color; } x1+=incX; y1+=incY; //System.Windows.Forms.MessageBox.Show(x1+" "+y1); } } return false; } else throw new ArgumentException(); } else throw new ArgumentException(); } else throw new ArgumentException(); } else throw new ArgumentException(); }
public object Clone() { CheckersBoard newBoard=new CheckersBoard(); for(int x=0; x<newBoard.Width; x++) for(int y=0; y<newBoard.Height; y++) { CheckersPiece piece=this.GetPieceAt(x, y) as CheckersPiece; if(piece!=null) { CheckersPiece clone=(CheckersPiece)piece.Clone(); newBoard.PutPieceAt(x, y, clone); } } return newBoard; }
public Pawn(CheckersBoard board, int x, int y, PieceColor color) : base(board, x, y, color) { if (Color == PieceColor.White) { XMov = new[] { -1, 1 }; YMov = new[] { -1, -1 }; } else if (color == PieceColor.Black) { XMov = new[] { 1, -1 }; YMov = new[] { 1, 1 }; } }
public BoardDrawer(CheckersBoard board) { this._Board = board; this.SelectionColor = Color.Red; this.SelectionPen = new Pen(SelectionColor); }
private void NewGame() { MyBoard = new CheckersBoard(); MyBoard.InitializeBoard(); _boardDrawer = new BoardDrawer(this.MyBoard); _boardDrawer.SelectionColor = Color.Yellow; //var white = new HumanCheckersPlayer(PieceColor.White); SimpleCheckersPlayer white = new SimpleCheckersPlayer(PieceColor.White, new PawnStrengthBoardEvaluator()); white.MaxSearchDepth = 3; white.OnPerformMove += white_OnPerformMove; //white.OnPlay += white_OnPlay; white.OnOtherPlayerMovePerformed += white_OnOtherPlayerMovePerformed; white.OnInvalidMove += white_OnInvalidMove; white.OnGameOver += white_OnGameOver; var black = new SimpleCheckersPlayer(PieceColor.Black, new SimpleHeuristicBoardEvaluator()); //var black = new HumanCheckersPlayer(PieceColor.Black); black.MaxSearchDepth = 3; black.OnPerformMove += black_OnPerformMove; //black.OnPlay+=black_OnPlay; black.OnOtherPlayerMovePerformed += black_OnOtherPlayerMovePerformed; black.OnInvalidMove += black_OnInvalidMove; black.OnGameOver += black_OnGameOver; Text = white + " | " + black; InitializeBoardDrawing(); StartGame(white, black); }
protected double SelectMove(CheckersBoard board, PieceColor color, int currentSearchDepth, double alpha, double beta) { var moves=board.RightMoves(color); if(currentSearchDepth==0 || moves.Count==0) return _boardEvaluator.Eval(board, currentSearchDepth,Color); else { //if have only one oportunnity the return this one and save time if (currentSearchDepth == MaxSearchDepth && (moves.Count == 1)) { selectedMove = moves[0]; return 0; } double max=(this.Color==color)? double.MinValue: double.MaxValue; for(int i=0;i<moves.Count;i++) { var move = moves[i]; CheckersBoard newBoard=PerformMove(board, move); double eval=SelectMove(newBoard, color == PieceColor.White ? PieceColor.Black : PieceColor.White, currentSearchDepth-1,alpha,beta); if(color==this.Color) { if(max<eval) { max=eval; if(currentSearchDepth==MaxSearchDepth) { selectedMove=move; //System.Windows.Forms.MessageBox.Show("Got a move "+selectedMove + " currentSearchDepth :"+MaxSearchDepth); } if (beta<=max) break; else alpha = Math.Max(alpha,max); } else if(max==eval) { if((currentSearchDepth==MaxSearchDepth) && (r.Next(9)%2==0)) { selectedMove=move; //System.Windows.Forms.MessageBox.Show("Got a move "+selectedMove + " currentSearchDepth :"+MaxSearchDepth); } } } else { if(max>eval) max=eval; if (alpha>=max) break; else beta = Math.Min(beta,max); } //System.Windows.Forms.MessageBox.Show(newBoard.ToString()+'\n'+"Level "+currentSearchDepth+" Color "+color+" Max "+max+" eval "+eval); } return max; } }
protected CheckersBoard PerformMove(CheckersBoard board, CheckersMove move) { CheckersBoard newBoard=(CheckersBoard)board.Clone(); int x1=move.MovePath[0].X; int y1=move.MovePath[0].Y; CheckersPiece movingPiece=(CheckersPiece)newBoard.GetPieceAt(x1, y1); for(int i=0; i<move.MovePath.Length-1; i++) { int x2=move.MovePath[i+1].X; int y2=move.MovePath[i+1].Y; int incX=Inc(x1, x2); int incY=Inc(y1, y2); while(x1!=x2 || y1!=y2) { CheckersPiece piece=newBoard.GetPieceAt(x1, y1) as CheckersPiece; if(piece!=null) { newBoard.RemovePiece(piece); } x1+=incX; y1+=incY; } } //White queen if(y1==0 && movingPiece.Color==PieceColor.White) { newBoard.PutPieceAt(x1, y1, new Queen(newBoard, x1, y1, PieceColor.White)); } else if(y1==7 && movingPiece.Color==PieceColor.Black) //black queen { newBoard.PutPieceAt(x1, y1, new Queen(newBoard, x1, y1, PieceColor.Black)); } else //simply move piece newBoard.PutPieceAt(x1, y1, movingPiece); return newBoard; }
public Queen(CheckersBoard board,int x,int y,PieceColor color) : base(board,x,y,color) { }
public CheckersModerator(CheckersBoard board, CheckersPlayer whiteplayer, CheckersPlayer blackplayer) : base(new IPlayer[2]{whiteplayer,blackplayer}) { this.CurrentGameState = board; }
public CheckersPiece(CheckersBoard board, int x, int y, PieceColor color) : base(board, x, y) { this.Color = color; }