Beispiel #1
0
        static void Main(string[] args)
        {
            bool      isValid;
            string    fen  = "k7/4r3/8/8/4R3/8/8/K7 w - - 0 1";
            ChessGame game = new ChessGame(fen);
            Player    me   = Player.White;

            while (!game.IsGameOver())
            {
                DrawBoard(game);
                if (game.WhoseTurn == me)
                {
                    Move move;
                    do
                    {
                        string from = Console.ReadLine();
                        string to   = Console.ReadLine();
                        move = new Move(from, to, me);
                        if ((move.NewPosition.Rank == 1 && move.Player == Player.Black) || (move.NewPosition.Rank == 8 && move.Player == Player.White))
                        {
                            move.Promotion = 'Q';
                        }
                        isValid = game.IsValidMove(move);
                    } while (!isValid);
                    move.Promotion = null;
                    game.ApplyMove(move, true);
                }
                else
                {
                    string from = Console.ReadLine();
                    string to   = Console.ReadLine();
                    Move   move = new Move(from, to, game.WhoseTurn);
                    isValid = game.IsValidMove(move);
                    if (!isValid)
                    {
                        Console.WriteLine("Invalid move");
                    }
                    game.ApplyMove(move, true);
                }
            }
            if (game.DrawClaimed && game.DrawByRepitition)
            {
                Console.WriteLine("Draw");
                Console.WriteLine("Board State Repeated 3 times.");
            }
            else if (game.DrawClaimed && !game.DrawByRepitition)
            {
                Console.WriteLine("Draw");
                Console.WriteLine(game.DrawReason);
            }
            else if (game.IsWinner(me))
            {
                Console.WriteLine("You win");
            }
            else
            {
                Console.WriteLine("You lose");
            }
            Console.Read();
        }
 public static void TestAfter1c5()
 {
     ChessGame game = new ChessGame();
     game.ApplyMove(new Move("E2", "E4", Player.White), true);
     game.ApplyMove(new Move("C7", "C5", Player.Black), true);
     string fen = game.GetFen();
     Assert.AreEqual("rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2", fen);
 }
Beispiel #3
0
        public static void TestAfter1c5()
        {
            var game = new ChessGame();

            game.ApplyMove(new Move("E2", "E4", Player.White), true);
            game.ApplyMove(new Move("C7", "C5", Player.Black), true);

            Assert.AreEqual("rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2", game.GetFen());
        }
 public static void TestMovingWhiteKingLosingCastlingRights()
 {
     ChessGame game = new ChessGame();
     game.ApplyMove(new Move("E2", "E4", Player.White), true);
     game.ApplyMove(new Move("C7", "C5", Player.Black), true);
     game.ApplyMove(new Move("E1", "E2", Player.White), true);
     string fen = game.GetFen();
     Assert.AreEqual("rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPPKPPP/RNBQ1BNR b kq - 1 2", fen);
 }
Beispiel #5
0
        public static void TestMovingWhiteHRookLosingCastlingRights()
        {
            var game = new ChessGame();

            game.ApplyMove(new Move("H2", "H3", Player.White), true);
            game.ApplyMove(new Move("E7", "E5", Player.Black), true);
            game.ApplyMove(new Move("H1", "H2", Player.White), true);

            Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/8/7P/PPPPPPPR/RNBQKBN1 b Qkq - 1 2", game.GetFen());
        }
Beispiel #6
0
        public static void TestAfter2Nf3()
        {
            var game = new ChessGame();

            game.ApplyMove(new Move("E2", "E4", Player.White), true);
            game.ApplyMove(new Move("C7", "C5", Player.Black), true);
            game.ApplyMove(new Move("G1", "F3", Player.White), true);

            Assert.AreEqual("rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2", game.GetFen());
        }
Beispiel #7
0
        public static void TestMovingWhiteKingLosingCastlingRights()
        {
            var game = new ChessGame();

            game.ApplyMove(new Move("E2", "E4", Player.White), true);
            game.ApplyMove(new Move("C7", "C5", Player.Black), true);
            game.ApplyMove(new Move("E1", "E2", Player.White), true);

            Assert.AreEqual("rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPPKPPP/RNBQ1BNR b kq - 1 2", game.GetFen());
        }
Beispiel #8
0
        public static void TestMovingWhiteARookLosingCastlingRights()
        {
            var game = new ChessGame();

            game.ApplyMove(new Move("A2", "A3", Player.White), true);
            game.ApplyMove(new Move("E7", "E5", Player.Black), true);
            game.ApplyMove(new Move("A1", "A2", Player.White), true);

            Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/8/P7/RPPPPPPP/1NBQKBNR b Kkq - 1 2", game.GetFen());
        }
 public static void TestMovingBlackKingLosingCastlingRights()
 {
     ChessGame game = new ChessGame();
     game.ApplyMove(new Move("E2", "E4", Player.White), true);
     game.ApplyMove(new Move("E7", "E5", Player.Black), true);
     game.ApplyMove(new Move("G1", "F3", Player.White), true);
     game.ApplyMove(new Move("E8", "E7", Player.Black), true);
     string fen = game.GetFen();
     Assert.AreEqual("rnbq1bnr/ppppkppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQ - 2 3", fen);
 }
Beispiel #10
0
        public static void TestMovingBlackARookLosingCastlingRights()
        {
            var game = new ChessGame();

            game.ApplyMove(new Move("E2", "E4", Player.White), true);
            game.ApplyMove(new Move("A7", "A6", Player.Black), true);
            game.ApplyMove(new Move("G1", "F3", Player.White), true);
            game.ApplyMove(new Move("A8", "A7", Player.Black), true);

            Assert.AreEqual("1nbqkbnr/rppppppp/p7/8/4P3/5N2/PPPP1PPP/RNBQKB1R w KQk - 2 3", game.GetFen());
        }
Beispiel #11
0
        public static void TestMovingBlackHRookLosingCastlingRights()
        {
            var game = new ChessGame();

            game.ApplyMove(new Move("E2", "E4", Player.White), true);
            game.ApplyMove(new Move("H7", "H6", Player.Black), true);
            game.ApplyMove(new Move("G1", "F3", Player.White), true);
            game.ApplyMove(new Move("H8", "H7", Player.Black), true);

            Assert.AreEqual("rnbqkbn1/pppppppr/7p/8/4P3/5N2/PPPP1PPP/RNBQKB1R w KQq - 2 3", game.GetFen());
        }
