예제 #1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get",
                         Route = "ChessGame/{board?}/{move?}")] HttpRequest req,
            string board, string move,
            ILogger log)
        {
            var game = await Task.Run(() =>
            {
                if (string.IsNullOrEmpty(board))
                {
                    return(new ChessGame(_boardEngineProvider, _checkDetectionService, _boardSetup, Colours.White));
                }

                return(ChessGameConvert.Deserialise(board));
            });

            string moveResult = string.Empty;

            if (!string.IsNullOrEmpty(move))
            {
                moveResult = game.Move(move);
            }

            var items = game.BoardState.GetItems((int)game.CurrentPlayer).ToArray();

            return(new JsonResult(new ChessWebApiResult(game, game.CurrentPlayer, moveResult ?? "", items)));
        }
        private void RefreshPathsFeature(IBoardState <ChessPieceEntity> boardState, Colours currentPlayer)
        {
            if (FeatureFlags.CachingPaths)
            {
                // Need proper boardstate key I think, currently a few tests fail, I guess around some state related
                // so something not encoded in the textboard (enpassant  and castle viability namely)
                var stateKey = ChessGameConvert.SerialiseBoard(boardState);
                if (_stateCache.TryGetValue(stateKey, out var items))
                {
                    boardState.UpdatePaths(items);
                }
                else
                {
                    RefreshChessPaths(boardState, currentPlayer);

                    if (FeatureFlags.CachingPaths)
                    {
                        _stateCache.Add(stateKey, boardState.GetItems().ToArray());
                    }
                }
            }
            else
            {
                RefreshChessPaths(boardState, currentPlayer);
            }
        }
예제 #3
0
        public ChessWebApiResult GetMovesForPlayer(string board, Colours forPlayer)
        {
            var game  = ChessGameConvert.Deserialise(board);
            var items = game.BoardState.GetItems((int)forPlayer);

            return(new ChessWebApiResult(game, forPlayer, string.Empty, items.ToArray()));
        }
예제 #4
0
        public ChessWebApiResult GetMoves(string board)
        {
            var game  = ChessGameConvert.Deserialise(board);
            var items = game.BoardState.GetItems((int)game.CurrentPlayer);

            return(new ChessWebApiResult(game, game.CurrentPlayer, string.Empty, items.ToArray()));
        }
예제 #5
0
        public ChessWebApiResult GetMovesForLocation(string board, string location)
        {
            var game = ChessGameConvert.Deserialise(board);
            var loc  = location.ToBoardLocation();
            var item = game.BoardState.GetItem(loc);

            return(new ChessWebApiResult(game, item.Item.Player, string.Empty, item));
        }
예제 #6
0
        public ChessWebApiResult PlayMove(string board, string move)
        {
            var game = ChessGameConvert.Deserialise(board);
            var msg  = game.Move(move);

            return(new ChessWebApiResult(
                       game,
                       game.CurrentPlayer,
                       msg,
                       game.BoardState.GetItems((int)game.CurrentPlayer).ToArray()
                       ));
        }
예제 #7
0
 public ChessWebApiResult(
     ChessGame game,
     Colours toMove,
     string message,
     params LocatedItem <ChessPieceEntity>[] items
     )
 {
     Game           = game;
     Board          = ChessGameConvert.Serialise(game);
     BoardText      = new ChessBoardBuilder().FromChessGame(game).ToTextBoard();
     Moves          = items.SelectMany(i => i.Paths.FlattenMoves());
     AvailableMoves = ToMoveList(items);
     WhoseTurn      = toMove.ToString();
     Message        = message;
 }
예제 #8
0
 public Task <ChessWebApiResult> PlayMoveAsync(string board, string move)
 {
     return(Task.Run(() =>
     {
         var game = ChessGameConvert.Deserialise(board);
         var msg = game.Move(move);
         var items = game.BoardState.GetItems((int)game.CurrentPlayer).ToArray();
         var result = new ChessWebApiResult
         {
             Board = ChessGameConvert.Serialise(game),
             BoardText = new ChessBoardBuilder().FromChessGame(game).ToTextBoard(),
             AvailableMoves = ToMoveList(game, items),
             WhoseTurn = game.CurrentPlayer.ToString(),
             Message = msg
         };
         return result;
     }));
 }
        public void Should_serialise_and_deserialise_to_68char_format()
        {
            var chessGame      = ChessFactory.NewChessGame();
            var actualNewBoard = ChessGameConvert.Serialise(chessGame);

            var expectedNewBoard = "rnbqkbnrpppppppp................................PPPPPPPPRNBQKBNR" // The board
                                   + "W"                                                              // Whose turn
                                   + "0000";                                                          // White Queen/King Black Queen/King castle availability

            Assert.That(actualNewBoard, Is.EqualTo(expectedNewBoard));

            var actualGame = ChessGameConvert.Deserialise(actualNewBoard);

            var actualGameAsString   = new ChessBoardBuilder().FromChessGame(actualGame).ToTextBoard();
            var expectedGameAsString = new ChessBoardBuilder().FromChessGame(chessGame).ToTextBoard();

            Assert.That(actualGameAsString, Is.EqualTo(expectedGameAsString));
            Console.WriteLine(actualGameAsString);
        }