public void BasicVerificationTests() { // Tuple tops out at 7 items so we're pushing it // <startSquare, endSquare, Deployed, Color, Promotion Class, ExpectedToString> Tuple <BoardSquare, BoardSquare, bool, PieceColor, PieceClass, string>[] testData = { // PieceClass.EnPassantTarget is invalid, so use when not promoting new Tuple <BoardSquare, BoardSquare, bool, PieceColor, PieceClass, string> (new BoardSquare(new PieceFile(5), 2), new BoardSquare(new PieceFile(5), 4), false, PieceColor.White, PieceClass.EnPassantTarget, "e2e4"), new Tuple <BoardSquare, BoardSquare, bool, PieceColor, PieceClass, string> (new BoardSquare(new PieceFile(4), 4), new BoardSquare(new PieceFile(4), 5), false, PieceColor.Black, PieceClass.EnPassantTarget, "d4d5"), new Tuple <BoardSquare, BoardSquare, bool, PieceColor, PieceClass, string> (new BoardSquare(new PieceFile(5), 7), new BoardSquare(new PieceFile(5), 8), true, PieceColor.White, PieceClass.Queen, "e7e8q"), }; // Verify each entry in the testData array foreach (Tuple <BoardSquare, BoardSquare, bool, PieceColor, PieceClass, string> tuple in testData) { Trace.WriteLine(String.Format("Constucting MoveInformation[{0}:{1}->{2}:{3}] Color: {4} Deployed: {5} Promotion: {6}", tuple.Item1.File.ToString(), tuple.Item1.Rank, tuple.Item2.File.ToString(), tuple.Item2.Rank, tuple.Item4.ToString(), tuple.Item3.ToString(), tuple.Item5.ToString())); MoveInformation testMove = new MoveInformation(tuple.Item1, tuple.Item2, tuple.Item3, ChessBoard.InitialFENPosition); testMove.Color = tuple.Item4; if (tuple.Item5 != PieceClass.EnPassantTarget) { testMove.PromotionJob = tuple.Item5; } Trace.WriteLine(String.Format("Verifying ToString() == \"{0}\"", tuple.Item6)); Assert.AreEqual(0, String.Compare(testMove.ToString(), tuple.Item6)); } }
private async Task <MoveInformation?> PrepareMove(SourceFolder sourceFolder, SystemFile systemFile) { var fileInfo = systemFile.File; if (!_fileService.IsFileReady(fileInfo)) { _logger.LogWarning("File is not ready or was removed"); return(null); } var moveInformation = new MoveInformation(systemFile); moveInformation.MoveProblem = await FindProblems(sourceFolder, systemFile); if (sourceFolder.ShouldAddTagFromFilename) { moveInformation.SourceTags.AddRange(_sourceTagsProvider.GetTagsFromName(fileInfo)); } if (sourceFolder.ShouldCreateTagsFromSubfolders) { moveInformation.SourceTags.AddRange(_sourceTagsProvider.GetTagsFromPath(sourceFolder, fileInfo)); } return(moveInformation); }
public void BasicTests() { // RevertLastMove() does just what it says. To test it we can apply // a move, then revert it and verify the board state ChessBoard testBoard = new ChessBoard(); testBoard.NewGame(); // Basic opening pawn move "e2e4" Trace.WriteLine("Applying a basic pawn move...e2e4"); MoveInformation moveInfo = new MoveInformation( new BoardSquare(E_FILE, 2), new BoardSquare(E_FILE, 4), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); Trace.WriteLine("Verify pawn is found at new location..."); ChessPiece piece = testBoard.FindPieceAt(E_FILE, 4); Assert.AreEqual(piece.Job, PieceClass.Pawn); Assert.AreEqual(piece.Color, PieceColor.White); // Now revert it Trace.WriteLine("Reverting the move..."); testBoard.RevertLastMove(); Trace.WriteLine("Verify pawn is not at e4"); piece = testBoard.FindPieceAt(E_FILE, 4); Assert.IsNull(piece); Trace.WriteLine("Verify pawn is back at e2, and not deployed"); piece = testBoard.FindPieceAt(E_FILE, 2); Assert.AreEqual(piece.Job, PieceClass.Pawn); Assert.IsFalse(piece.Deployed); }
public void BasicTests() { // r4rk1/4bpp1/pqbppnPp/1p6/4P3/P1N1B3/1PPNB1PP/R3QRK1 b - - 0 17 ChessBoard testBoard = new ChessBoard(); testBoard.NewPosition("r4rk1/4bpp1/pqbppnPp/1p6/4P3/P1N1B3/1PPNB1PP/R3QRK1 b - - 0 17"); // Black queen takes white bishop MoveInformation moveInfo = new MoveInformation( new BoardSquare(B_FILE, 6), new BoardSquare(E_FILE, 3), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify Properties Trace.WriteLine("Verifying LastMoveWasCapture is true..."); Assert.IsTrue(testBoard.LastMoveWasCapture); Trace.WriteLine("Verifying move counts..."); Assert.AreEqual(testBoard.HalfMoveCount, 0); Assert.AreEqual(testBoard.FullMoveCount, 18); Trace.WriteLine("Verifying castling rights..."); Assert.AreEqual(testBoard.WhiteCastlingRights, BoardSide.None); Assert.AreEqual(testBoard.BlackCastlingRights, BoardSide.None); Trace.WriteLine("Verifying orientation..."); Assert.AreEqual(testBoard.Orientation, BoardOrientation.WhiteOnBottom); Assert.AreEqual(testBoard.BlackCastlingRights, BoardSide.None); }
private static void AddMd5Path(MoveInformation moveInformation, List <string> newPathParts) { var md5Sub = moveInformation.SystemFile.Md5.Substring(0, 2); var md5SubSub = moveInformation.SystemFile.Md5.Substring(2, 2); newPathParts.Add(md5Sub); newPathParts.Add(md5SubSub); }
public MovedInformation( MoveInformation moveInformation, bool requireSave, FileInfo movedFileInfo) : base(moveInformation.SystemFile) { RequireSave = requireSave; MovedFileInfo = movedFileInfo; MoveProblem = moveInformation.MoveProblem; SourceTags.AddRange(moveInformation.SourceTags); }
public void BasicTests() { // PromotePiece does some verification, it must be a pawn and it must // be on a back rank // r2k2r1/p2n1P2/b3q2p/2p1p3/Pb1P4/R1N5/1pQ2PPP/3R2K1 w - - 0 22 // +---+---+---+---+---+---+---+---+ // | r | | | k | | | r | | // +---+---+---+---+---+---+---+---+ // | p | | | n | | P | | | // +---+---+---+---+---+---+---+---+ // | b | | | | q | | | p | // +---+---+---+---+---+---+---+---+ // | | | p | | p | | | | // +---+---+---+---+---+---+---+---+ // | P | b | | P | | | | | // +---+---+---+---+---+---+---+---+ // | R | | N | | | | | | // +---+---+---+---+---+---+---+---+ // | | p | Q | | | P | P | P | // +---+---+---+---+---+---+---+---+ // | | | | R | | | K | | // +---+---+---+---+---+---+---+---+ ChessBoard testBoard = new ChessBoard(); testBoard.NewPosition("r2k2r1/p2n1P2/b3q2p/2p1p3/Pb1P4/R1N5/1pQ2PPP/3R2K1 w - - 0 22"); // setup the promotion move f7g8n // Don't promote to Queen or Rook as the next move will be illegal with Black // King in check MoveInformation moveInfo = new MoveInformation( new BoardSquare(F_FILE, 7), new BoardSquare(G_FILE, 8), false, testBoard.CurrentFEN); testBoard.PromotePiece(F_FILE, 7, G_FILE, 8, PieceClass.Knight, ref moveInfo); testBoard.MovePiece(ref moveInfo); // Verify the promoted piece ChessPiece piece = testBoard.FindPieceAt(G_FILE, 8); Assert.AreEqual(PieceClass.Knight, piece.Job); Assert.AreEqual(PieceColor.White, piece.Color); // Promote black pawn b2b1q moveInfo = new MoveInformation( new BoardSquare(B_FILE, 2), new BoardSquare(B_FILE, 1), false, testBoard.CurrentFEN); testBoard.PromotePiece(B_FILE, 2, B_FILE, 1, PieceClass.Queen, ref moveInfo); testBoard.MovePiece(ref moveInfo); // Verify the promoted piece piece = testBoard.FindPieceAt(B_FILE, 1); Assert.AreEqual(PieceClass.Queen, piece.Job); Assert.AreEqual(PieceColor.Black, piece.Color); }
public void BasicTests() { // Prepares ..d7d5 for en-passant capture // "r1bqkbnr/pppppppp/n7/4P3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2" // +---+---+---+---+---+---+---+---+ // | r | | b | q | k | b | n | r | // +---+---+---+---+---+---+---+---+ // | p | p | p | p | p | p | p | p | // +---+---+---+---+---+---+---+---+ // | n | | | | | | | | // +---+---+---+---+---+---+---+---+ // | | | | | P | | | | // +---+---+---+---+---+---+---+---+ // | | | | | | | | | // +---+---+---+---+---+---+---+---+ // | | | | | | | | | // +---+---+---+---+---+---+---+---+ // | P | P | P | P | | P | P | P | // +---+---+---+---+---+---+---+---+ // | R | N | B | Q | K | B | N | R | // +---+---+---+---+---+---+---+---+ ChessBoard testBoard = new ChessBoard(); testBoard.NewPosition("r1bqkbnr/pppppppp/n7/4P3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2"); // Prepare our en-passant target Trace.WriteLine("Preparing en-passant victim d7d5..."); MoveInformation moveInfo = new MoveInformation( new BoardSquare(D_FILE, 7), new BoardSquare(D_FILE, 5), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify the target was set Trace.WriteLine("Verifying target is at d7d6 (behind moved pawn)"); BoardSquare enPassantTarget; Assert.IsTrue(testBoard.GetEnPassantTarget(out enPassantTarget)); Assert.AreEqual(enPassantTarget, new BoardSquare(D_FILE, 6)); // Make another mover Trace.WriteLine("ignore capture opportunity a2a3..."); moveInfo = new MoveInformation( new BoardSquare(A_FILE, 2), new BoardSquare(A_FILE, 3), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify the target was cleared Trace.WriteLine("Verifying target is now cleared"); Assert.IsFalse(testBoard.GetEnPassantTarget(out enPassantTarget)); }
public void RevertPromotionMoveTest() { // PromotePiece does some verification, it must be a pawn and it must // be on a back rank // r2k2r1/p2n1P2/b3q2p/2p1p3/Pb1P4/R1N5/1pQ2PPP/3R2K1 w - - 0 22 // +---+---+---+---+---+---+---+---+ // | r | | | k | | | r | | // +---+---+---+---+---+---+---+---+ // | p | | | n | | P | | | // +---+---+---+---+---+---+---+---+ // | b | | | | q | | | p | // +---+---+---+---+---+---+---+---+ // | | | p | | p | | | | // +---+---+---+---+---+---+---+---+ // | P | b | | P | | | | | // +---+---+---+---+---+---+---+---+ // | R | | N | | | | | | // +---+---+---+---+---+---+---+---+ // | | p | Q | | | P | P | P | // +---+---+---+---+---+---+---+---+ // | | | | R | | | K | | // +---+---+---+---+---+---+---+---+ ChessBoard testBoard = new ChessBoard(); testBoard.NewPosition("r2k2r1/p2n1P2/b3q2p/2p1p3/Pb1P4/R1N5/1pQ2PPP/3R2K1 w - - 0 22"); // setup the promotion move f7g8n MoveInformation moveInfo = new MoveInformation( new BoardSquare(F_FILE, 7), new BoardSquare(G_FILE, 8), false, testBoard.CurrentFEN); testBoard.PromotePiece(F_FILE, 7, G_FILE, 8, PieceClass.Knight, ref moveInfo); testBoard.MovePiece(ref moveInfo); // Verify the promoted piece ChessPiece piece = testBoard.FindPieceAt(G_FILE, 8); Assert.AreEqual(PieceClass.Knight, piece.Job); Assert.AreEqual(PieceColor.White, piece.Color); // Revert the move testBoard.RevertLastMove(); // Verify the piece is a pawn again and back at F7 piece = testBoard.FindPieceAt(F_FILE, 7); Assert.AreEqual(PieceClass.Pawn, piece.Job); }
private MovedInformation MoveFile(OversawCollection oversawCollection, MoveInformation moveInformation) { using var loggerScope = _logger.BeginScope( "Processing new file in source: {NewFile}, {MoveProblem}, {@SourceTags}", moveInformation.SystemFile.File.FullName, moveInformation.MoveProblem, moveInformation.SourceTags); try { return(_destinationFolderService.Move(oversawCollection.DestinationFolder, moveInformation)); } catch (Exception e) { _logger.LogError(e, "Error occurred while moving file"); return(new MovedInformation(moveInformation, false, moveInformation.SystemFile.File)); } }
public void EnPassantMoveTests() { // Prepares ..d7d5 for en-passant capture // "r1bqkbnr/pppppppp/n7/4P3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2" // +---+---+---+---+---+---+---+---+ // | r | | b | q | k | b | n | r | // +---+---+---+---+---+---+---+---+ // | p | p | p | p | p | p | p | p | // +---+---+---+---+---+---+---+---+ // | n | | | | | | | | // +---+---+---+---+---+---+---+---+ // | | | | | P | | | | // +---+---+---+---+---+---+---+---+ // | | | | | | | | | // +---+---+---+---+---+---+---+---+ // | | | | | | | | | // +---+---+---+---+---+---+---+---+ // | P | P | P | P | | P | P | P | // +---+---+---+---+---+---+---+---+ // | R | N | B | Q | K | B | N | R | // +---+---+---+---+---+---+---+---+ ChessBoard testBoard = new ChessBoard(); testBoard.NewPosition("r1bqkbnr/pppppppp/n7/4P3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2"); // Prepare our en-passant target Trace.WriteLine("Preparing en-passant victim d7d5..."); MoveInformation moveInfo = new MoveInformation( new BoardSquare(D_FILE, 7), new BoardSquare(D_FILE, 5), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Now attempt the en-passant e5d6 Trace.WriteLine("Capture the black pawn by en-passant e5d6..."); moveInfo = new MoveInformation( new BoardSquare(E_FILE, 5), new BoardSquare(D_FILE, 6), true, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify the capture Trace.WriteLine("Verify the captured pawn"); Assert.IsTrue(moveInfo.IsCapture); Assert.AreEqual(moveInfo.CapturedPiece.Job, PieceClass.Pawn); Assert.AreEqual(moveInfo.CapturedPiece.Color, PieceColor.Black); }
private static void AddDestinationFolder( DestinationFolder destinationFolder, MoveInformation moveInformation, ICollection <string> newPathParts) { var destDirectory = destinationFolder.GetDestinationDirectory(); var fileDirectory = moveInformation.SystemFile.File.DirectoryName; if (destDirectory == null) { if (fileDirectory != null) { newPathParts.Add(fileDirectory); } } else { newPathParts.Add(destDirectory.FullName); } }
private async void ButtonMoveClick(object sender, RoutedEventArgs e) { hour = textBlockHour.Text; minute = textBlockMinute.Text; time = hour + minute; Thread thread = new Thread(ShowDate); using (var context = new ContextTypes()) { move = new MoveInformation { PathFrom = System.IO.Path.Combine(textBoxMoveFirst.Text), PathTo = textBoxMoveSecond.Text, Data = datePiker.SelectedDate.Value, Time = time }; context.Moving.Add(move); await context.SaveChangesAsync(); } }
public void BasicTests() { // ChessBoard.MovePiece() assumes the move is valid, but it will // handle some side-effects, like moving the rook on a king move // that is castling, en-passant captures, and promotions if marked ChessBoard testBoard = new ChessBoard(); testBoard.NewGame(); // Basic opening pawn move "e2e4" Trace.WriteLine("Applying a basic pawn move...e2e4"); MoveInformation moveInfo = new MoveInformation( new BoardSquare(E_FILE, 2), new BoardSquare(E_FILE, 4), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); Trace.WriteLine("Verify pawn is found at new location..."); ChessPiece piece = testBoard.FindPieceAt(E_FILE, 4); Assert.AreEqual(piece.Job, PieceClass.Pawn); Assert.AreEqual(piece.Color, PieceColor.White); }
public MovedInformation Move( DestinationFolder destinationFolder, MoveInformation moveInformation) { if (moveInformation.MoveProblem == MoveProblem.AlreadyContains) { var shouldDelete = destinationFolder.GetDestinationDirectory() != null; if (shouldDelete) { moveInformation.SystemFile.File.Delete(); } _logger.LogWarning( "File with this md5 already contains in database. Md5: {Md5} File: {NewFile}. File deleted {FileDeleted}.", moveInformation.SystemFile.Md5, moveInformation.SystemFile.File.FullName, shouldDelete); return(new MovedInformation( moveInformation, false, moveInformation.SystemFile.File)); } var newPath = GetNewPath(destinationFolder, moveInformation); _logger.LogInformation("Destination path is calculated: {NewPath}.", newPath); var newFile = new FileInfo(newPath); var wasANewFile = _fileService.MoveFile(moveInformation.SystemFile, ref newFile); _logger.LogInformation("Saved to: {NewPath}.", newFile.FullName); return(new MovedInformation( moveInformation, wasANewFile && moveInformation.MoveProblem == MoveProblem.None, newFile)); }
private static string GetNewPath( DestinationFolder destinationFolder, MoveInformation moveInformation) { var newPathParts = new List <string>(); AddDestinationFolder(destinationFolder, moveInformation, newPathParts); var problemSubfolder = GetProblemSubfolder(destinationFolder, moveInformation.MoveProblem); var renamed = false; if (problemSubfolder != null) { newPathParts.Add(problemSubfolder); } else { if (destinationFolder.ShouldCreateSubfoldersByHash) { AddMd5Path(moveInformation, newPathParts); } if (destinationFolder.ShouldRenameByHash) { RenameToMd5(moveInformation, newPathParts); renamed = true; } } if (!renamed) { newPathParts.Add(moveInformation.SystemFile.File.Name); } return(Path.Combine(newPathParts.ToArray())); }
/// <summary> /// After AI finds move, this move should be done and /// state should be updated and MakeTurn method should be called again /// </summary> private void OnTurnEnd(MoveInformation bestMove) { //Add callback to this method when move will be performed controller.ArmyFinishedMove += MakeTurn; MakeMove(bestMove.From, bestMove.To); }
public void RevertCastlingMoveTest() { // There are 4 possible castles (Wh:Bl)(Kingside:Queenside) // White to move: "r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R w KQkq - 6 8" //+---+---+---+---+---+---+---+---+ //| r | | | | k | | | r | //+---+---+---+---+---+---+---+---+ //| p | p | p | q | | p | p | p | //+---+---+---+---+---+---+---+---+ //| | | n | b | b | n | | | //+---+---+---+---+---+---+---+---+ //| | | | p | p | | | | //+---+---+---+---+---+---+---+---+ //| | | | P | P | | | | //+---+---+---+---+---+---+---+---+ //| | | N | B | B | N | | | //+---+---+---+---+---+---+---+---+ //| P | P | P | Q | | P | P | P | //+---+---+---+---+---+---+---+---+ //| R | | | | K | | | R | //+---+---+---+---+---+---+---+---+ ChessBoard testBoard = new ChessBoard(); testBoard.NewPosition("r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R w KQkq - 6 8"); // Castle White Kingside Trace.WriteLine("Castle White Kingside - e1g1"); MoveInformation moveInfo = new MoveInformation( new BoardSquare(E_FILE, 1), new BoardSquare(G_FILE, 1), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify the rook moved as well Trace.WriteLine("Verifying the rook moved as well"); ChessPiece whiteKingsideRook = testBoard.FindPieceAt(F_FILE, 1); Assert.AreEqual(whiteKingsideRook.Job, PieceClass.Rook); Trace.WriteLine("Verifying the castling rights are updated"); Assert.IsFalse(testBoard.WhiteCastlingRights.HasFlag(BoardSide.King)); // Revert it testBoard.RevertLastMove(); // Verify the king is back home Trace.WriteLine("Verify the king is back home"); ChessPiece king = testBoard.GetKing(PieceColor.White); Assert.AreEqual(king.Job, PieceClass.King); Assert.AreEqual(king.Color, PieceColor.White); Assert.AreEqual(king.File, E_FILE); Assert.AreEqual(king.Rank, 1); Assert.IsFalse(king.Deployed); // Verify the rook is back home Trace.WriteLine("Verify the rook is back home"); ChessPiece rook = testBoard.FindPieceAt(H_FILE, 1); Assert.AreEqual(rook.Job, PieceClass.Rook); Assert.AreEqual(rook.Color, PieceColor.White); Assert.AreEqual(rook.File, H_FILE); Assert.AreEqual(rook.Rank, 1); Assert.IsFalse(rook.Deployed); }
private static void RenameToMd5(MoveInformation moveInformation, List <string> newPathParts) { newPathParts.Add($"{moveInformation.SystemFile.Md5}{moveInformation.SystemFile.File.Extension}"); }
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { if (CurrentFloor == null) { return; } e.Handled = true; var position = GetAbsolutePosition(e.GetPosition(this)); try { switch (SelectedActionMode) { case ActionMode.AddStart: { var start = new StartNode(CurrentFloor.Model, position); AddVisualEntity(new VisualStartNode(start, CurrentFloor)); break; } case ActionMode.AddExit: { var exit = new ExitNode(CurrentFloor.Model, position); AddVisualEntity(new VisualExitNode(exit, CurrentFloor)); break; } case ActionMode.AddEntry: { var entry = new EntryNode(CurrentFloor.Model, position); AddVisualEntity(new VisualEntryNode(entry, CurrentFloor)); break; } case ActionMode.AddStairs: case ActionMode.AddRoad: { CreatingSection(position); break; } case ActionMode.Move: { var entity = CurrentFloor.GetVisualEntity(position); if (entity != null) { moveInformation = new MoveInformation(entity.GetUnit(position), position); entity.Model.IsSelected = true; } else if (Shell != null) { moveInformation = new MoveInformation(Shell, e.GetPosition(this), true); e.Handled = false; } e.Handled = true; break; } case ActionMode.Remove: { var entity = CurrentFloor.GetVisualEntity(position); CurrentFloor.RemoveVisualEntity(entity); break; } case ActionMode.SetScale: { if (IsCreatingLine) { CreatingLineInf.LastPosition = position; SetScale(); } else { CreatingLineInf = new CreatingLineInformation(new VisualThumb(position)); } break; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void BasicTests() { // There are 4 possible castles (Wh:Bl)(Kingside:Queenside) // White to move: "r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R w KQkq - 6 8" // Black to move: "r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R b KQkq - 6 8" //+---+---+---+---+---+---+---+---+ //| r | | | | k | | | r | //+---+---+---+---+---+---+---+---+ //| p | p | p | q | | p | p | p | //+---+---+---+---+---+---+---+---+ //| | | n | b | b | n | | | //+---+---+---+---+---+---+---+---+ //| | | | p | p | | | | //+---+---+---+---+---+---+---+---+ //| | | | P | P | | | | //+---+---+---+---+---+---+---+---+ //| | | N | B | B | N | | | //+---+---+---+---+---+---+---+---+ //| P | P | P | Q | | P | P | P | //+---+---+---+---+---+---+---+---+ //| R | | | | K | | | R | //+---+---+---+---+---+---+---+---+ // Reset board ChessBoard testBoard = new ChessBoard(); testBoard.NewPosition("r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R w KQkq - 6 8"); // Verify all rights intact Trace.WriteLine("Verifying all castling rights are still valid..."); Assert.IsTrue(testBoard.CanPlayerCastle(PieceColor.White, BoardSide.King)); Assert.IsTrue(testBoard.CanPlayerCastle(PieceColor.White, BoardSide.Queen)); Assert.IsTrue(testBoard.CanPlayerCastle(PieceColor.Black, BoardSide.King)); Assert.IsTrue(testBoard.CanPlayerCastle(PieceColor.Black, BoardSide.Queen)); // Move the white kingside rook h1g1 Trace.WriteLine("Moving kingside rook, h1g1..."); MoveInformation moveInfo = new MoveInformation( new BoardSquare(H_FILE, 1), new BoardSquare(G_FILE, 1), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify white kingside rights are gone Trace.WriteLine("Verifying castling rights... white kingside cleared"); Assert.IsFalse(testBoard.WhiteCastlingRights.HasFlag(BoardSide.King)); // Move the black kingside rook h8g8 Trace.WriteLine("Moving kingside rook, h8g8..."); moveInfo = new MoveInformation( new BoardSquare(H_FILE, 8), new BoardSquare(G_FILE, 8), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify black kingside rights are gone Trace.WriteLine("Verifying castling rights... black kingside cleared"); Assert.IsFalse(testBoard.BlackCastlingRights.HasFlag(BoardSide.King)); // Move the white queenside rook a1b1 Trace.WriteLine("Moving queenside rook, a1b1..."); moveInfo = new MoveInformation( new BoardSquare(A_FILE, 1), new BoardSquare(B_FILE, 1), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify white queenside rights are gone Trace.WriteLine("Verifying castling rights... white queenside cleared"); Assert.AreEqual(testBoard.WhiteCastlingRights, BoardSide.None); // Move the black queenside rook a8b8 Trace.WriteLine("Moving queenside rook, a8b8..."); moveInfo = new MoveInformation( new BoardSquare(A_FILE, 8), new BoardSquare(B_FILE, 8), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify black queenside rights are gone Trace.WriteLine("Verifying castling rights... black queenside cleared"); Assert.AreEqual(testBoard.BlackCastlingRights, BoardSide.None); }
public void BasicPropertiesTests() { BoardSquare startSquare = new BoardSquare(new PieceFile('b'), 6); BoardSquare endSquare = new BoardSquare(new PieceFile('e'), 3); bool previouslyDeployed = true; Trace.WriteLine(String.Format("Constucting default MoveInformation[{0}:{1}->{2}:{3}] Deployed: {4}", startSquare.File.ToString(), startSquare.Rank, endSquare.File.ToString(), endSquare.Rank, previouslyDeployed)); MoveInformation testMove = new MoveInformation(startSquare, endSquare, previouslyDeployed, ChessBoard.InitialFENPosition); // First verify the basic properties set on creation Trace.WriteLine(String.Format("Verifying start and end squares...")); Assert.AreEqual(testMove.Start, startSquare); Trace.WriteLine(String.Format("Verifying FirstMove...")); Assert.IsFalse(testMove.FirstMove); // Now set some extraneous properties Trace.WriteLine(String.Format("Verifying Color set/get...")); PieceColor testColor = PieceColor.Black; testMove.Color = testColor; Assert.AreEqual(testMove.Color, testColor); // CastlingRights Trace.WriteLine(String.Format("Verifying CastlingRights set/get...")); BoardSide testCastlingRights = BoardSide.Queen; testMove.CastlingRights = testCastlingRights; Assert.AreEqual(testMove.CastlingRights, testCastlingRights); // PromotionClass Trace.WriteLine(String.Format("Verifying PromotionJob set/get...")); PieceClass testPromotionClass = PieceClass.Knight; Assert.IsFalse(testMove.IsPromotion); testMove.PromotionJob = testPromotionClass; Assert.AreEqual(testMove.PromotionJob, testPromotionClass); Assert.IsTrue(testMove.IsPromotion); // CapturedPiece and CastlingRook are mutually exclusive Properties // neither can be checked without first being set or an exception is // thrown. Also, if one is set and you check the other, an exception // is thrown // IsCapture and IsCastle can be used to check if either is set // without the exception Trace.WriteLine(String.Format("Verifying IsCapture and IsCastle initially false...")); Assert.IsFalse(testMove.IsCapture); Assert.IsFalse(testMove.IsCastle); Trace.WriteLine(String.Format("Set a capture piece and verify IsCapture and CapturedPiece...")); ChessPiece testPiece = new ChessPiece(PieceColor.White, PieceClass.Pawn, new PieceFile('c'), 2); testMove.CapturedPiece = testPiece; Assert.IsTrue(testMove.IsCapture); Assert.AreEqual(testMove.CapturedPiece, testPiece); try { Trace.WriteLine(String.Format("Attempting to get the CastlingRook (invalid)...")); ChessPiece rook = testMove.CastlingRook; } catch (InvalidOperationException) { Trace.WriteLine(String.Format("Caught InvalidOperationException...")); } // Recreate the test move to clear the captured piece testMove = new MoveInformation(startSquare, endSquare, previouslyDeployed, ChessBoard.InitialFENPosition); Trace.WriteLine(String.Format("Set a castling rook and verify IsCastle and CsatlingRook...")); ChessPiece testRook = new ChessPiece(PieceColor.White, PieceClass.Rook, new PieceFile('a'), 1); testMove.CastlingRook = testRook; Assert.IsTrue(testMove.IsCastle); Assert.AreEqual(testMove.CastlingRook, testRook); try { Trace.WriteLine(String.Format("Attempting to get the CapturedPiece (invalid)...")); ChessPiece capture = testMove.CapturedPiece; } catch (InvalidOperationException) { Trace.WriteLine(String.Format("Caught InvalidOperationException...")); } // Setting a non-Rook to a castling rook will throw testMove = new MoveInformation(startSquare, endSquare, previouslyDeployed, ChessBoard.InitialFENPosition); try { testMove.CastlingRook = testPiece; } catch (ArgumentException) { Trace.WriteLine(String.Format("Caught ArgumentException...")); } }
public void CastlingMoveTests() { // There are 4 possible castles (Wh:Bl)(Kingside:Queenside) // White to move: "r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R w KQkq - 6 8" // Black to move: "r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R b KQkq - 6 8" //+---+---+---+---+---+---+---+---+ //| r | | | | k | | | r | //+---+---+---+---+---+---+---+---+ //| p | p | p | q | | p | p | p | //+---+---+---+---+---+---+---+---+ //| | | n | b | b | n | | | //+---+---+---+---+---+---+---+---+ //| | | | p | p | | | | //+---+---+---+---+---+---+---+---+ //| | | | P | P | | | | //+---+---+---+---+---+---+---+---+ //| | | N | B | B | N | | | //+---+---+---+---+---+---+---+---+ //| P | P | P | Q | | P | P | P | //+---+---+---+---+---+---+---+---+ //| R | | | | K | | | R | //+---+---+---+---+---+---+---+---+ // Reset board ChessBoard testBoard = new ChessBoard(); testBoard.NewPosition("r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R w KQkq - 6 8"); // Castle White Kingside Trace.WriteLine("Castle White Kingside - e1g1"); MoveInformation moveInfo = new MoveInformation( new BoardSquare(E_FILE, 1), new BoardSquare(G_FILE, 1), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify the rook moved as well Trace.WriteLine("Verifying the rook moved as well"); ChessPiece whiteKingsideRook = testBoard.FindPieceAt(F_FILE, 1); Assert.AreEqual(whiteKingsideRook.Job, PieceClass.Rook); Trace.WriteLine("Verifying the castling rights are updated"); Assert.IsFalse(testBoard.WhiteCastlingRights.HasFlag(BoardSide.King)); // Reset board testBoard = new ChessBoard(); testBoard.NewPosition("r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R w KQkq - 6 8"); // Castle White Queenside Trace.WriteLine("Castle White Kingside - e1c1"); moveInfo = new MoveInformation( new BoardSquare(E_FILE, 1), new BoardSquare(C_FILE, 1), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify the rook moved as well Trace.WriteLine("Verifying the rook moved as well"); ChessPiece whiteQueensideRook = testBoard.FindPieceAt(D_FILE, 1); Assert.AreEqual(whiteQueensideRook.Job, PieceClass.Rook); Trace.WriteLine("Verifying the castling rights are updated"); Assert.IsFalse(testBoard.WhiteCastlingRights.HasFlag(BoardSide.Queen)); // Reset board - note black to play in fen testBoard = new ChessBoard(); testBoard.NewPosition("r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R b KQkq - 6 8"); // Castle Black Kingside Trace.WriteLine("Castle White Kingside - e8g8"); moveInfo = new MoveInformation( new BoardSquare(E_FILE, 8), new BoardSquare(G_FILE, 8), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify the rook moved as well Trace.WriteLine("Verifying the rook moved as well"); ChessPiece blackKingsideRook = testBoard.FindPieceAt(F_FILE, 8); Assert.AreEqual(blackKingsideRook.Job, PieceClass.Rook); Trace.WriteLine("Verifying the castling rights are updated"); Assert.IsFalse(testBoard.BlackCastlingRights.HasFlag(BoardSide.King)); // Reset board - note black to play in fen testBoard = new ChessBoard(); testBoard.NewPosition("r3k2r/pppq1ppp/2nbbn2/3pp3/3PP3/2NBBN2/PPPQ1PPP/R3K2R b KQkq - 6 8"); // Castle Black Queenside Trace.WriteLine("Castle White Kingside - e8c8"); moveInfo = new MoveInformation( new BoardSquare(E_FILE, 8), new BoardSquare(C_FILE, 8), false, testBoard.CurrentFEN); testBoard.MovePiece(ref moveInfo); // Verify the rook moved as well Trace.WriteLine("Verifying the rook moved as well"); ChessPiece blackQueensideRook = testBoard.FindPieceAt(D_FILE, 8); Assert.AreEqual(blackQueensideRook.Job, PieceClass.Rook); Trace.WriteLine("Verifying the castling rights are updated"); Assert.IsFalse(testBoard.BlackCastlingRights.HasFlag(BoardSide.Queen)); }