Beispiel #12
0
        public static void TestMovingBlackKingLosingCastlingRights()
        {
            var game = new ChessGame();

            game.ApplyMove(new Move("E2", "E4", Player.White), true);
            game.ApplyMove(new Move("E7", "E5", Player.Black), true);
            game.ApplyMove(new Move("G1", "F3", Player.White), true);
            game.ApplyMove(new Move("E8", "E7", Player.Black), true);

            Assert.AreEqual("rnbq1bnr/ppppkppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQ - 2 3", game.GetFen());
        }
Beispiel #13
0
        public async Task <object> MakeMove(object moveData)
        {
            var state = (GameState)moveData;

            _gameLib.ApplyMove(new Move(
                                   Converter.ConvertChessPosition(state.LastMove.Item1),
                                   Converter.ConvertChessPosition(state.LastMove.Item2), _gameLib.WhoseTurn),
                               alreadyValidated: true);
            _lastMove = state.LastMove;

            return(await Task.Run(() => {
                return true;
            }));
        }
Beispiel #14
0
        static void Main(string[] args)
        {
            // Let's start by creating a chess game instance.
            ChessGame game = new ChessGame();

            // Now the game's board is in the start position and it's white's turn.
            Console.WriteLine("It's this color's turn: {0}", game.WhoseTurn);

            // This is how to find out which piece is at a certain position:
            Piece pieceAtA1 = game.GetPieceAt(new Position("A1")); // Or "a1", the casing doesn't matter

            /* There are other overloading options as well:
             * game.GetPieceAt(new Position(File.A, 1));
             * game.GetPieceAt(File.A, 1);
             * All those three options return the same. */
            Console.WriteLine("What piece is there at A1? {0}", pieceAtA1.GetFenCharacter());
            // GetFenCharacter() returns the FEN character for the given piece. White piece: uppercase, black piece: lowercase. The character is the first char of a piece's name (exception: Knight -> N/n because King -> K/k).
            // The Piece class is the abstract base class for pieces. All piece classes (e.g. Rook) derive from this class.

            // White has to make a turn. They want to move their E2 pawn to E4. Is that valid?
            Move e2e4    = new Move("E2", "E4", Player.White);
            bool isValid = game.IsValidMove(e2e4);

            Console.WriteLine("E2-E4 for white is valid: {0}", isValid);

            // Great, it's valid! So white wants to actually make that move.
            MoveType type = game.ApplyMove(e2e4, true);

            // The first argument is the move, the second argument indicates whether it's already validated. Here it is, so pass 'true'. If it's not validated yet, ApplyMove will do it. **Only pass `true` if it's really validated! If you pass `true`, ApplyMove won't do ANY validity checks.**
            // The return type is the MoveType enum. It holds one, or a combination, of these values: Invalid, Move, Capture, Castling, Promotion
            // Each valid move will always carry the 'Move' value. If it's also something else, it will carry both values (e.g. if the move is a capture, `type` will have the value MoveType.Move | MoveType.Capture).
            // MoveType is a flags enumeration. https://msdn.microsoft.com/en-us/library/ms229062%28v=vs.100%29.aspx
            // e4 is just a normal move, so `type` will just be MoveType.Move.
            Console.WriteLine("Move type: {0}", type);
            Debug.Log(game.GetPieceAt(File.E, 4));

            // When a move has been made, check the Status property. It will let you know if there is a special event: check, checkmate, stalemate ...
            // GameStatus status = game.Status;
            // Console.WriteLine("Special event? {0}", status.Event);
            // Here it just returns 'None' because nothing special happened.
            // GameStatus has two other properties: PlayerWhoCausedEvent (quite self-explanatory) and EventExplanation (used if Chess.NET needs to be more specific about an event, e.g. when it's a draw, explaining why).

            // Now it's black's turn.
            Console.WriteLine("It's this color's turn: {0}", game.WhoseTurn);

            // You can figure out all valid moves using GetValidMoves.
            IEnumerable <Move> validMoves = game.GetValidMoves(Player.Black);

            // Here it returns all valid moves for black, but you can also find all valid moves *from a certain position* by passing a Position instance as argument.
            Console.WriteLine("How many valid moves does black have? {0}", validMoves.Count());

            // It might happen that you don't really care about all valid moves, but just want to know if there are valid moves. Chess.NET also has a method for that:
            bool hasValidMoves = game.HasAnyValidMoves(Player.Black);

            // Again, you can also pass a Position instance here.
            Console.WriteLine("Black has any valid moves: {0}", hasValidMoves);

            // Congratulations! You have learned about the most important methods of Chess.NET. Enjoy using the library :)
            Console.ReadKey();
        }
Beispiel #15
0
        void PuzzleFinished(SubmittedMoveResponse response, bool correct)
        {
            CurrentPuzzleEndedUtc = DateTime.UtcNow;

            response.Correct         = correct ? 1 : -1;
            response.ExplanationSafe = Current.ExplanationSafe;

            PastPuzzleIds.Add(Current.ID);

            if (!correct)
            {
                Moves.RemoveAt(Moves.Count - 1);
                FENs.RemoveAt(FENs.Count - 1);
                Checks.RemoveAt(Checks.Count - 1);

                response.FEN = FENs[FENs.Count - 1];

                ChessGame correctGame = gameConstructor.Construct(Current.Variant, response.FEN);
                foreach (string move in PossibleVariations.First())
                {
                    string[] p = move.Split('-', '=');
                    correctGame.ApplyMove(new Move(p[0], p[1], correctGame.WhoseTurn, p.Length == 2 ? null : new char?(p[2][0])), true);
                    FENs.Add(correctGame.GetFen());
                    Checks.Add(correctGame.IsInCheck(correctGame.WhoseTurn) ? correctGame.WhoseTurn.ToString().ToLowerInvariant() : null);
                    Moves.Add(move);
                }
            }
            response.ReplayFENs   = FENs;
            response.ReplayChecks = Checks;
            response.ReplayMoves  = Moves;
        }
