public CheckersWindow() { this.InitializeComponent(); board = new CheckersBoard(0, x); board.GameMaxLength = 150; lastMove = new CheckersMove(null, false, 1); moveList.Tag = new List<CheckersMove>(); myRepaint = new Repaint(this.repaint); CheckersPlayer player = new CheckersPlayer(ComputerPlayer); alg = new Algorithms(player); int blackWhite = 1; for (int row = 0; row < 8; ++row) { for (int col = 0; col < 8; ++col) { if (blackWhite % 2 == 0) { BlackSquare square = new BlackSquare(); square.SetValue(Grid.ColumnProperty, col); square.SetValue(Grid.RowProperty, row); square.Tag = new System.Windows.Point(row, col); square.DragEnter += new DragEventHandler(square_DragEnter); square.Drop += new DragEventHandler(square_Drop); square.MouseLeftButtonDown += new MouseButtonEventHandler(square_MouseLeftButtonDown); UIBoard.Children.Add(square); blackSquares.Add(square); } else { WhiteSquare square = new WhiteSquare(); square.SetValue(Grid.ColumnProperty, col); square.SetValue(Grid.RowProperty, row); UIBoard.Children.Add(square); } blackWhite++; } blackWhite++; } StringBuilder stringBuilder = new StringBuilder("Current Player is:\n"); stringBuilder.Append(board.GetCurrentPlayer().GetPlayerID().ToString() == "-1" ? "Black" : "White"); currentPlayerLabel.Content = stringBuilder.ToString(); repaint(null); board.GameOver += new CheckersBoard.EndGame(board_GameOver); if (board.GetCurrentPlayer().GetPlayerID() == alg.ComputerPlayerID) { computerThread = new Thread(this.computerMove); computerThread.Start(); } this.visualGameOver = new EndGame(this.theGameIsOver); }
public IMove EvaluatePosition() { CheckersMove move = new CheckersMove(null, false, this.currentPlayer.GetPlayerID()); if (this.gameOver) { if (currentPlayer.GetPlayerID() == Winner) { move.SetMoveScore(double.MaxValue ); } else { move.SetMoveScore(double.MinValue); } return move as IMove; } double finalScore = 0; if (currentPlayer.GetPlayerID() == 1) { finalScore = this.whitePieceCount * this.weights[0] + this.whiteKingcount * this.weights[1] - this.blackKingCount * this.weights[1] - this.blackPieceCount * this.weights[0]; finalScore += (whiteSidePieces - blackSidePieces) * this.weights[2]; if (whitePieceCount > 0) finalScore += (-whiteKingBlocked) * weights[3]; if (blackPieceCount > 0) finalScore += (blackKingBlocked) * weights[3]; finalScore += (-whiteDogHole) * weights[4]; finalScore += (blackDogHole) * weights[4]; finalScore += this.legalMoves.Count * weights[5]; finalScore += weights[6]; finalScore += -trappedWhiteKing * weights[7]; finalScore += trappedBlackKing * weights[7]; finalScore += -whiteDustHole * weights[8]; finalScore += blackDustHole * weights[8]; finalScore += whiteKingCentered * weights[9]; finalScore += -blackKingCentered * weights[9]; finalScore += whitePieceCentered * weights[10]; finalScore += -blackPieceCentered * weights[10]; move.SetMoveScore(finalScore); } else { finalScore = -this.whitePieceCount * this.weights[0] - this.whiteKingcount * this.weights[1] + this.blackKingCount * this.weights[1] + this.blackPieceCount * this.weights[0]; finalScore += (blackSidePieces - whiteSidePieces) * this.weights[2]; if (whitePieceCount > 0) finalScore += (whiteKingBlocked) * weights[3]; if (blackPieceCount > 0) finalScore += (-blackKingBlocked) * weights[3]; finalScore += (whiteDogHole) * weights[4]; finalScore += (-blackDogHole) * weights[4]; finalScore += this.legalMoves.Count * weights[5]; finalScore += weights[6]; finalScore += trappedWhiteKing * weights[7]; finalScore += -trappedBlackKing * weights[7]; finalScore += whiteDustHole * weights[8]; finalScore += -blackDustHole * weights[8]; finalScore += -whiteKingCentered * weights[9]; finalScore += blackKingCentered * weights[9]; finalScore += -whitePieceCentered * weights[10]; finalScore += blackPieceCentered * weights[10]; move.SetMoveScore(finalScore); } return move as IMove; }
private void computerMove() { CheckersMove move = null; switch(this.AIID) { case 'H': move = alg.ABNegamax(board, 8, 0, double.MinValue, double.MaxValue) as CheckersMove; break; case 'E': move = alg.ABNegamax(board, 2, 0, double.MinValue, double.MaxValue) as CheckersMove; break; case 'P': move = alg.POSM(board, 0.5d) as CheckersMove; break; } if (move == null) throw new ArgumentException("The move has returned null!"); this.lastComputerMove = move; this.gameHistory.Add(move); board.MakeMove(move); }
void square_Drop(object sender, DragEventArgs e) { BlackSquare square = sender as BlackSquare; if (currentItemContainer != null) { if (square.Opacity != 1) { currentItemContainer.Children.Clear(); currentItemContainer = null; square.backGround.Children.Add((Ellipse)e.Data.GetData(typeof(Ellipse))); parentSquare.Opacity = 1; parentSquare = null; System.Windows.Point squareCoords = (System.Windows.Point)square.Tag; if (isInMove == false) { isInMove = true; } List<CheckersMove> newLegalMoves = new List<CheckersMove>(); foreach (var item in movesForCurrentPiece) { CheckersGame.Point moveCoords = item.GetPathAt(moveIndex); if (moveCoords != null && moveCoords.IsEqual((int)squareCoords.X, (int)squareCoords.Y)) { newLegalMoves.Add(item); } } movesForCurrentPiece = newLegalMoves; ++moveIndex; if (movesForCurrentPiece.Count == 1)// && moveIndex == movesForCurrentPiece[0].GetPath().Count) { if (SearchDepth == 8) { lastPlayerMove = movesForCurrentPiece[0]; evaluator = new CheckersBoard(board, false); } board.MakeMove(movesForCurrentPiece[0]); repaint(movesForCurrentPiece[0]); if (!board.GameIsOver()) { computerThread = new Thread(this.computerMove); computerThread.Start(); } isInMove = false; } } //else // MessageBox.Show("Illegal move!", "Illegal", MessageBoxButton.OK, MessageBoxImage.Exclamation); } foreach (var elem in blackSquares) { elem.Opacity = 1.0; foreach (var item in elem.backGround.Children) if (item is Ellipse) { Ellipse elli = item as Ellipse; int row = (int)elem.GetValue(Grid.RowProperty); int col = (int)elem.GetValue(Grid.ColumnProperty); elli.Tag = new System.Windows.Point(row, col); } } }
private void repaint(CheckersMove move) { if (move != null) { StringBuilder builder = new StringBuilder(move.PlayerID.ToString() == "1" ? "White: " : "Black: "); foreach (var item in move.GetPath()) { builder.Append(Convert.ToChar(65 + item.Y)); builder.Append((item.X + 1).ToString()); builder.Append('-'); } //builder.Remove(builder.Length - 1, 1); builder.Append(move.Duration.TotalSeconds.ToString()); moveList.Items.Add(builder.ToString()); List<CheckersMove> list = moveList.Tag as List<CheckersMove>; list.Add(move); moveList.ScrollIntoView(moveList.Items[moveList.Items.Count - 1]); lastMove = move; } if (!board.GameIsOver()) winnerLabel.Visibility = Visibility.Hidden; StringBuilder stringBuilder = new StringBuilder("Current Player is:\n"); stringBuilder.Append(board.GetCurrentPlayer().GetPlayerID().ToString() == "-1" ? "Black" : "White"); currentPlayerLabel.Content = stringBuilder.ToString(); whitePieces.Content = "White Pieces : " + board.WhitePieces.ToString(); blackPieces.Content = "Black Pieces : " + board.BlackPieces.ToString(); foreach (var item in this.blackSquares) { BlackSquare square = item as BlackSquare; int row = (int)square.GetValue(Grid.RowProperty); int col = (int)square.GetValue(Grid.ColumnProperty); int pieceType = board.GetPiece(row, col); switch (pieceType) { case 0: square.backGround.Children.Clear(); break; case 1: square.backGround.Children.Clear(); square.backGround.Children.Add(ellipseBuilder(row, col, pieceType)); break; case -1: square.backGround.Children.Clear(); square.backGround.Children.Add(ellipseBuilder(row, col, pieceType)); break; case 2: square.backGround.Children.Clear(); square.backGround.Children.Add(ellipseBuilder(row, col, pieceType)); break; case -2: square.backGround.Children.Clear(); square.backGround.Children.Add(ellipseBuilder(row, col, pieceType)); break; } } }