public Move GetMove(Int32 startSquare, Int32 endSquare, Player player) { List<Move> moves = engine.FindMoves(board, player.Color.ToString()); for (var i = 0; i < moves.Count; i++) { var move = moves[i]; if (move.StartSquare == (Byte)startSquare && move.EndSquare == (Byte)endSquare) { // same piece must jump rule if (player.Type == Player.PlayerType.HUMAN) { if (move.HasChildMoves) mustJumpFromSquare = move.EndSquare; else mustJumpFromSquare = 0; } return move; } } return null; }
public Boolean IsMoveLegal(Int32 startSquare, Int32 endSquare, Player player) { List<Move> moves = engine.FindMoves(board, player.Color.ToString()); for (var i = 0; i < moves.Count; i++) { var move = moves[i]; // force same piece jumping if (player.Type == Player.PlayerType.HUMAN) if (mustJumpFromSquare > 0 && move.StartSquare != mustJumpFromSquare) continue; if (move.StartSquare == (Byte)startSquare && move.EndSquare == (Byte)endSquare) return true; } return false; }
private void ChangeCurrentPlayer() { if (currentPlayer == redPlayer) currentPlayer = whitePlayer; else currentPlayer = redPlayer; }
private void StartNewGame() { board = new Byte[] { 0,2,0,2,0,2,0,2, 2,0,2,0,2,0,2,0, 0,2,0,2,0,2,0,2, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 1,0,1,0,1,0,1,0, 0,1,0,1,0,1,0,1, 1,0,1,0,1,0,1,0}; ai = new AI(); engine = new MoveEngine(board); redPlayer = new Player(Player.PlayerColor.RED, Player.PlayerType.COMPUTER); whitePlayer = new Player(Player.PlayerColor.WHITE, Player.PlayerType.HUMAN); currentPlayer = whitePlayer; gameOver = false; pbTurnSignal.BackColor = Color.Green; timer = new Timer(); timer.Interval = 200; timer.Tick += timer_Tick; lblScore.Text = "0"; try { whiteChecker = Image.FromFile(@"images/whiteChecker.png"); redChecker = Image.FromFile(@"images/redChecker.png"); } catch (System.Exception e) { MessageBox.Show("Failed to load King checker images", "Error"); gameOver = true; } }
private List<Move> GetMoves(Player.PlayerColor playerColor) { PrepareBitBoards(); var moves = new List<Move>(); for (Byte index = 0; index < _board.Length; index++) { Player.PlayerColor currentSquareCheckerColor = Player.GetCheckerColor((Piece)_board[index]); if (currentSquareCheckerColor == playerColor) { switch ((Piece)_board[index]) { case Piece.WhiteKing: case Piece.RedKing: if (CanMoveNW(index)) { Byte endSquare = GetSquareFromBitboard(whiteAttackSquares[index][0]); var move = new Move(index, endSquare, false) { WillKing = IsKingingSquare(endSquare) }; moves.Add(move); } if (CanMoveNE(index)) { Byte endSquare = GetSquareFromBitboard(whiteAttackSquares[index][1]); var move = new Move(index, endSquare, false) { WillKing = IsKingingSquare(endSquare) }; moves.Add(move); } if (CanMoveSW(index)) { Byte endSquare = GetSquareFromBitboard(redAttackSquares[index][0]); var move = new Move(index, endSquare, false) { WillKing = IsKingingSquare(endSquare) }; moves.Add(move); } if (CanMoveSE(index)) { Byte endSquare = GetSquareFromBitboard(redAttackSquares[index][1]); var move = new Move(index, endSquare, false) { WillKing = IsKingingSquare(endSquare) }; moves.Add(move); } break; case Piece.WhiteChecker: if (CanMoveNW(index)) { Byte endSquare = GetSquareFromBitboard(whiteAttackSquares[index][0]); var move = new Move(index, endSquare, false) { WillKing = IsKingingSquare(endSquare) }; moves.Add(move); } if (CanMoveNE(index)) { Byte endSquare = GetSquareFromBitboard(whiteAttackSquares[index][1]); var move = new Move(index, endSquare, false) { WillKing = IsKingingSquare(endSquare) }; moves.Add(move); } break; case Piece.RedChecker: if (CanMoveSW(index)) { Byte endSquare = GetSquareFromBitboard(redAttackSquares[index][0]); var move = new Move(index, endSquare, false) { WillKing = IsKingingSquare(endSquare) }; moves.Add(move); } if (CanMoveSE(index)) { Byte endSquare = GetSquareFromBitboard(redAttackSquares[index][1]); var move = new Move(index, endSquare, false) { WillKing = IsKingingSquare(endSquare) }; moves.Add(move); } break; } } } return moves; }
public Move GetMove(Byte[] board, Player player) { _board = board; var savedPlayerColor = player.Color; var move = (mustJumpFromSquare == 0) ? DoAI(player) : GetJumpFromChoices(player); player.SetColor(savedPlayerColor); return move; }
private List<Move> GetPlayerLegalMoves(Player player) { String playerColor = player.Color.ToString().ToLower(); List<Move> moves = _engine.FindMoves(_board, playerColor); return moves; }
private Int32 negamax(List<Move> moves, Int32 plies, Player player) { if (plies == 0 || moves.Count == 0) return EvaluateBoard(null); Int32 best = Int32.MinValue; Int32 current; foreach (var m in moves) { _history.Push((Byte[])_board.Clone()); MakeMove(m); ChangeSides(player); var nextMoves = GetPlayerLegalMoves(player); current = -(negamax(nextMoves, plies - 1, player)); ChangeSides(player); if (current > best) best = current; _board = _history.Pop(); } return best; }
private Move GetJumpFromChoices(Player player) { Move move = null; if (jumpChoices.Count() == 1) { move = jumpChoices[0]; DoMustJumpLogic(move); } else if (jumpChoices.Count() > 1) { move = DoAI(player, jumpChoices); } return move; }
private Move DoAI(Player player, List<Move> movePool) { List<Move> potentialMoves = (movePool == null) ? GetPlayerLegalMoves(player) : movePool; // no moves, game's over if (potentialMoves.Count == 0) return null; // one move, must take it if (potentialMoves.Count == 1) { DoMustJumpLogic(potentialMoves[0]); return potentialMoves[0]; } Move bestMove = potentialMoves[0]; //_evaluated = 0; // Statistics only for (var i = 0; i < potentialMoves.Count(); i++) { _history.Push((Byte[])_board.Clone()); MakeMove(potentialMoves[i]); player.SetColor((player.Color == Player.PlayerColor.RED) ? Player.PlayerColor.WHITE : Player.PlayerColor.RED); var moves = GetPlayerLegalMoves(player); //potentialMoves[i].SetScore(negamax(moves, 6, player)); //potentialMoves[i].SetScore(minimax(moves, 6, player)); potentialMoves[i].SetScore(alphabeta(moves, 6, player, Int32.MinValue, Int32.MaxValue)); player.SetColor((player.Color == Player.PlayerColor.RED) ? Player.PlayerColor.WHITE : Player.PlayerColor.RED); if (potentialMoves[i].Score > bestMove.Score) bestMove = potentialMoves[i]; _board = (Byte[])_history.Pop(); } if (bestMove != null) DoMustJumpLogic(bestMove); score = bestMove.Score; return bestMove; }
private Move DoAI(Player player) { return DoAI(player, null); }
private Int32 alphabeta(List<Move> moves, Int32 plies, Player player, Int32 alpha, Int32 beta) { if (moves.Count == 0) { if (player.Color == Player.PlayerColor.WHITE) return Int32.MaxValue; else return Int32.MinValue; } if (plies == 0) return EvaluateBoard(null); Int32 best = Int32.MinValue; Int32 current; Int32 localAlpha = alpha; foreach (var m in moves) { _history.Push((Byte[])_board.Clone()); MakeMove(m); player.SetColor((player.Color == Player.PlayerColor.RED) ? Player.PlayerColor.WHITE : Player.PlayerColor.RED); var nextMoves = GetPlayerLegalMoves(player); current = -(alphabeta(nextMoves, plies - 1, player, -beta, -localAlpha)); player.SetColor((player.Color == Player.PlayerColor.RED) ? Player.PlayerColor.WHITE : Player.PlayerColor.RED); _board = _history.Pop(); if (current > best) best = current; if (best >= beta) break; if (best > localAlpha) localAlpha = best; } return best; }
private static void ChangeSides(Player player) { player.SetColor((player.Color == Player.PlayerColor.RED) ? Player.PlayerColor.WHITE : Player.PlayerColor.RED); }