Beispiel #16
0
        public static void TestCastling_NoCastlingButCapture()
        {
            ChessGame game = new ChessGame("r2q3r/4bkpp/p3Rn2/1ppp4/3P4/2P5/PP1B1PPP/R2Q2K1 b - - 0 1");

            game.ApplyMove(new Move("F7", "E6", Player.Black), true);
            Assert.AreEqual("r2q3r/4b1pp/p3kn2/1ppp4/3P4/2P5/PP1B1PPP/R2Q2K1 w - - 0 2", game.GetFen());
        }
        public bool Move()
        {
            var requestbody = "";

            using (StreamReader reader = new StreamReader(Request.Body))
            {
                requestbody = reader.ReadToEnd();
            }
            var datas = requestbody.Split("-");
            //datas[0] --> player      datas[1] --> original    datas[2] --> new
            Move move = new Move(datas[1], datas[2], MakePlayer(datas[0]));

            if (datas[0] != game.WhoseTurn.ToString())
            {
                return(false);
            }
            if (!game.IsValidMove(move))
            {
                return(false);
            }
            game.ApplyMove(move, true);
            // need to add special conditions here!
            // not finished yet!
            return(true);
        }
Beispiel #18
0
 public static void TestAfter1e4()
 {
     ChessGame game = new ChessGame();
     game.ApplyMove(new Move("E2", "E4", Player.White), true);
     string fen = game.GetFen();
     Assert.AreEqual("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", fen);
 }
Beispiel #19
0
        static bool MyTurn()
        {
            ReadOnlyCollection <MoreDetailedMove> validMoves = Game.GetValidMoves(MyPlayer);

            if (validMoves.Count == 0)
            {
                // Lose or Draw
                //Environment.Exit(1);
                return(false);
            }
            else
            {
                //System.Threading.Thread.Sleep(1000);
                var    random       = new Random();
                var    k            = random.Next(0, validMoves.Count);
                var    move         = validMoves[k];
                var    x            = random.Next(0, 100);
                string targetString = move.GenerateSANString(Game);
                if (x > 100)
                {
                    Trace.WriteLine("Retard Breaks.");
                    targetString = targetString.Insert(random.Next(0, targetString.Length), (errorChar[random.Next(0, errorChar.Length)]).ToString());
                }
                Console.Out.WriteLine(targetString);
                Trace.WriteLine("My Move: " + targetString);

                if (x > 100)
                {
                    string invalidReceive = Console.In.ReadLine();
                    if (invalidReceive != ".")
                    {
                        throw new ArgumentException("Invalidation prompt is not `.`");
                    }
                    //System.Threading.Thread.Sleep(3000);
                    Console.Out.WriteLine(move.SANString);
                    Trace.WriteLine("My Re-Move: " + targetString);
                }

                MoveType mt = Game.ApplyMove(move, true);
                if (mt == MoveType.Invalid)
                {
                    Trace.TraceError("My move san has errors.");
                    return(false);
                }
            }
            return(true);
        }
Beispiel #20
0
        public static bool ApplyMoveAndCheckOtherValidMoves()
        {
            var      game = new ChessGame();
            var      e2e4 = new Move("E2", "E4", Player.White);
            MoveType type = game.ApplyMove(e2e4, true);

            return(game.HasAnyValidMoves(Player.Black) || type == MoveType.Invalid);
        }
Beispiel #21
0
        public static void TestAfter1e4()
        {
            var game = new ChessGame();

            game.ApplyMove(new Move("E2", "E4", Player.White), true);

            Assert.AreEqual("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", game.GetFen());
        }
Beispiel #22
0
        public static bool CheckMoveIsValidAndApply()
        {
            var      game    = new ChessGame();
            var      e2e4    = new Move("E2", "E4", Player.White);
            bool     isValid = game.IsValidMove(e2e4);
            MoveType type    = game.ApplyMove(e2e4, true);

            return(type == MoveType.Invalid && isValid);
        }
Beispiel #23
0
 public override Task OnActivateAsync()
 {
     ChessGame = new ChessGame();
     foreach (var move in State.Moves)
     {
         ChessGame.ApplyMove(move, true);
     }
     return(base.OnActivateAsync());
 }
Beispiel #24
0
        public static bool ApplyMoveAndCheckValid()
        {
            var      game    = new ChessGame();
            var      e2e4    = new Move("E2", "E4", Player.White);
            Piece    pawn    = game.GetPieceAt(new Position("E2"));
            MoveType type    = game.ApplyMove(e2e4, true);
            bool     isValid = pawn.IsValidMove(e2e4, game);

            return(isValid && type == MoveType.Invalid);
        }
Beispiel #25
0
        public static bool ComplexTest()
        {
            var      game      = new ChessGame();
            Piece    pieceAtA1 = game.GetPieceAt(new Position("A1"));
            var      e2e4      = new Move("E2", "E4", Player.White);
            bool     isValid   = game.IsValidMove(e2e4);
            MoveType type      = game.ApplyMove(e2e4, true);

            return(game.HasAnyValidMoves(Player.Black) || type == MoveType.Invalid || isValid);
        }
Beispiel #26
0
        public static void TestCastling_KingE_RooksCF()
        {
            ChessGame game = new ChessGame("bnrbkrnq/pppppppp/8/8/8/8/PPPPPPPP/BNRBKRNQ w KQkq - 0 1");

            string[] moves = { "b2b3", "h7h6", "a1b2", "g7g6", "b1a3", "e7e6", "c2c3", "d7d6", "d1c2", "c7c6", "g1f3", "b7b6", "h2h3", "a7a6", "h1h2", "f7f6" };
            foreach (string m in moves)
            {
                game.ApplyMove(new Move(m.Substring(0, 2), m.Substring(2, 2), game.WhoseTurn), true);
            }

            Assert.True(game.IsValidMove(new Move("E1", "C1", Player.White)));
            Assert.True(game.IsValidMove(new Move("E1", "F1", Player.White)));
        }
