Ejemplo n.º 1
0
        public object Put()
        {
            var chessBoard = new ChessBoard();
            (new TraditionalBoardStager()).Stage(chessBoard);

            var chessGame = new ChessGame(
                chessBoard,
                PlayerTypeEnum.White,
                new Dictionary<PlayerTypeEnum, List<ChessPiece>>());

            var id = Guid.NewGuid();
            m_chessGameRepo.Put(id, chessGame);

            return new
            {
                _id = id,
                _turn = ((char)chessGame.PlayerTurn),
                _taken = new
                {
                    w = chessGame.GetTakenPiecesByPlayer(PlayerTypeEnum.White).Select(x => (char)x.Type),
                    b = chessGame.GetTakenPiecesByPlayer(PlayerTypeEnum.Black).Select(x => (char)x.Type),
                },
                _state = chessBoard.GetBoardAsListOfString(),
                _history = chessGame.GetMoves(),
                _moves = new PotentialMoveService().GetPotentialMoves(chessGame, chessGame.PlayerTurn),
            };
        }
Ejemplo n.º 2
0
        private List<string> _GetPotentialMoves(ChessGame g, PlayerTypeEnum player, string source)
        {
            List<string> potentialMoves = new List<string>();

            var chessboard = g.ChessBoard;
            ChessPiece sourcePiece = chessboard.Get(source);
            Tuple<int, int> sourceLocation = NotationHelper.Translate(source);
            var relativeMoveProvider = m_relativePathProviderFactory.GetProviderByPieceType(sourcePiece.Type);
            var relativePaths = relativeMoveProvider.GetRelativePaths(sourcePiece);
            var absolutePaths = BoardLogic.ConvertToAbsolutePaths(sourceLocation, relativePaths);

            foreach (Tuple<int, int>[] absolutePath in absolutePaths)
            {
                foreach (Tuple<int, int> absoluteMove in absolutePath)
                {
                    string destinationAsNotation = NotationHelper.Translate(absoluteMove);

                    var canMove = _rules.CanMove(
                        chessboard,
                        source + destinationAsNotation, player, g.GetMoves().LastOrDefault());

                    if (canMove.Success)
                    {
                        potentialMoves.Add(destinationAsNotation);
                    }
                }
            }

            return potentialMoves;
        }
Ejemplo n.º 3
0
        public void King_Two_Move_No_Rook()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var move = g.PlayerMove(PlayerTypeEnum.White, "e1g1");

            Assert.IsFalse(move.Success);
        }
Ejemplo n.º 4
0
        public Dictionary<string, List<string>> GetPotentialMoves(ChessGame g, PlayerTypeEnum player)
        {
            Dictionary<string, List<string>> movesBySquare = new Dictionary<string, List<string>>();

            var chessBoard = g.ChessBoard;
            List<string> sourceSquares = chessBoard.GetPiecesForPlayer(player);
            
            foreach (var sourceSquare in sourceSquares)
            {
                var potentialMoves = _GetPotentialMoves(g, player, sourceSquare);
                if (potentialMoves.Count > 0) movesBySquare[sourceSquare] = potentialMoves;
            }

            return movesBySquare;
        }
Ejemplo n.º 5
0
        public void PawnSwap()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("f7", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "f7f8");
            ChessPiece queen = b.Get("f8");

            Assert.IsTrue(moveResult.Success);
            Assert.IsTrue(queen.Type == PieceTypeEnum.Queen);
        }
Ejemplo n.º 6
0
        public void PotentialMoves()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("f7", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);

            PotentialMoveService s = new PotentialMoveService();
            s.GetPotentialMoves(g, PlayerTypeEnum.White);
            var whiteMoves = s.GetPotentialMoves(g, PlayerTypeEnum.White);
            var blackMoves = s.GetPotentialMoves(g, PlayerTypeEnum.Black);

            Assert.AreEqual(5, whiteMoves["e1"].Count);
            Assert.AreEqual(2, whiteMoves["f7"].Count);

            Assert.AreEqual(5, blackMoves["e8"].Count);
        }
