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;
        }