Esempio n. 1
0
        public IActionResult Moves(Guid gameId, [FromBody] UserInputForMovesPost userInput)
        {
            var move = KeyPressedToShift(userInput.KeyPressed);
            var game = TestData.AGameDto(move);

            return(new ObjectResult(game));
        }
Esempio n. 2
0
        private static Direction?ParseClick(UserInputForMovesPost userInput, SokobanField game)
        {
            var playerPos  = game.PlayerPos;
            var clickedPos = userInput.ClickedPos;
            // Swicth playerPos.Y and clickedPos.Y because (0,0) in upper left corner
            var playerToClickVec = new Vec(clickedPos.X - playerPos.X, playerPos.Y - clickedPos.Y);

            if (playerToClickVec.X == 0 && playerToClickVec.Y == 0)
            {
                return(null);
            }
            var radian = Math.Atan2(playerToClickVec.Y, playerToClickVec.X);

            radian += radian < 0 ? 2 * Math.PI : 0;
            if (radian < Math.PI / 4)
            {
                return(Direction.Right);
            }
            else if (radian < 3 * Math.PI / 4)
            {
                return(Direction.Up);
            }
            else if (radian < 5 * Math.PI / 4)
            {
                return(Direction.Left);
            }
            else if (radian < 7 * Math.PI / 4)
            {
                return(Direction.Down);
            }
            else
            {
                return(Direction.Right);
            }
        }
Esempio n. 3
0
        public static Direction?GetDirection(UserInputForMovesPost userInput, SokobanField game)
        {
            if (!IsCorrectInput(userInput))
            {
                return(null);
            }
            if (userInput.ClickedPos == null)
            {
                switch ((int)userInput.KeyPressed)
                {
                case 37:
                    return(Direction.Left);

                case 39:
                    return(Direction.Right);

                case 38:
                    return(Direction.Up);

                case 40:
                    return(Direction.Down);

                default:
                    return(null);
                }
            }
            else
            {
                return(ParseClick(userInput, game));
            }
        }
Esempio n. 4
0
        private static bool IsCorrectInput(UserInputForMovesPost userInput)
        {
            var isKeyboardInput = (userInput.KeyPressed >= 37 || userInput.KeyPressed <= 40) &&
                                  userInput.ClickedPos == null;
            var isClickInput = userInput.ClickedPos != null;

            return(isKeyboardInput || isClickInput);
        }
Esempio n. 5
0
        public IActionResult Moves(Guid gameId, [FromBody] UserInputForMovesPost userInput)
        {
            var game = TestData.AGameDto(userInput.ClickedPos ?? new Vec(1, 1));

            if (userInput.ClickedPos != null)
            {
                game.Cells.First(c => c.Type == "color4").Pos = userInput.ClickedPos;
            }
            return(new ObjectResult(game));
        }
Esempio n. 6
0
        public IActionResult Moves(Guid gameId, [FromBody] UserInputForMovesPost userInput)
        {
            var directionRaw = userInput.KeyPressed;
            var game         = repo.GetGame();

            if (!directions.ContainsKey(directionRaw))
            {
                return(new ObjectResult(game.ToDto()));
            }

            var direction = directions[directionRaw];

            game.UpdateBoard(direction);
            repo.SaveGame(game);

            return(new ObjectResult(game.ToDto()));
        }
Esempio n. 7
0
        public Vec GetMovement(UserInputForMovesPost input)
        {
            switch ((KeyPress)input.KeyPressed)
            {
            case KeyPress.KEY_DOWN:
                return(new Vec(0, 1));

            case KeyPress.KEY_LEFT:
                return(new Vec(-1, 0));

            case KeyPress.KEY_RIGHT:
                return(new Vec(1, 0));

            case KeyPress.KEY_UP:
                return(new Vec(0, -1));
            }

            return(new Vec(0, 0));
        }
Esempio n. 8
0
        public IActionResult Click(Guid gameId, [FromBody] UserInputForMovesPost userInput)
        {
            var game       = GamesRepo.Games[gameId];
            var maxColorId = -1;

            if (userInput.KeyPressed.ToString().ToUpper() == "I")
            {
                var palette  = game.Palette;
                var maxCount = -1;

                for (var i = 0; i < palette.Count; i++)
                {
                    var prevContentState = (int[, ])game.Content.Clone();
                    ReColourCells(game, i);
                    var newCount = GetAdjacentCells(game, i).Count;
                    if (newCount > maxCount)
                    {
                        maxCount   = newCount;
                        maxColorId = i;
                    }
                    game.SetContent(prevContentState);
                }
            }
            else
            {
                if (userInput.ClickedPos == null)
                {
                    return(new ObjectResult(game.GetDto()));
                }
                maxColorId = game.Content[userInput.ClickedPos.X, userInput.ClickedPos.Y];
            }
            var adjacentCellCount = ReColourCells(game, maxColorId);

            game.Score = adjacentCellCount;
            var result = game.GetDto();

            result.IsFinished = adjacentCellCount == game.SizeX * game.SizeY;
            return(new ObjectResult(result));

            //return new ObjectResult(
            //    ReColourCells(game, maxColorId));
        }
Esempio n. 9
0
        public IActionResult Moves(Guid gameId, [FromBody] UserInputForMovesPost userInput)
        {
            if (!GamesRepo.Instance.ContainsGame(gameId))
            {
                return(new BadRequestObjectResult("game id is bad"));
            }
            var game = GamesRepo.Instance.GetGame(gameId);
            var dir  = InputParser.GetDirection(userInput, game);

            if (dir is Game.Direction direction)
            {
                game.Move(direction);
            }
            var gameDto = new GameDto(GameDtoBuilder.BuildCells(game).ToArray(),
                                      true, true,
                                      game.Width, game.Height,
                                      gameId,
                                      game.IsGameFinished,
                                      game.Moves);

            return(new ObjectResult(gameDto));
        }
Esempio n. 10
0
        public IActionResult Moves(Guid gameId, [FromBody] UserInputForMovesPost userInput)
        {
            var game = GamesRepo.GetGame(gameId);

            game.MovePlayer(moveProvider.GetMovement(userInput));

            if (game.GameField.IsFinished)
            {
                GameDto gameField = game.GameField;
                try
                {
                    var stringCells = gameDataLoader.Load(game.GameField.Level + 1);
                    var cells       = parsingService.Parse(stringCells);
                    game.GameField = new GameDto(cells, true, true, game.GameField.Width, game.GameField.Height, gameId,
                                                 false, 0, game.GameField.Level + 1);
                }
                catch
                {
                    game.GameField = gameField;
                }
            }
            return(new ObjectResult(game.GameField));
        }