Beispiel #27
0
        public static void Test960CastlingBlack1()
        {
            ChessGame game = new ChessGame("q1nbrk1r/pp2p1pp/2npbp2/8/4P3/1P3P2/P1PP1BPP/QNNBRRK1 b kq - 3 6");

            Assert.True(game.IsValidMove(new Move("F8", "H8", Player.Black)), "Kingside castling should be valid.");
            Assert.False(game.IsValidMove(new Move("F8", "E8", Player.Black)), "Queenside castling should be invalid.");

            Assert.Contains(new Move("F8", "H8", Player.Black), game.GetValidMoves(Player.Black));
            Assert.Contains(new Move("F8", "H8", Player.Black), game.GetValidMoves(new Position("F8")));

            game.ApplyMove(new Move("F8", "H8", Player.Black), true);
            Assert.AreEqual("q1nbrrk1/pp2p1pp/2npbp2/8/4P3/1P3P2/P1PP1BPP/QNNBRRK1 w - - 4 7", game.GetFen());
        }
Beispiel #28
0
        public static void Test960CastlingWhite2()
        {
            ChessGame game = new ChessGame("bbrqknr1/pp1ppppp/6n1/2p5/4P3/5Q2/PPPP1PPP/BBR1KNRN w KQkq - 2 3");

            Assert.True(game.IsValidMove(new Move("E1", "C1", Player.White)), "Queenside castling should be valid.");
            Assert.False(game.IsValidMove(new Move("E1", "G1", Player.White)), "Kingside castling should be invalid.");

            Assert.Contains(new Move("E1", "C1", Player.White), game.GetValidMoves(Player.White));
            Assert.Contains(new Move("E1", "C1", Player.White), game.GetValidMoves(new Position("E1")));

            game.ApplyMove(new Move("E1", "C1", Player.White), true);
            Assert.AreEqual("bbrqknr1/pp1ppppp/6n1/2p5/4P3/5Q2/PPPP1PPP/BBKR1NRN b kq - 3 3", game.GetFen());
        }
Beispiel #29
0
        public static void Test960CastlingWhite1()
        {
            ChessGame game = new ChessGame("qnbbrkr1/pppp1ppp/6n1/4p3/8/1P4N1/P1PPPPPP/QNBBRKR1 w KQkq - 1 3");

            Assert.True(game.IsValidMove(new Move("F1", "G1", Player.White)), "Kingside castling should be valid.");
            Assert.False(game.IsValidMove(new Move("F1", "E1", Player.White)), "Queenside castling should be invalid.");

            Assert.Contains(new Move("F1", "G1", Player.White), game.GetValidMoves(Player.White));
            Assert.Contains(new Move("F1", "G1", Player.White), game.GetValidMoves(new Position("F1")));

            game.ApplyMove(new Move("F1", "G1", Player.White), true);
            Assert.AreEqual("qnbbrkr1/pppp1ppp/6n1/4p3/8/1P4N1/P1PPPPPP/QNBBRRK1 b kq - 2 3", game.GetFen());
        }
Beispiel #30
0
        public static void Test960CastlingBlack2()
        {
            ChessGame game = new ChessGame("r1k3br/pppp2pp/2n2p2/8/7N/8/PKP2PPP/3R2BR b kq - 2 12");

            Assert.True(game.IsValidMove(new Move("C8", "A8", Player.Black)), "Queenside castling should be valid.");
            Assert.False(game.IsValidMove(new Move("C8", "H8", Player.Black)), "Kingside castling should be invalid.");

            Assert.Contains(new Move("C8", "A8", Player.Black), game.GetValidMoves(Player.Black));
            Assert.Contains(new Move("C8", "A8", Player.Black), game.GetValidMoves(new Position("C8")));

            game.ApplyMove(new Move("C8", "A8", Player.Black), true);
            Assert.AreEqual("2kr2br/pppp2pp/2n2p2/8/7N/8/PKP2PPP/3R2BR w - - 3 13", game.GetFen());
        }
Beispiel #31
0
        private async void MakeComputerMove()
        {
            // Set the fen
            chessEngineProcess.StandardInput.WriteLine("position fen " + chessGame.GetFen());
            chessEngineProcess.StandardInput.WriteLine("go movetime " + (ThinkingTime));

            // Keep reading lines until "bestmove" comes up
            string programOutput;

            do
            {
                programOutput = await chessEngineProcess.StandardOutput.ReadLineAsync();
            } while (!programOutput.StartsWith("bestmove"));

            // Make that move
            // TODO: Fix
            programOutput = programOutput.Remove(0, 9);
            chessGame.ApplyMove(new Move(programOutput.Substring(0, 2), programOutput.Substring(2, 2), chessGame.WhoseTurn,
                                         programOutput.Length == 5 ? programOutput[4] as char? : null), true);

            chessboard.FEN = chessGame.GetFen();

            var    lastMove  = chessGame.Moves.Last();
            string moveInSAN = DetailedMoveToString(lastMove, chessGame.IsInCheck(chessGame.WhoseTurn));

            // If it was White to move last
            if (chessGame.WhoseTurn == Player.Black)
            {
                MoveList.Rows.Add(moveInSAN);
            }
            else
            {
                MoveList.Rows[MoveList.Rows.GetLastRow(0)].Cells["ColumnBlack"].Value = moveInSAN;
            }

            // Test for checkmate, stalemate
            if (chessGame.IsCheckmated(chessGame.WhoseTurn))
            {
                chessboard.UnhighlightSquares();
                StopGame(Enum.GetName(typeof(Player), chessGame.WhoseTurn) + " has been checkmated");
            }
            else if (chessGame.IsStalemated(chessGame.WhoseTurn))
            {
                chessboard.UnhighlightSquares();
                StopGame(Enum.GetName(typeof(Player), chessGame.WhoseTurn) + " has been stalemated");
            }
            else
            {
                chessboard.SquareClicked += Chessboard_SquareClicked;
            }
        }
        public Modified MakeMove([OptionsFrom(nameof(MakeMove_options))] MoveInfo moveInfo)
        {
            var move = _chessGame.GetValidMoves(_chessGame.WhoseTurn).Single(m =>
                                                                             (int)m.OriginalPosition.Rank == moveInfo.OrigX &&
                                                                             (int)m.OriginalPosition.File == moveInfo.OrigY &&
                                                                             (int)m.NewPosition.Rank == moveInfo.DestX &&
                                                                             (int)m.NewPosition.File == moveInfo.DestY &&
                                                                             (char?)m.Promotion == moveInfo.Promotion &&
                                                                             m.Player.ToString() == moveInfo.Player
                                                                             );

            _chessGame.ApplyMove(move, false);
            return(new Modified(this));
        }
