Пример #1
0
        public Cellule IAAutoPlay(long idGame)
        {
            Board board = _context.Boards.ToList().Find(b => b.IdGame == idGame && b.Player == "player1");

            if (board == null)
            {
                throw new Exception("Invalid idGame");
            }

            var cellules = _celluleController.GetCellulesByBoard(board.Id);

            var result = new IAPlayerController().RunIA(cellules);

            //var result = cellules[Utils.RandomNumber(0, cellules.Count - 1)];
            _celluleController.HitCellule(result);

            return(result);
        }
Пример #2
0
        public async Task <ActionResult <Game> > Put(int id, [FromBody] PutParams putParams)
        {
            Game    game    = _gameController.FindGameById(id);
            Cellule cellule = putParams.Cellule;

            switch (game.GameState)
            {
            case GameState.WAITING:
                if (putParams.CustomBoatPlace)
                {
                    _gameController.EditGameState(game, GameState.PLACE_BOAT);
                }
                else
                {
                    _gameController.SetRandomCellToBoard(game.Id, "player1");
                    _gameController.EditGameState(game, GameState.PLAYER1_TURN);
                }
                break;

            case GameState.PLACE_BOAT:
                if (putParams.Cellules.Count == 0)
                {
                    throw new Exception("Missing params : [List<Cellule> Cellules]");
                }

                foreach (List <Cellule> boat in putParams.Cellules)
                {
                    string identificator = $"boat-{boat.Count}-{Utils.RandomString(5)}";
                    foreach (Cellule boatCell in boat)
                    {
                        boatCell.BoatIdentificator = identificator;
                        _celluleController.SafeEditCellule(boatCell);
                    }
                }
                _gameController.EditGameState(game, GameState.PLAYER1_TURN);
                break;

            case GameState.PLAYER1_TURN:
                Cellule cell = _celluleController.FindCelluleById(cellule.Id);
                if (cell == null)
                {
                    return(NoContent());
                }

                _celluleController.HitCellule(cell);

                bool isWin = _gameController.IsGameWin(cellule.BoardId);

                _gameController.EditGameState(game,
                                              isWin ? GameState.PLAYER1_WIN : cell.IsBoat ? GameState.PLAYER1_HIT : GameState.PLAYER1_SINK);
                break;

            case GameState.PLAYER1_HIT:
                _gameController.EditGameState(game, GameState.PLAYER2_TURN);
                break;

            case GameState.PLAYER1_SINK:
                _gameController.EditGameState(game, GameState.PLAYER2_TURN);
                break;

            case GameState.PLAYER2_HIT:
                _gameController.EditGameState(game, GameState.PLAYER1_TURN);
                break;

            case GameState.PLAYER2_SINK:
                _gameController.EditGameState(game, GameState.PLAYER1_TURN);
                break;

            case GameState.PLAYER2_TURN:
                cell = _gameController.RunBoardIAPlay(game.Id);

                isWin = _gameController.IsGameWin(cell.BoardId);
                _gameController.EditGameState(game,
                                              isWin ? GameState.PLAYER2_WIN : cell.IsBoat ? GameState.PLAYER2_HIT : GameState.PLAYER2_SINK);
                break;
            }

            return(game);
        }