Ejemplo n.º 7
0
        public void Castle()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("a1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Rook });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1c1");

            Assert.IsTrue(moveResult.Success);
            Assert.IsNotNull(moveResult.Moves);
            Assert.AreEqual(2, moveResult.Moves.Length);
            Assert.AreEqual("e1c1", moveResult.Moves[0]);
            Assert.AreEqual("a1d1", moveResult.Moves[1]);
            Assert.IsNotNull(g.ChessBoard.Get("d1"));
            Assert.AreEqual(PieceTypeEnum.Rook, g.ChessBoard.Get("d1").Type);
        }
Ejemplo n.º 8
0
        static void RandomIntoCheck()
        {
            Random r = new Random(Guid.NewGuid().GetHashCode());

            ChessBoard chessBoard = new ChessBoard();
            (new TraditionalBoardStager()).Stage(chessBoard);

            PlayerTypeEnum currentPlayer = PlayerTypeEnum.White;
            ChessGame chessGame = new ChessGame(
                chessBoard,
                currentPlayer,
                new Dictionary<PlayerTypeEnum, List<ChessPiece>>());

            bool isCheckmate = false;
            while (true)
            {
                int movesWithoutSuccess = 0;

                while (true)
                {
                    List<string> notation = chessBoard.GetPiecesForPlayer(currentPlayer);
                    List<string> opponentNotation =
                        chessBoard.GetPiecesForPlayer(currentPlayer == PlayerTypeEnum.White
                            ? PlayerTypeEnum.Black
                            : PlayerTypeEnum.White);

                    if ((notation.Count == 1 && opponentNotation.Count == 1) || movesWithoutSuccess > 5000)
                    {
                        //Console.WriteLine("{0} on {1}, moves w/o success: {2}", notation.Count, notation.Count, movesWithoutSuccess);

                        chessBoard = new ChessBoard();
                        (new TraditionalBoardStager()).Stage(chessBoard);

                        currentPlayer = PlayerTypeEnum.White;
                        chessGame = new ChessGame(
                            chessBoard,
                            currentPlayer,
                            new Dictionary<PlayerTypeEnum, List<ChessPiece>>());
                    }

                    int randomPieceIndex = r.Next(0, notation.Count);
                    string randomSource = notation.ElementAt(randomPieceIndex);

                    int randomRank = r.Next(0, 8);
                    int randomFile = r.Next(0, 8);

                    string randomDestination = NotationHelper.Translate(randomRank, randomFile);
                    var move = chessGame.PlayerMove(currentPlayer, randomSource + randomDestination);

                    if (move.Success)
                    {
                        movesWithoutSuccess = 0;

                        if (move.MoveType == MoveTypeEnum.Checkmate)
                        {
                            (new ConsoleBoardPrinter()).PrintBoard(chessBoard.CreateStateCopy(), currentPlayer);
                            Console.WriteLine("Check: {0}, Moves: {1}", Enum.GetName(typeof(MoveTypeEnum), move.MoveType), string.Join(", ", move.Moves));
                        }

                        currentPlayer = move.Turn;

                        if (move.MoveType == MoveTypeEnum.Checkmate)
                        {
                            isCheckmate = true;
                        }

                        break;
                    }

                    ++movesWithoutSuccess;
                }

                if (isCheckmate)
                {
                    break;
                }
            }
        }
Ejemplo n.º 9
0
        public void White_Puts_Black_In_Check()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("d1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Queen });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "d1a4");

            Assert.IsTrue(moveResult.Success);
            Assert.IsTrue(moveResult.MoveType == MoveTypeEnum.Check);
        }
Ejemplo n.º 10
0
        public void Cannot_Move_Into_Check()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });
            b.Set("h2", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Queen });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1e2");

            Assert.IsFalse(moveResult.Success);
        }
Ejemplo n.º 11
0
        public void Double_Check()
        {
            ChessBoard b = new ChessBoard();
            b.Set("h1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("h4", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Rook });
            b.Set("h5", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });
            b.Set("f4", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Bishop });

            b.Set("h6", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });
            b.Set("g7", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Pawn });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.Black, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.Black, "g7g5");
            Assert.IsTrue(moveResult.Success);

            moveResult = g.PlayerMove(PlayerTypeEnum.White, "h5g6");
            Assert.IsTrue(moveResult.Success);
        }