Beispiel #33
0
        private static void Example()
        {
            var   game      = new ChessGame();
            Piece pieceAtA1 = game.GetPieceAt(new Position("A1")); // Or "a1", the casing doesn't matter

            Console.WriteLine("What piece is there at A1? {0}", pieceAtA1.GetFenCharacter());
            // GetFenCharacter() returns the FEN character for the given piece. White piece: uppercase, black piece: lowercase. The character is the first char of a piece's name (exception: Knight -> N/n because King -> K/k).
            // The Piece class is the abstract base class for pieces. All piece classes (e.g. Rook) derive from this class.

            // White has to make a turn. They want to move their E2 pawn to E4. Is that valid?
            var  e2e4    = new Move("E2", "E4", Player.White);
            bool isValid = game.IsValidMove(e2e4);

            Console.WriteLine("E2-E4 for white is valid: {0}", isValid);

            // Great, it's valid! So white wants to actually make that move.
            MoveType type = game.ApplyMove(e2e4, true);

            // The first argument is the move, the second argument indicates whether it's already validated. Here it is, so pass 'true'. If it's not validated yet, ApplyMove will do it. **Only pass `true` if it's really validated! If you pass `true`, ApplyMove won't do ANY validity checks.**
            // The return type is the MoveType enum. It holds one, or a combination, of these values: Invalid, Move, Capture, Castling, Promotion
            // Each valid move will always carry the 'Move' value. If it's also something else, it will carry both values (e.g. if the move is a capture, `type` will have the value MoveType.Move | MoveType.Capture).
            // MoveType is a flags enumeration. https://msdn.microsoft.com/en-us/library/ms229062%28v=vs.100%29.aspx
            // e4 is just a normal move, so `type` will just be MoveType.Move.
            Console.WriteLine("Move type: {0}", type);

            // ChessGame provides methods to check whether a player is in check, checkmated... Here is an example:
            Console.WriteLine("Black in check? {0}", game.IsInCheck(Player.Black));
            // Here IsInCheck returns 'false' because black is not in check.

            // Now it's black's turn.
            Console.WriteLine("It's this color's turn: {0}", game.WhoseTurn);

            // You can figure out all valid moves using GetValidMoves.
            IEnumerable <Move> validMoves = game.GetValidMoves(Player.Black);

            // Here it returns all valid moves for black, but you can also find all valid moves *from a certain position* by passing a Position instance as argument.
            Console.WriteLine("How many valid moves does black have? {0}", validMoves.Count());

            // It might happen that you don't really care about all valid moves, but just want to know if there are valid moves. Chess.NET also has a method for that:
            bool hasValidMoves = game.HasAnyValidMoves(Player.Black);

            // Again, you can also pass a Position instance here.
            Console.WriteLine("Black has any valid moves: {0}", hasValidMoves);

            // Congratulations! You have learned about the most important methods of Chess.NET. Enjoy using the library :)
            Console.ReadKey();
        }
Beispiel #34
0
    public bool MovePiece(string origin, string dest)
    {
        Move move = new Move(origin, dest, game.WhoseTurn);

        if (game.IsValidMove(move))
        {
            Debug.Log(game.WhoseTurn + " from " + origin + " to " + dest + " successful.");

            MoveType type = game.ApplyMove(move, true);

            GameObject pieceToMove = GetPieceAt(origin);
            pieceToMove.transform.Translate(Vector3.up * (boardPos[dest].position.z - pieceToMove.transform.position.z));
            pieceToMove.transform.Translate(Vector3.right * (boardPos[dest].position.x - pieceToMove.transform.position.x));
            return(true);
        }

        Debug.Log(game.WhoseTurn + " from " + origin + " to " + dest + " failed.");
        return(false);
    }
Beispiel #35
0
        public static void TestValidMoveBlackPawn_EnPassant()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.B, 1), new Position(File.A, 3), Player.White);
            Move move2 = new Move(new Position(File.E, 7), new Position(File.E, 5), Player.Black);
            Move move3 = new Move(new Position(File.E, 2), new Position(File.E, 3), Player.White);
            Move move4 = new Move(new Position(File.E, 5), new Position(File.E, 4), Player.Black);
            Move move5 = new Move(new Position(File.D, 2), new Position(File.D, 4), Player.White);
            Move move6 = new Move(new Position(File.E, 4), new Position(File.D, 3), Player.Black);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);
            cb.ApplyMove(move3, true);
            cb.ApplyMove(move4, true);
            cb.ApplyMove(move5, true);

            Assert.True(cb.IsValidMove(move6));
        }
Beispiel #36
0
        public static void TestInvalidMoveBlackPawn_EnPassant()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.B, Rank.One), new Position(File.A, Rank.Three), Player.White);
            Move move2 = new Move(new Position(File.E, Rank.Seven), new Position(File.E, Rank.Five), Player.Black);
            Move move3 = new Move(new Position(File.E, Rank.Two), new Position(File.E, Rank.Three), Player.White);
            Move move4 = new Move(new Position(File.E, Rank.Five), new Position(File.E, Rank.Four), Player.Black);
            Move move5 = new Move(new Position(File.H, Rank.Two), new Position(File.H, Rank.Four), Player.White);
            Move move6 = new Move(new Position(File.E, Rank.Four), new Position(File.D, Rank.Three), Player.Black);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);
            cb.ApplyMove(move3, true);
            cb.ApplyMove(move4, true);
            cb.ApplyMove(move5, true);

            Assert.False(cb.IsValidMove(move6));
        }
Beispiel #37
0
        public static void TestValidMoveBlackPawn_TwoSteps()
        {
            ChessGame cb = new ChessGame();
            cb.ApplyMove(new Move(new Position(File.A, Rank.Two), new Position(File.A, Rank.Three), Player.White), true);

            Move move1 = new Move(new Position(File.D, Rank.Seven), new Position(File.D, Rank.Five), Player.Black);

            Assert.True(cb.IsValidMove(move1), "move1 should be valid");
        }
