public void CreateQueensideCastle() { var move = new ChessMove { QueensideCastle = true }; Assert.AreEqual("O-O-O", move.ToString()); }
public void PushEPawn() { var move = new ChessMove { StartSquare = ChessBoardSquare.GetAN(5, 2), EndSquare = ChessBoardSquare.GetAN(5, 4), Piece = ChessPiece.WhitePawn }; Assert.AreEqual("Pe2-e4", move.ToString()); }
public void PromotionCaptureCheckmate() { var move = new ChessMove { StartSquare = ChessBoardSquare.GetAN(1, 7), EndSquare = ChessBoardSquare.GetAN(2, 8), WasPieceCaptured = true, PawnPromotedTo = PromotionChessPiece.Queen, IsCheckmateMove = true, Piece = ChessPiece.WhitePawn }; Assert.AreEqual("Pa7xb8=q#", move.ToString()); }
public static void Play() { ChessBoard board; do { Console.Write("Board layout to start with, or enter for default: "); string boardKey; boardKey = Console.ReadLine(); try { if (boardKey.Length > 0) { board = ChessBoard.FromUniqueKey(boardKey); } else { board = ChessBoard.NewGame(); } } catch (Exception ex) { board = null; Console.WriteLine(ex.Message); } } while (board == null); Dictionary <string, List <string> > validMoves = new Dictionary <string, List <string> >(); List <string> targetList = null; board.WriteToConsole(); while (board.GetValidMoves().Any()) { foreach (ChessMove move in board.GetValidMoves()) { if (!validMoves.TryGetValue(move.Source.ToString(), out targetList)) { targetList = new List <string>(); validMoves[move.Source.ToString()] = targetList; } targetList.Add(move.ToString()); } if (!board.GetValidMoves().Any()) { break; } string source; string target; if (board.IsBlacksTurn) { if (board.IsBlackInCheck) { Console.WriteLine("Black is in CHECK."); } } else { if (board.IsWhiteInCheck) { Console.WriteLine("White is in CHECK."); } } Console.WriteLine("Press enter to have the computer play {0}'s turn", board.IsBlacksTurn ? "black" : "white"); do { Console.Write("Move from coordinate ({0}): ", String.Join(",", validMoves.Keys)); source = Console.ReadLine(); } while ((source.Length > 0) && !validMoves.TryGetValue(source, out targetList)); if (source.Length == 0) { firstProgress = true; ChessMove move = Evaluator.GetBestMove(board, 4, Progress); Console.WriteLine(move.ToString()); board = board.Move(move); Console.WriteLine("Board layout code: {0}", board.GetUniqueKey()); board.WriteToConsole(move.Target); } else { do { Console.Write("Move to Coordinate or press Enter to go back ({0}): ", string.Join(",", targetList.Select(a => a.Substring(3)).ToArray())); target = Console.ReadLine(); } while ((target.Length > 0) && !targetList.Contains(source + "-" + target)); if (target.Length == 0) { continue; } board = board.Move(new ChessMove(source + "-" + target)); Console.WriteLine("Board layout code: {0}", board.GetUniqueKey()); board.WriteToConsole(new Coordinate(target)); } validMoves.Clear(); } if (board.IsBlacksTurn) { Console.WriteLine("Checkmate, white wins."); } else { Console.WriteLine("Checkmate, black wins."); } Console.ReadLine(); }
void DoNextMove(ChessPlayer player, ChessPlayer opponent) { ChessMove nextMove = null; //DateTime start = DateTime.Now; bool? isValidMove = false; ChessState newstate = null; if (player.IsComputer) { // Clear out the decision tree _tmpDecisionTree = null; Profiler.BeginTurn(player.Color); nextMove = player.GetNextMove(_mainChessState.CurrentBoard); Profiler.EndTurn(player.TimeOfLastMove); Logger.Log("Time Of " + player.ColorAndName + "'s last move: " + player.TimeOfLastMove); SetDecisionTree(_tmpDecisionTree); if (!this.IsGameRunning) { // if we're no longer running, leave the method return; } if ((nextMove == null) || (!nextMove.IsBasicallyValid)) { IsGameRunning = false; _results = "The framework caught " + player.ColorAndName + " returning a completely invalid move, therefore " + player.ColorAndName + " loses!"; if (nextMove == null) { Logger.Log(player.ColorAndName + " returned a null move object."); } else { Logger.Log(player.ColorAndName + "'s invalid move was: " + nextMove.ToString()); } return; } if (nextMove.Flag == ChessFlag.AIWentOverTime) { // the AI went over it's time limit. IsGameRunning = false; _results = player.ColorAndName + " went over the time limit to move and grace period. Total move time was: " + player.TimeOfLastMove.ToString(); return; } if (nextMove.Flag != ChessFlag.Stalemate) { // The move is not a stale mate, and it's basically valid, // so, let's see if the move is actually valid. newstate = _mainChessState.Clone(); newstate.MakeMove(nextMove); isValidMove = opponent.IsValidMove(newstate.PreviousBoard, newstate.PreviousMove); if (!opponent.IsHuman) { Logger.Log("Time Of " + opponent.ColorAndName + "'s validate move: " + opponent.TimeOfLastMove); } } } else //player is human { this.SetGuiChessBoard_IsLocked(false); nextMove = player.GetNextMove(_mainChessState.CurrentBoard); if (!IsGameRunning) { // if we're no longer running, leave the method return; } newstate = _mainChessState.Clone(); newstate.MakeMove(nextMove); isValidMove = opponent.IsValidMove(newstate.PreviousBoard, newstate.PreviousMove); if (!opponent.IsHuman) { Logger.Log("Time Of " + opponent.ColorAndName + "'s validate move: " + opponent.TimeOfLastMove); } this.SetGuiChessBoard_IsLocked(true); }//end if player == human if ((UpdatedState != null) && (newstate != null)) { // If someone is sub'd to our update delegate, // update them with the new state. UpdatedState(newstate); } if (isValidMove == null) { // the AI went over it's time limit. IsGameRunning = false; _results = opponent.ColorAndName + " went over the time limit to validate a move and grace period. Total move time was: " + opponent.TimeOfLastMove.ToString(); return; } if (isValidMove == true) { _mainChessState = newstate; if (player.Color == ChessColor.Black) { _mainChessState.FullMoves++;//Increment fullmoves after black's turn } //Determine if a pawn was moved or a kill was made. if (ResetHalfMove()) { _mainChessState.HalfMoves = 0; } else { if (_mainChessState.HalfMoves < 50) { _mainChessState.HalfMoves++; } else { //end of game: 50 move rule IsGameRunning = false; _results = "Game is a stalemate. 50 moves were made without a kill or a pawn advancement."; } } if (nextMove.Flag == ChessFlag.Check) { Logger.Log(player.ColorAndName + " has put " + opponent.ColorAndName + " in Check!"); } if (nextMove.Flag == ChessFlag.Checkmate) { // Checkmate on a valid move has been signaled. IsGameRunning = false; _results = player.ColorAndName + " has signaled that the game is a checkmate _and_ " + opponent.ColorAndName + " said the last move was valid."; } } else { // It is either a stalemate or an invalid move. Either way, we're done running. IsGameRunning = false; if (nextMove.Flag == ChessFlag.Stalemate) { // A stalemate has occurred. Since stalemates can occur because the AI can't // make a move, we don't have the other AI check their move (because it would // probably just be an empty move). _results = player.ColorAndName + " has signaled that the game is a stalemate."; } else { _results = opponent.ColorAndName + " has signaled that " + player.ColorAndName + " returned an invalid move!"; Logger.Log(player.ColorAndName + "'s invalid move was: " + nextMove.ToString()); } } }