public void TestFENEnPassantWhite() { var str = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3"; var b2 = Notation.ReadFEN(str); Assert.AreEqual(Notation.TextToTile("e3"), b2.EnPassantTile); // compare state var b1 = new Board(true); b1.Move(Notation.TextToTile("e2"), Notation.TextToTile("e4")); bool equal = b1.State.SequenceEqual(b2.State); Assert.IsTrue(equal); }
public void TestIsCaptureMoveEnPassant() { var b = new Board(); b.PlayerTurn = Color.Black; int posWhite = Notation.TextToTile("e5"); int posBlack = Notation.TextToTile("d7"); b.State[posWhite] = Colors.Val(Piece.Pawn, Color.White); b.State[posBlack] = Colors.Val(Piece.Pawn, Color.Black); b.Move(posBlack, posBlack - 16); bool capture = Moves.IsCaptureMove(b, posWhite, posWhite + 7); Assert.IsTrue(capture); }
public void TestEnPassantRight() { var b = new Board(); b.PlayerTurn = Color.White; int posBlack = Notation.TextToTile("e4"); int posWhite = Notation.TextToTile("f2"); b.State[posBlack] = Colors.Val(Piece.Pawn, Color.Black); b.State[posWhite] = Colors.Val(Piece.Pawn, Color.White); b.Move(posWhite, posWhite + 16); var moves = Attacks.GetAttacks(b, posBlack); Assert.AreEqual(3, moves.Length); Assert.IsTrue(moves.Contains(Notation.TextToTile("f4"))); }
public void TestEnPassantLeft() { var b = new Board(); b.PlayerTurn = Color.Black; int posWhite = Notation.TextToTile("e5"); int posBlack = Notation.TextToTile("d7"); b.State[posWhite] = Colors.Val(Piece.Pawn, Color.White); b.State[posBlack] = Colors.Val(Piece.Pawn, Color.Black); b.Move(posBlack, posBlack - 16); var moves = Moves.GetMoves(b, posWhite); Assert.AreEqual(2, moves.Length); Assert.IsTrue(moves.Contains(Notation.TextToTile("d6"))); }
public List <OpeningMove> FindMoves() { var output = new Dictionary <string, OpeningMove>(); for (int i = 0; i < Book.Length; i++) { var line = Book[i]; string nextMove = line[MovesMade + 1]; int from = Notation.TextToTile(nextMove.Substring(0, 2)); int to = Notation.TextToTile(nextMove.Substring(2, 2)); OpeningMove move = null; if (output.ContainsKey(nextMove)) { move = output[nextMove]; } else { move = new OpeningMove(); move.From = from; move.To = to; output[nextMove] = move; } move.SampleCount++; if (line[0] == "D") { move.Draws++; } else if (line[0] == "B") { move.BlackWins++; } else if (line[0] == "W") { move.WhiteWins++; } } return(output.Select(x => x.Value).ToList()); }
public void TestIsCaptureMoveNormal() { var b = new Board(); b.PlayerTurn = Color.Black; int posWhite = Notation.TextToTile("d2"); int posBlack = Notation.TextToTile("d6"); b.State[posWhite] = Colors.Val(Piece.Rook, Color.White); b.State[posBlack] = Colors.Val(Piece.Pawn, Color.Black); bool capture = Moves.IsCaptureMove(b, posWhite, posBlack); Assert.IsTrue(capture); capture = Moves.IsCaptureMove(b, posWhite, posBlack - 8); Assert.IsFalse(capture); }
public void TestIsEnPassantLeftBlack() { var b = new Board(); b.PlayerTurn = Color.White; int posBlack = Notation.TextToTile("e4"); int posWhite = Notation.TextToTile("d2"); b.State[posBlack] = Colors.Val(Piece.Pawn, Color.Black); b.State[posWhite] = Colors.Val(Piece.Pawn, Color.White); b.Move(posWhite, posWhite + 16); bool isEnPassant = Moves.IsEnPassantCapture(b, posBlack, posBlack - 9); Assert.IsTrue(isEnPassant); // test the other / wrong way - not allowed! isEnPassant = Moves.IsEnPassantCapture(b, posBlack, posBlack - 7); Assert.IsFalse(isEnPassant); }
public void TestEnPassantVictimBlackLeft() { var b = new Board(); b.PlayerTurn = Color.White; int posBlack = Notation.TextToTile("e4"); int posWhite = Notation.TextToTile("d2"); b.State[posBlack] = Colors.Val(Piece.Pawn, Color.Black); b.State[posWhite] = Colors.Val(Piece.Pawn, Color.White); b.Move(posWhite, posWhite + 16); bool isEnPassant = Moves.IsEnPassantCapture(b, posBlack, posBlack - 9); Assert.IsTrue(isEnPassant); int target = Moves.EnPassantVictim(b, posBlack, posBlack - 9); Assert.AreEqual(target, posWhite + 16); }
/// <summary> /// Returns information about the move. /// Is not guaranteed to return the "From" or "Color" field. /// Includes: /// Piece /// To /// Promotion /// Check /// Mate /// </summary> /// <returns></returns> public PGNMove GetMove(Board board) { var val = Value.Trim(); var move = new PGNMove(); move.Check = val.Contains('+') || val.Contains('#'); move.Mate = val.Contains('#'); move.Capture = val.Contains('x'); move.Color = board.PlayerTurn; move.MoveNumber = board.MoveCount; val = Regex.Replace(val, @"[x\.\+\#]", ""); move.Piece = Notation.GetPiece(val); move.Promotion = Notation.GetPromotion(val); // remove piece type and promotion if (move.Piece != Piece.Pawn) { val = val.Substring(1); } if (move.Promotion != Piece.None) { val = val.Replace("=", ""); val = val.Substring(0, val.Length - 1); } if (val == "O-O") { move.From = (move.Color == Color.White) ? 4 : 60; move.To = (move.Color == Color.White) ? 6 : 62; move.Piece = Piece.King; } else if (val == "O-O-O") { move.From = (move.Color == Color.White) ? 4 : 60; move.To = (move.Color == Color.White) ? 2 : 58; move.Piece = Piece.King; } else { move.To = Notation.TextToTile(val.Substring(val.Length - 2)); int fromX = -1; int fromY = -1; // check if there is additional information in the move, e.g. "From" tile if (val.Length == 4) { move.From = Notation.TextToTile(val.Substring(0, 2)); } else if (val.Length == 3) { char ch = val[0]; if (Char.IsLetter(ch)) { fromX = Notation.FileToInt(ch); } else { fromY = Notation.RankToInt(ch); } } move.From = GetMovingPiece(board, move, fromX, fromY); } return(move); }
public void TestTextToTile() { string a = "E1"; string b = "d5"; string c = "h8"; string d = "a1"; string e = "b5"; string f = "c7"; string g = "f5"; string h = "g6"; string X = "A0"; string XX = "A9"; string XXX = "A12"; string XXXX = "K4"; int tile = 0; tile = Notation.TextToTile(a); Assert.AreEqual(4, tile); tile = Notation.TextToTile(b); Assert.AreEqual(35, tile); tile = Notation.TextToTile(c); Assert.AreEqual(63, tile); tile = Notation.TextToTile(d); Assert.AreEqual(0, tile); tile = Notation.TextToTile(e); Assert.AreEqual(33, tile); tile = Notation.TextToTile(f); Assert.AreEqual(50, tile); tile = Notation.TextToTile(g); Assert.AreEqual(37, tile); tile = Notation.TextToTile(h); Assert.AreEqual(46, tile); bool exception = false; try { tile = Notation.TextToTile(X); } catch (Exception) { exception = true; } Assert.IsTrue(exception); try { tile = Notation.TextToTile(XX); } catch (Exception) { exception = true; } Assert.IsTrue(exception); try { tile = Notation.TextToTile(XXX); } catch (Exception) { exception = true; } Assert.IsTrue(exception); try { tile = Notation.TextToTile(XXXX); } catch (Exception) { exception = true; } Assert.IsTrue(exception); }