Beispiel #38
0
        public static void TestValidMoveWhiteKingVertical()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.E, Rank.Two), new Position(File.E, Rank.Three), Player.White);
            Move move2 = new Move(new Position(File.A, Rank.Seven), new Position(File.A, Rank.Six), Player.Black);
            Move move3 = new Move(new Position(File.E, Rank.One), new Position(File.E, Rank.Two), Player.White);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);

            Assert.True(cb.IsValidMove(move3), "move3 should be valid");
        }
Beispiel #39
0
        public static void TestInvalidMoveWhitePawn_TwoStepsNotOnRankTwo()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.E, Rank.Two), new Position(File.E, Rank.Three), Player.White);
            Move move2 = new Move(new Position(File.E, Rank.Three), new Position(File.E, Rank.Five), Player.White);

            cb.ApplyMove(move1, true);

            Assert.False(cb.IsValidMove(move2), "move2 should be invalid");
        }
Beispiel #40
0
        public static void TestValidMoveWhitePawn_EnPassant()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.E, Rank.Two), new Position(File.E, Rank.Four), Player.White);
            Move move2 = new Move(new Position(File.E, Rank.Seven), new Position(File.E, Rank.Six), Player.Black);
            Move move3 = new Move(new Position(File.E, Rank.Four), new Position(File.E, Rank.Five), Player.White);
            Move move4 = new Move(new Position(File.D, Rank.Seven), new Position(File.D, Rank.Five), Player.Black);
            Move move5 = new Move(new Position(File.E, Rank.Five), new Position(File.D, Rank.Six), Player.White);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);
            cb.ApplyMove(move3, true);
            cb.ApplyMove(move4, true);

            Assert.True(cb.IsValidMove(move5));
        }
Beispiel #41
0
        public static void TestBlackCheckmated()
        {
            ChessGame cb = new ChessGame();
            cb.ApplyMove(new Move("E2", "E4", Player.White), true);
            cb.ApplyMove(new Move("E7", "E5", Player.Black), true);
            cb.ApplyMove(new Move("F1", "C4", Player.White), true);
            cb.ApplyMove(new Move("D7", "D6", Player.Black), true);
            cb.ApplyMove(new Move("D1", "F3", Player.White), true);
            cb.ApplyMove(new Move("H7", "H6", Player.Black), true);
            Assert.True(cb.ApplyMove(new Move("F3", "F7", Player.White), false));

            Assert.AreEqual(GameEvent.Checkmate, cb.Status.Event);
            Assert.AreEqual(Player.White, cb.Status.PlayerWhoCausedEvent);
            Assert.AreEqual("Black is checkmated", cb.Status.EventExplanation);
        }
Beispiel #42
0
        public static void TestValidMoveBlackKnight()
        {
            ChessGame cb = new ChessGame();
            cb.ApplyMove(new Move(new Position(File.A, Rank.Two), new Position(File.A, Rank.Three), Player.White), true);

            Move move1 = new Move(new Position(File.B, Rank.Eight), new Position(File.C, Rank.Six), Player.Black);

            Assert.True(cb.IsValidMove(move1), "move1 should be valid");
        }
Beispiel #43
0
        public static void TestValidMoveWhiteKingVertical()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.E, 2), new Position(File.E, 3), Player.White);
            Move move2 = new Move(new Position(File.A, 7), new Position(File.A, 6), Player.Black);
            Move move3 = new Move(new Position(File.E, 1), new Position(File.E, 2), Player.White);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);

            Assert.True(cb.IsValidMove(move3), "move3 should be valid");
        }
Beispiel #44
0
        public static void TestInvalidMoveWhiteKing_KingsideCastling_KingMoved()
        {
            Piece[][] board = new Piece[8][]
            {
                new[] { o, kb, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, kw, o, o, rw }
            };
            ChessGame cb = new ChessGame(board, Player.White);
            Move move1 = new Move(new Position(File.E, 1), new Position(File.D, 1), Player.White);
            Move move2 = new Move(new Position(File.B, 8), new Position(File.B, 7), Player.Black);
            Move move3 = new Move(new Position(File.D, 1), new Position(File.F, 1), Player.White);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);

            Assert.False(cb.IsValidMove(move3), "move3 should be invalid");
        }
Beispiel #45
0
        public static void TestValidMoveBlackPawn_2Steps()
        {
            ChessGame cb = new ChessGame();
            cb.ApplyMove(new Move(new Position(File.A, 2), new Position(File.A, 3), Player.White), true);

            Move move1 = new Move(new Position(File.D, 7), new Position(File.D, 5), Player.Black);

            Assert.True(cb.IsValidMove(move1), "move1 should be valid");
        }
Beispiel #46
0
        public static void TestApplyMoveBlackKing_QueensideCastling()
        {
            ChessPiece[][] board  = new ChessPiece[8][]
            {
                new[] { rb, o, o, o, kb, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, kw, o, o, o }
            };
            Move move = new Move(new Position(File.E, Rank.Eight), new Position(File.C, Rank.Eight), Player.Black);
            ChessGame cb = new ChessGame(board, Player.Black);
            cb.ApplyMove(move, true);

            ChessPiece[][] expected = new ChessPiece[8][]
            {
                new[] { o, o, kb, rb, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, kw, o, o, o }
            };

            Assert.AreEqual(expected, cb.GetBoard());
        }
Beispiel #47
0
        public static void TestGetValidMovesBlackStartingPosition()
        {
            ChessGame cb = new ChessGame();
            cb.ApplyMove(new Move(new Position(File.A, Rank.Two), new Position(File.A, Rank.Three), Player.White), true);
            ReadOnlyCollection<Move> actual = cb.GetValidMoves(Player.Black);
            List<Move> expected = new List<Move>()
            {
                new Move("A7", "A6", Player.Black),
                new Move("A7", "A5", Player.Black),
                new Move("B7", "B6", Player.Black),
                new Move("B7", "B5", Player.Black),
                new Move("C7", "C6", Player.Black),
                new Move("C7", "C5", Player.Black),
                new Move("D7", "D6", Player.Black),
                new Move("D7", "D5", Player.Black),
                new Move("E7", "E6", Player.Black),
                new Move("E7", "E5", Player.Black),
                new Move("F7", "F6", Player.Black),
                new Move("F7", "F5", Player.Black),
                new Move("G7", "G6", Player.Black),
                new Move("G7", "G5", Player.Black),
                new Move("H7", "H6", Player.Black),
                new Move("H7", "H5", Player.Black),
                new Move("B8", "A6", Player.Black),
                new Move("B8", "C6", Player.Black),
                new Move("G8", "F6", Player.Black),
                new Move("G8", "H6", Player.Black)
            };

            Assert.AreEqual(expected.Count, actual.Count);
            foreach (Move move in expected)
            {
                Assert.True(actual.Contains(move), "Actual does not contain " + move.ToString());
            }
        }
