private string DetailedMoveToString(DetailedMove move, bool check) { if (move.Castling == CastlingType.KingSide) { return("O-O"); } else if (move.Castling == CastlingType.QueenSide) { return("O-O-O"); } if (move.Piece is ChessDotNet.Pieces.Pawn) { string movetext; if (move.IsCapture) { movetext = $"{move.OriginalPosition.ToString().ToLower()[0]}x{move.NewPosition.ToString().ToLower()}"; } else { movetext = move.NewPosition.ToString().ToLower(); } if (move.Promotion.HasValue) { movetext += "=" + move.Promotion.Value; } return(movetext + (check ? "+" : "")); } else { return($"{move.Piece.GetFenCharacter().ToString().ToUpper()}{(move.IsCapture ? 'x' : ' ')}{move.NewPosition.ToString().ToLower()}{(check ? '+' : ' ')}".Replace(" ", "")); } }
private void AddMoveToMoveHistory(DetailedMove move, bool silent = false) { Player shouldBePlayer = Plies % 2 == 0 ? Player.White : Player.Black; if (move.Player != shouldBePlayer) { throw new ArgumentException(""); } ++Plies; if (move.Player == Player.White) { int No = Plies / 2 + 1; while (MoveHistory.Rows.Count < No) { MoveHistory.Rows.Add(); } MoveHistory.Last().No = No; MoveHistory.Last().WhiteDetailedMove = move; } else { MoveHistory.Last().BlackDetailedMove = move; } if (!silent) { SetSelection(Plies); } }
void GenerateAntichessForcedCapturePositions(string pgn) { PgnReader <AntichessGame> pgnReader = new PgnReader <AntichessGame>(); AntichessGame copy = new AntichessGame(); pgnReader.ReadPgnFromString(pgn); string fen; ReadOnlyCollection <DetailedMove> moves = pgnReader.Game.Moves; if (moves.Count >= 2) { for (int i = 0; i < moves.Count; i++) { copy.ApplyMove(moves[i], true); if (i < 1) { continue; } fen = copy.GetFen(); ReadOnlyCollection <Move> validMoves = copy.GetValidMoves(copy.WhoseTurn); DetailedMove lastMove = moves[i]; if (validMoves.Count != 1) { continue; } if (copy.GetPieceAt(validMoves[0].NewPosition) != null || (copy.GetPieceAt(validMoves[0].OriginalPosition) is Pawn && validMoves[0].OriginalPosition.File != validMoves[0].NewPosition.File)) { fenQueue.Add(new Tuple <string, string[], string, string>(fen, new string[] { lastMove.OriginalPosition.ToString().ToLowerInvariant(), lastMove.NewPosition.ToString().ToLowerInvariant() }, "Antichess", "forcedCapture")); } } } }
void GenerateMateInOnePositions <TGame>(string pgn, Func <string, TGame> createFromFen, string variant, string initialFen) where TGame : ChessGame, new() { if (variant == "Horde" && initialFen.StartsWith("pppppppp")) { return; // Once upon a time on lichess, the Black player had the Pawns and the White player the Pieces. That has been changed now, resulting in several invalid PGNs. } PgnReader <TGame> pgnReader = new PgnReader <TGame>(); TGame copy = new TGame(); try { pgnReader.ReadPgnFromString(pgn); } catch (PgnException) { Console.WriteLine("[Warning] PGN exception for variant {0}; {1}", variant, pgn); return; // I once got a PGN with an atomic game where self-checks were allowed. So to handle this case and similar rule changes like this, I have to catch the PgnException. } string fen; ReadOnlyCollection <DetailedMove> moves = pgnReader.Game.Moves; if (moves.Count >= 3) // there will never be a mate-in-one position if white hasn't done two moves { for (int i = 0; i < moves.Count; i++) { copy.ApplyMove(moves[i], true); if (i < 2) { continue; // there will never be a mate-in-one position if white hasn't done two moves } if (copy is ThreeCheckChessGame) { ThreeCheckChessGame g = copy as ThreeCheckChessGame; if ((g.WhoseTurn == Player.White ? g.ChecksByWhite : g.ChecksByBlack) != 2) { continue; } } fen = copy.GetFen(); ReadOnlyCollection <Move> validMoves = copy.GetValidMoves(copy.WhoseTurn); DetailedMove lastMove = moves[i]; for (int j = 0; j < validMoves.Count; j++) { TGame copy2 = createFromFen(fen); copy2.ApplyMove(validMoves[j], true); if (copy2.IsWinner(ChessUtilities.GetOpponentOf(copy2.WhoseTurn))) { fenQueue.Add(new Tuple <string, string[], string, string>(fen, new string[] { lastMove.OriginalPosition.ToString().ToLowerInvariant(), lastMove.NewPosition.ToString().ToLowerInvariant() }, variant, "mateInOne")); break; } } } } }
public ChessBoardHistoryEntry(ChessGame game) { GCD = game.GetGameCreationData(); if (GCD.Moves.Length > 0) { Move = GCD.Moves.Last(); GCD.Moves = new DetailedMove[1] { Move }; } }
public static void TestInequality_DifferentIsCapture() { DetailedMove move1 = new DetailedMove(new Position(File.E, 2), new Position(File.E, 4), Player.White, null, new Queen(Player.White), true, CastlingType.None); DetailedMove move2 = new DetailedMove(new Position(File.E, 2), new Position(File.E, 4), Player.White, null, new Queen(Player.White), false, CastlingType.None); Assert.AreNotEqual(move1, move2, "move1 and move2 are equal"); Assert.False(move1.Equals(move2), "move1.Equals(move2) should be false"); Assert.False(move2.Equals(move1), "move2.Equals(move1) should be false"); Assert.True(move1 != move2, "move1 != move2 should be true"); Assert.True(move2 != move1, "move2 != move1 should be true"); Assert.False(move1 == move2, "move1 == move2 should be true"); Assert.False(move2 == move1, "move2 == move1 should be true"); Assert.AreNotEqual(move1.GetHashCode(), move2.GetHashCode()); }
public static void TestInequality_DifferentPlayer() { DetailedMove move1 = new DetailedMove(new Position(File.E, 2), new Position(File.E, 4), Player.White, null, new Pawn(Player.White), false, CastlingType.None); DetailedMove move2 = new DetailedMove(new Position(File.E, 2), new Position(File.E, 4), Player.Black, null, new Pawn(Player.Black), false, CastlingType.None); Assert.AreNotEqual(move1, move2, "move1 and move2 are equal"); Assert.False(move1.Equals(move2), "move1.Equals(move2) should be false"); Assert.False(move2.Equals(move1), "move2.Equals(move1) should be false"); Assert.True(move1 != move2, "move1 != move2 should be true"); Assert.True(move2 != move1, "move2 != move1 should be true"); Assert.False(move1 == move2, "move1 == move2 should be true"); Assert.False(move2 == move1, "move2 == move1 should be true"); Assert.AreNotEqual(move1.GetHashCode(), move2.GetHashCode()); }
public static void TestInequality_DifferentPromotion() { var move1 = new DetailedMove(new Square(File.E, 2), new Square(File.E, 4), Player.White, null, new Pawn(Player.White), false, CastlingType.None); var move2 = new DetailedMove(new Square(File.E, 2), new Square(File.E, 4), Player.White, null, new Queen(Player.White), false, CastlingType.None); Assert.AreNotEqual(move1, move2); Assert.False(move1.Equals(move2)); Assert.False(move2.Equals(move1)); Assert.False(move1 == move2); Assert.False(move2 == move1); Assert.True(move1 != move2); Assert.True(move2 != move1); Assert.AreNotEqual(move1.GetHashCode(), move2.GetHashCode()); }
public static void TestInequality_DifferentCastlingType() { var move1 = new DetailedMove(new Square(File.E, 1), new Square(File.C, 1), Player.White, null, new King(Player.White), true, CastlingType.QueenSide); var move2 = new DetailedMove(new Square(File.E, 1), new Square(File.G, 1), Player.White, null, new King(Player.White), false, CastlingType.KingSide); Assert.AreNotEqual(move1, move2); Assert.False(move1.Equals(move2)); Assert.False(move2.Equals(move1)); Assert.False(move1 == move2); Assert.False(move2 == move1); Assert.True(move1 != move2); Assert.True(move2 != move1); Assert.AreNotEqual(move1.GetHashCode(), move2.GetHashCode()); }
public static void TestInequality_DifferentCastlingType() { DetailedMove move1 = new DetailedMove(new Position(File.E, 1), new Position(File.C, 1), Player.White, null, new King(Player.White), true, CastlingType.QueenSide); DetailedMove move2 = new DetailedMove(new Position(File.E, 1), new Position(File.G, 1), Player.White, null, new King(Player.White), false, CastlingType.KingSide); Assert.AreNotEqual(move1, move2, "move1 and move2 are equal"); Assert.False(move1.Equals(move2), "move1.Equals(move2) should be false"); Assert.False(move2.Equals(move1), "move2.Equals(move1) should be false"); Assert.True(move1 != move2, "move1 != move2 should be true"); Assert.True(move2 != move1, "move2 != move1 should be true"); Assert.False(move1 == move2, "move1 == move2 should be true"); Assert.False(move2 == move1, "move2 == move1 should be true"); Assert.AreNotEqual(move1.GetHashCode(), move2.GetHashCode()); }
public static void TestEquality() { var move1 = new DetailedMove(new Square(File.E, 2), new Square(File.E, 4), Player.White, null, new Pawn(Player.White), false, CastlingType.None); var move2 = new DetailedMove(new Square(File.E, 2), new Square(File.E, 4), Player.White, null, new Pawn(Player.White), false, CastlingType.None); Assert.AreEqual(move1, move2); Assert.True(move1.Equals(move2)); Assert.True(move2.Equals(move1)); Assert.True(move1 == move2); Assert.True(move2 == move1); Assert.True(null == (DetailedMove)null); Assert.False(move1 != move2); Assert.False(move2 != move1); Assert.AreEqual(move1.GetHashCode(), move2.GetHashCode()); }
public static void TestEquality() { DetailedMove move1 = new DetailedMove(new Position(File.E, 2), new Position(File.E, 4), Player.White, null, new Pawn(Player.White), false, CastlingType.None); DetailedMove move2 = new DetailedMove(new Position(File.E, 2), new Position(File.E, 4), Player.White, null, new Pawn(Player.White), false, CastlingType.None); DetailedMove move3 = null; DetailedMove move4 = null; Assert.AreEqual(move1, move2, "move1 and move2 should be equal"); Assert.True(move1.Equals(move2), "move1.Equals(move2) should be true"); Assert.True(move2.Equals(move1), "move2.Equals(move1) should be true"); Assert.True(move1 == move2, "move1 == move2 should be true"); Assert.True(move2 == move1, "move2 == move1 should be true"); Assert.True(move3 == move4, "move3 == move4 should be True"); Assert.False(move1 != move2, "move1 != move2 should be false"); Assert.False(move2 != move1, "move2 != move1 should be false"); Assert.AreEqual(move1.GetHashCode(), move2.GetHashCode()); }
public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } if (ReferenceEquals(this, obj)) { return(true); } DetailedMove move = (DetailedMove)obj; return(OriginalPosition.Equals(move.OriginalPosition) && NewPosition.Equals(move.NewPosition) && Player == move.Player && Promotion == move.Promotion && Piece == move.Piece && IsCapture == move.IsCapture && Castling == move.Castling); }
public GameCreationData() { Moves = new DetailedMove[] {}; Resigned = Player.None; FullMoveNumber = 1; }
public static string MakeFromBoardAndMove( ChessGame before, DetailedMove move) { var ep = before.GetFen().Split(' ')[3]; string eran = ""; var pieceChar = Char.ToUpper(move.Piece.GetFenCharacter()); if (pieceChar != 'P') { eran += pieceChar; } eran += move.OriginalPosition.ToString().ToLower(); if (move.IsCapture) { eran += 'x'; var capturedPiece = before.GetPieceAt(move.NewPosition); if (capturedPiece == null) { capturedPiece = new Pawn(); } var capturedPieceChar = Char.ToUpper(capturedPiece.GetFenCharacter()); if (capturedPieceChar != 'P') { eran += capturedPieceChar; } } else { eran += '-'; } eran += move.NewPosition.ToString().ToLower(); eran += ' '; bool anyCastle = false; if (before.CanWhiteCastleKingSide) { eran += 'K'; anyCastle = true; } if (before.CanWhiteCastleQueenSide) { eran += 'Q'; anyCastle = true; } if (before.CanBlackCastleKingSide) { eran += 'k'; anyCastle = true; } if (before.CanBlackCastleQueenSide) { eran += 'q'; anyCastle = true; } if (!anyCastle) { eran += '-'; } eran += ' '; if (ep != "-") { eran += ep; } else { eran += '-'; } return(eran); }
public CrazyhouseDetailedMove(DetailedMove move) : base(new Move(move.OriginalPosition, move.NewPosition, move.Player), move.Piece, move.IsCapture, move.Castling, move.SAN) { IsDrop = false; }
protected override void AddDetailedMove(DetailedMove dm) { AddDetailedMove(new CrazyhouseDetailedMove(dm)); }