public static List <Move> lastNMoves(Board board, int N) { List <Move> moveHistory = new List <Move>(); Move currentMove = board.getTransitionMove(); int i = 0; while (currentMove != MoveFactory.getNullMove() && i < N) { moveHistory.Add(currentMove); currentMove = currentMove.getBoard().getTransitionMove(); i++; } return(moveHistory); }
/// <summary> /// execute the best move based on minimax algorithm /// </summary> /// <param name="board"></param> /// <param name="depth"></param> /// <returns></returns> public Move execute(Board board) { long startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(); Move bestMove = MoveFactory.getNullMove(); int largestSeenValue = Int32.MinValue; int lowestSeenValue = Int32.MaxValue; int currentValue; message = String.Concat(board.getCurrentPlayer() + " Thinking with depth = " + searchDepth); int numMoves = board.getCurrentPlayer().getLegalMoves().Count; List <Move> sortedMoves = MoveSorter.EXPENSIVE.Sort(board.getCurrentPlayer().getLegalMoves().ToList()).ToList(); foreach (Move move in sortedMoves) { MoveTransition moveTransition = board.getCurrentPlayer().makeMove(move); this.quiescenceCount = 0; if (moveTransition.getMoveStatus().isDone()) { currentValue = board.getCurrentPlayer().getAlliance().isWhite() ? min(moveTransition.getBoard(), searchDepth - 1, largestSeenValue, lowestSeenValue) : max(moveTransition.getBoard(), searchDepth - 1, largestSeenValue, lowestSeenValue); if (board.getCurrentPlayer().getAlliance().isWhite() && currentValue > largestSeenValue) { largestSeenValue = currentValue; bestMove = move; } else if (board.getCurrentPlayer().getAlliance().isBlack() && currentValue < lowestSeenValue) { lowestSeenValue = currentValue; bestMove = move; if (moveTransition.getBoard().getWhitePlayer().isCheckMate()) { break; } } } } long executionTime = DateTimeOffset.Now.ToUnixTimeMilliseconds() - startTime; return(bestMove); }
/// <summary> /// execute the best move based on minimax algorithm /// </summary> /// <param name="board"></param> /// <param name="depth"></param> /// <returns></returns> public Move execute(Board board) { long startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(); Move bestMove = MoveFactory.getNullMove(); int largestSeenValue = Int32.MinValue; int lowestSeenValue = Int32.MaxValue; int currentValue; message = String.Concat(board.getCurrentPlayer() + " Thinking with depth = " + searchDepth); int numMoves = board.getCurrentPlayer().getLegalMoves().Count; foreach (Move move in board.getCurrentPlayer().getLegalMoves()) { MoveTransition moveTransition = board.getCurrentPlayer().makeMove(move); if (moveTransition.getMoveStatus().isDone()) { currentValue = board.getCurrentPlayer().getAlliance().isWhite() ? min(moveTransition.getBoard(), searchDepth - 1) : max(moveTransition.getBoard(), searchDepth - 1); if (board.getCurrentPlayer().getAlliance().isWhite() && currentValue >= largestSeenValue) { largestSeenValue = currentValue; bestMove = move; } else if (board.getCurrentPlayer().getAlliance().isBlack()) { lowestSeenValue = currentValue; bestMove = move; } } } long executionTime = DateTimeOffset.Now.ToUnixTimeMilliseconds() - startTime; return(bestMove); }
private Board(Builder builder) { //Create new gameboard with builder this.gameBoard = createGameBoard(builder); //white and black active pieces this.whitePieces = calculateActivePieces(this.gameBoard, Alliance.WHITE); this.blackPieces = calculateActivePieces(this.gameBoard, Alliance.BLACK); this.enPassantPawn = builder.getEnPassantPawn(); //white and black initial legal moves calculation IReadOnlyCollection <Move> whiteStandardLegalMoves = calculateLegalMoves(this.whitePieces); IReadOnlyCollection <Move> blackStandardLegalMoves = calculateLegalMoves(this.blackPieces); //Player instantiation this.whitePlayer = new WhitePlayer(this, whiteStandardLegalMoves, blackStandardLegalMoves); this.blackPlayer = new BlackPlayer(this, whiteStandardLegalMoves, blackStandardLegalMoves); this.currentPlayer = builder.getNextMoveMaker().choosePlayer(this.whitePlayer, this.blackPlayer); this.transitionMove = builder.getMoveTransition() != null?builder.getMoveTransition() : MoveFactory.getNullMove(); }