Beispiel #48
0
        public static void TestValidMoveBlackQueenDiagonal()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.A, Rank.Two), new Position(File.A, Rank.Three), Player.White);
            Move move2 = new Move(new Position(File.E, Rank.Seven), new Position(File.E, Rank.Six), Player.Black);
            Move move3 = new Move(new Position(File.H, Rank.Two), new Position(File.H, Rank.Three), Player.White);
            Move move4 = new Move(new Position(File.D, Rank.Eight), new Position(File.H, Rank.Four), Player.Black);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);
            cb.ApplyMove(move3, true);

            Assert.True(cb.IsValidMove(move4), "move4 should be valid");
        }
Beispiel #49
0
        public static void TestBlackNotStalematedAfterApplyMove()
        {
            ChessPiece[][] board  = new ChessPiece[8][]
            {
                new[] { o, o, kw, o, o, o, o, o },
                new[] { kb, o, qw, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o }
            };
            ChessGame cb = new ChessGame(board, Player.Black);

            Assert.True(cb.ApplyMove(new Move("A7", "A8", Player.Black), false));

            Assert.AreEqual(GameEvent.None, cb.Status.Event);
            Assert.AreEqual(Player.None, cb.Status.PlayerWhoCausedEvent);
            Assert.AreEqual("No special event", cb.Status.EventExplanation);
        }
Beispiel #50
0
        public static void TestValidMoveBlackKingHorizontal()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.A, Rank.Two), new Position(File.A, Rank.Three), Player.White);
            Move move2 = new Move(new Position(File.D, Rank.Seven), new Position(File.D, Rank.Six), Player.Black);
            Move move3 = new Move(new Position(File.H, Rank.Two), new Position(File.H, Rank.Three), Player.White);
            Move move4 = new Move(new Position(File.D, Rank.Eight), new Position(File.D, Rank.Seven), Player.Black);
            Move move5 = new Move(new Position(File.B, Rank.Two), new Position(File.B, Rank.Three), Player.White);
            Move move6 = new Move(new Position(File.E, Rank.Eight), new Position(File.D, Rank.Eight), Player.Black);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);
            cb.ApplyMove(move3, true);
            cb.ApplyMove(move4, true);
            cb.ApplyMove(move5, true);

            Assert.True(cb.IsValidMove(move6), "move6 should be valid");
        }
Beispiel #51
0
        public static void TestInvalidMoveWhitePawn_NoCapture()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.E, Rank.Two), new Position(File.E, Rank.Four), Player.White);
            Move move2 = new Move(new Position(File.D, Rank.Seven), new Position(File.D, Rank.Six), Player.Black);
            Move move3 = new Move(new Position(File.E, Rank.Four), new Position(File.D, Rank.Five), Player.White);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);

            Assert.False(cb.IsValidMove(move3));
        }
Beispiel #52
0
 public static void TestApplyMoveWhitePawn()
 {
     ChessGame cb = new ChessGame();
     Move move1 = new Move(new Position(File.E, Rank.Two), new Position(File.E, Rank.Three), Player.White);
     Assert.True(cb.ApplyMove(move1, false));
     ChessPiece[][] expected = new ChessPiece[8][]
     {
         new[] { rb, nb, bb, qb, kb, bb, nb, rb },
         new[] { pb, pb, pb, pb, pb, pb, pb, pb },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, pw, o, o, o },
         new[] { pw, pw, pw, pw, o, pw, pw, pw },
         new[] { rw, nw, bw, qw, kw, bw, nw, rw }
     };
     Assert.AreEqual(expected, cb.GetBoard(), "Unexpected board layout after applying move1");
     Move move2 = new Move(new Position(File.E, Rank.Three), new Position(File.E, Rank.Four), Player.White);
     Assert.True(cb.ApplyMove(move2, true));
     expected = new ChessPiece[8][]
     {
         new[] { rb, nb, bb, qb, kb, bb, nb, rb },
         new[] { pb, pb, pb, pb, pb, pb, pb, pb },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, pw, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { pw, pw, pw, pw, o, pw, pw, pw },
         new[] { rw, nw, bw, qw, kw, bw, nw, rw }
     };
     Assert.AreEqual(expected, cb.GetBoard(), "Unexpected board layout after applying move2");
 }
Beispiel #53
0
        public static void TestValidMoveWhiteQueenHorizontal()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.B, Rank.One), new Position(File.C, Rank.Three), Player.White);
            Move move2 = new Move(new Position(File.A, Rank.Seven), new Position(File.A, Rank.Six), Player.Black);
            Move move3 = new Move(new Position(File.D, Rank.Two), new Position(File.D, Rank.Three), Player.White);
            Move move4 = new Move(new Position(File.B, Rank.Seven), new Position(File.B, Rank.Six), Player.Black);
            Move move5 = new Move(new Position(File.C, Rank.One), new Position(File.D, Rank.Two), Player.White);
            Move move6 = new Move(new Position(File.C, Rank.Seven), new Position(File.C, Rank.Six), Player.Black);
            Move move7 = new Move(new Position(File.D, Rank.One), new Position(File.C, Rank.One), Player.White);
            Move move8 = new Move(new Position(File.D, Rank.One), new Position(File.B, Rank.One), Player.White);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);
            cb.ApplyMove(move3, true);
            cb.ApplyMove(move4, true);
            cb.ApplyMove(move5, true);
            cb.ApplyMove(move6, true);

            Assert.True(cb.IsValidMove(move7), "move7 should be valid");
            Assert.True(cb.IsValidMove(move8), "move8 should be valid");
        }