Ejemplo n.º 12
0
        public void Checkmate()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });
            b.Set("h2", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Queen });
            b.Set("g2", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Rook });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.Black, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.Black, "h2h1");

            Assert.IsTrue(moveResult.Success);
            Assert.IsTrue(moveResult.MoveType == MoveTypeEnum.Checkmate, "expected checkmate");
        }
Ejemplo n.º 13
0
        public void Castle_Block()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("a1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Rook });
            b.Set("b1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Knight });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1c1");

            Assert.IsFalse(moveResult.Success);
        }
Ejemplo n.º 14
0
        public void Cannot_Castle_Out_Of_Check()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("a1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Rook });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Queen });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1c1");

            Assert.IsFalse(moveResult.Success);

            moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1f1");

            Assert.IsTrue(moveResult.Success);
        }
Ejemplo n.º 15
0
 public void Put(Guid id, ChessGame g)
 {
     StaticCache[id] = g;
 }
Ejemplo n.º 16
0
        public void Overtake()
        {
            ChessBoard b = new ChessBoard();
            b.Set("g4", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });
            b.Set("h5", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Pawn });

            TraditionalRulesOfChess rules = new TraditionalRulesOfChess();

            ChessGame chessGame = new ChessGame(
                b,
                PlayerTypeEnum.White,
                null);

            var move = chessGame.PlayerMove(PlayerTypeEnum.White, "g4h5");

            Assert.IsTrue(move.Success, move.Error);
            Assert.IsNotNull(move.Taken);
            Assert.AreEqual(chessGame.GetTakenPiecesByPlayer(PlayerTypeEnum.White).Length, 1);
            Assert.AreEqual(chessGame.GetTakenPiecesByPlayer(PlayerTypeEnum.White)[0].Player, PlayerTypeEnum.Black);
        }
Ejemplo n.º 17
0
        static void InteractiveConsole()
        {
            ChessBoard chessBoard = new ChessBoard();
            //(new TraditionalBoardStager()).Stage(chessBoard);
            //(new RookTestingBoardStager()).Stage(chessBoard);
            //(new BishopTestingBoardStager()).Stage(chessBoard);
            //(new QueenTestingBoardStager()).Stage(chessBoard);
            //(new EnPassantTestingBoardStager()).Stage(chessBoard);
            //(new PawnSwapBoardStager()).Stage(chessBoard);
            (new KnightTestingBoardStager()).Stage(chessBoard);

            ChessGame chessGame = new ChessGame(
                chessBoard,
                PlayerTypeEnum.White,
                new Dictionary<PlayerTypeEnum, List<ChessPiece>>());

            ChessPiece[,] state = chessBoard.CreateStateCopy();
            (new ConsoleBoardPrinter()).PrintBoard(state, PlayerTypeEnum.White);

            PlayerTypeEnum playerTurn = PlayerTypeEnum.White;

            Console.WriteLine("Input: 1 square [a2] to show possible moves; 2 square [a1b1] to move");

            string move;
            Console.Write("> ");
            while (!string.IsNullOrWhiteSpace(move = Console.ReadLine()))
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(move))
                        break;

                    if (move.Length == 2)
                    {
                        var cpyb = new ChessBoard(chessBoard.CreateStateCopy());

                        var pm = (new PotentialMoveService()).GetPotentialMoves(chessGame, playerTurn);

                        List<string> moves;
                        pm.TryGetValue(move, out moves);
                        moves = moves ?? new List<string>();

                        (new ConsoleBoardPrinter()).PrintBoard(state, PlayerTypeEnum.White, new HashSet<string>(moves));
                    }
                    else if (move.Length == 4)
                    {
                        var moveResult = chessGame.PlayerMove(playerTurn, move);
                        playerTurn = moveResult.Turn;

                        state = chessBoard.CreateStateCopy();

                        (new ConsoleBoardPrinter()).PrintBoard(state, PlayerTypeEnum.White);
                        Console.WriteLine("{0} - {1} - {2} - {3} - {4}", moveResult.Success, moveResult.Error, moveResult.Taken, moveResult.MoveType, moveResult.Turn);
                    }
                    else
                        Console.WriteLine("Not implemented.");

                    Console.Write("> ");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }