Пример #1
0
        public PossibleMovesDTO GetPossibleMoves(SelectedPieceDTO data)
        {
            ChessBoard board    = new ChessBoard(data.Board);
            Point      selected = Utils.StringToPoint(data.Selected);
            Color      player   = Utils.CharToColor(data.PlayerColor);

            return(Logic.GetPossibleMoves(board, selected, player));
        }
Пример #2
0
        // public IActionResult ClickedTile(string clickedTile, string gameId, string selectedTile, List<string> canMove, List<string> canTake)
        public IActionResult ClickedTile([FromBody] TileClick data)
        {
            var gameId       = data.GameID;
            var clickedTile  = data.ClickedTile;
            var selectedTile = data.SelectedTile;
            var canMove      = data.CanMove;
            var canTake      = data.CanTake;
            var didMove      = false;

            List <string> changedTiles = new List <string>();

            changedTiles.AddRange(canMove);
            changedTiles.AddRange(canTake);

            string userId = _signInManager.UserManager.GetUserId(User);

            RefreshUser(User);
            GameStateDTO gamestate = _GameRepo.GetGamestate(gameId);
            char         userColor = _GameRepo.GetPlayerColor(gameId, userId);

            if (!_GameRepo.IsActivePlayer(gameId, userId))
            {
                // Not players turn
                return(Json(""));
            }

            else if (selectedTile == clickedTile)
            {
                // Clicked already selected tile. Deselect it.
                changedTiles.Add(selectedTile);
                selectedTile = null;
                canMove      = new List <string>();
                canTake      = new List <string>();
            }

            else if (_chessLogicManager.PositionIsColor(gamestate.Board, clickedTile, userColor))
            {
                // Clicked own piece. Get possible moves.
                SelectedPieceDTO selectedPiece = new SelectedPieceDTO {
                    Board       = gamestate.Board,
                    PlayerColor = userColor,
                    Selected    = clickedTile
                };
                var moves = _chessLogicManager.GetPossibleMoves(selectedPiece);
                canMove = moves.PositionsPieceCanMoveTo;
                canTake = moves.PositionsPieceCanKillAt;
                changedTiles.AddRange(canMove);
                changedTiles.AddRange(canTake);
                if (selectedTile != null)
                {
                    changedTiles.Add(selectedTile);
                }
                changedTiles.Add(clickedTile);
                selectedTile = clickedTile;
            }

            else if (selectedTile != null)
            {
                //a tile is already selected, and clicked an enemy or empty tile
                //Can the selected piece move there?
                MovePlanDTO movePlan = new MovePlanDTO
                {
                    Board       = gamestate.Board,
                    From        = selectedTile,
                    To          = clickedTile,
                    PlayerColor = userColor
                };

                changedTiles.Add(clickedTile);
                changedTiles.Add(selectedTile);
                if (_chessLogicManager.IsValidMove(movePlan))
                {
                    //yes, the selected piece can move there
                    //Apply the game logic, save the new board state, and log the move.
                    string[,] newBoard = _chessLogicManager.DoMove(movePlan);
                    NewMoveDTO newMove = new NewMoveDTO
                    {
                        NewBoard = newBoard,
                        From     = selectedTile,
                        To       = clickedTile,
                        GameID   = gameId
                    };
                    _GameRepo.AddNewMove(newMove);
                    _GameRepo.SetChangedTiles(gameId, new List <string> {
                        clickedTile, selectedTile
                    });
                    didMove = true;

                    if (_chessLogicManager.IsBlackCheckMate(newBoard) ||
                        _chessLogicManager.IsWhiteCheckMate(newBoard))
                    {
                        _GameRepo.SetGameToFinished(gameId);
                        _GameRepo.SetGameToFinished(gameId);
                    }
                }
                // Either a move was made, or the tile was deselected by clicking
                // a tile where a move wasn't possible. Remove highlights.
                canMove      = new List <string>();
                canTake      = new List <string>();
                selectedTile = null;
            }
            gamestate = _GameRepo.GetGamestate(gameId);

            ChessboardPartialViewModel board = new ChessboardPartialViewModel
            {
                GameState             = gamestate,
                SelectedTile          = selectedTile,
                CanMoveToAndTakeTiles = canTake,
                CanMoveToTiles        = canMove,
                GameId       = gameId,
                PlayerColor  = userColor,
                ChangedTiles = changedTiles,
                DidMove      = didMove
            };

            return(Json(board));
        }