Beispiel #54
0
        public static void TestApplyMoveWhitePawn_PromotionToQueen()
        {
            ChessPiece[][] board  = new ChessPiece[8][]
            {
                new[] { o, o, o, o, o, o, o, o },
                new[] { pw, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, kw, o, kb, o, o },
                new[] { o, o, o, o, o, o, o, o }
            };
            Move move = new Move(new Position(File.A, Rank.Seven), new Position(File.A, Rank.Eight), Player.White, Piece.Queen);
            ChessGame cb = new ChessGame(board, Player.White);
            Assert.True(cb.ApplyMove(move, false), "move should be valid");

            ChessPiece[][] expected = new ChessPiece[8][]
            {
                new[] { qw, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, kw, o, kb, o, o },
                new[] { o, o, o, o, o, o, o, o }
            };
            Assert.AreEqual(expected, cb.GetBoard());
        }
Beispiel #55
0
        public static void TestInvalidMoveWhiteKing_QueensideCastling_KingMoved()
        {
            ChessPiece[][] board  = new ChessPiece[8][]
            {
                new[] { o, kb, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { rw, o, o, o, kw, o, o, o }
            };
            ChessGame cb = new ChessGame(board, Player.White);
            Move move1 = new Move(new Position(File.E, Rank.One), new Position(File.D, Rank.One), Player.White);
            Move move2 = new Move(new Position(File.B, Rank.Eight), new Position(File.B, Rank.Seven), Player.Black);
            Move move3 = new Move(new Position(File.D, Rank.One), new Position(File.B, Rank.One), Player.White);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);

            Assert.False(cb.IsValidMove(move3), "move3 should be invalid");
        }
Beispiel #56
0
        public static void TestApplyMoveBlackPawn_PromotionToQueen()
        {
            ChessPiece[][] board  = new ChessPiece[8][]
            {
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, kw, o, kb, pb, o },
                new[] { o, o, o, o, o, o, o, o }
            };
            Move move = new Move(new Position(File.G, Rank.Two), new Position(File.G, Rank.One), Player.Black, Piece.Queen);
            ChessGame cb = new ChessGame(board, Player.Black);
            Assert.True(cb.ApplyMove(move, false), "move should be valid");

            ChessPiece[][] expected = new ChessPiece[8][]
            {
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, kw, o, kb, o, o },
                new[] { o, o, o, o, o, o, qb, o }
            };
            Assert.AreEqual(expected, cb.GetBoard());
        }
Beispiel #57
0
        public static void TestValidMoveBlackPawn_Capture()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.A, Rank.Two), new Position(File.A, Rank.Three), Player.White);
            Move move2 = new Move(new Position(File.E, Rank.Seven), new Position(File.E, Rank.Five), Player.Black);
            Move move3 = new Move(new Position(File.D, Rank.Two), new Position(File.D, Rank.Four), Player.White);
            Move move4 = new Move(new Position(File.E, Rank.Five), new Position(File.D, Rank.Four), Player.Black);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);
            cb.ApplyMove(move3, true);

            Assert.True(cb.IsValidMove(move4));
        }
Beispiel #58
0
        public static void TestApplyMoveBlackPawn_EnPassant()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.B, Rank.One), new Position(File.A, Rank.Three), Player.White);
            Move move2 = new Move(new Position(File.E, Rank.Seven), new Position(File.E, Rank.Five), Player.Black);
            Move move3 = new Move(new Position(File.E, Rank.Two), new Position(File.E, Rank.Three), Player.White);
            Move move4 = new Move(new Position(File.E, Rank.Five), new Position(File.E, Rank.Four), Player.Black);
            Move move5 = new Move(new Position(File.D, Rank.Two), new Position(File.D, Rank.Four), Player.White);
            Move move6 = new Move(new Position(File.E, Rank.Four), new Position(File.D, Rank.Three), Player.Black);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);
            cb.ApplyMove(move3, true);
            cb.ApplyMove(move4, true);
            cb.ApplyMove(move5, true);
            cb.ApplyMove(move6, true);

            ChessPiece[][] board  = new ChessPiece[8][]
            {
                new[] { rb, nb, bb, qb, kb, bb, nb, rb },
                new[] { pb, pb, pb, pb, o, pb, pb, pb },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { nw, o, o, pb, pw, o, o, o },
                new[] { pw, pw, pw, o, o, pw, pw, pw },
                new[] { rw, o, bw, qw, kw, bw, nw, rw }
            };

            Assert.AreEqual(board, cb.GetBoard(), "Unexpected board layout after en passant capture.");
        }
Beispiel #59
0
        public static void TestInvalidMoveBlackPawn_TwoStepsNotOnRankSeven()
        {
            ChessGame cb = new ChessGame();
            Move move1 = new Move(new Position(File.A, Rank.Two), new Position(File.A, Rank.Three), Player.White);
            Move move2 = new Move(new Position(File.E, Rank.Seven), new Position(File.E, Rank.Six), Player.Black);
            Move move3 = new Move(new Position(File.H, Rank.Two), new Position(File.H, Rank.Three), Player.White);
            Move move4 = new Move(new Position(File.E, Rank.Six), new Position(File.E, Rank.Four), Player.Black);

            cb.ApplyMove(move1, true);
            cb.ApplyMove(move2, true);
            cb.ApplyMove(move3, true);

            Assert.False(cb.IsValidMove(move4), "move4 should be invalid");
        }
Beispiel #60
0
        public static void TestApplyMoveWhiteKing_KingsideCastling()
        {
            ChessPiece[][] board  = new ChessPiece[8][]
            {
                new[] { o, o, o, o, kb, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, kw, o, o, rw }
            };
            Move move = new Move(new Position(File.E, Rank.One), new Position(File.G, Rank.One), Player.White);
            ChessGame cb = new ChessGame(board, Player.White);
            cb.ApplyMove(move, true);

            ChessPiece[][] expected = new ChessPiece[8][]
            {
                new[] { o, o, o, o, kb, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, rw, kw, o }
            };

            Assert.AreEqual(expected, cb.GetBoard());
        }