Пример #1
0
        private static bool AddShips(BattleShips battleShips, DbContextOptions <ApplicationDbContext> dbOptions)
        {
            var wantsToQuit = false;
            int unAddedShipCount;

            do
            {
                Console.Clear();
                var player = battleShips.GetPlayer(true);
                Console.WriteLine($"{player.Name} choose a ship to add:");
                var gameShips = battleShips.GetUnaddedGameShips();

                var gameShip = BattleShipsConsoleUi.DrawShipList(gameShips, player.Name);

                var(x, y) = GetCoordinates(
                    battleShips,
                    gameShip.Size,
                    true);

                if (x == 100 && y == 100)
                {
                    SaveGameMenu(battleShips, dbOptions);
                    wantsToQuit = true;
                    break;
                }

                var isHorizontal = BattleShipsConsoleUi.IsShipHorizontal;
                battleShips.AddShips(gameShip, x, y, isHorizontal);

                unAddedShipCount = battleShips.GetUnaddedGameShips().Count;
            } while (unAddedShipCount > 0);

            return(wantsToQuit);
        }
Пример #2
0
        private bool _changePlayer; // To display other screen before other player makes move

        public async Task <IActionResult> OnGetAsync(int id, int?shipId, int?x, int?y, string dir, bool horizontal, string move)
        {
            IsShipHorizontal = horizontal;


            Game = await _context.Games
                   .Where(g => g.GameId == id)
                   .Include(g => g.GameOption)
                   .Include(g => g.GameOption.GameOptionShips)
                   .Include(g => g.PlayerA)
                   .Include(g => g.PlayerA.GameShips)
                   .Include(g => g.PlayerA.PlayerBoardStates)
                   .Include(g => g.PlayerB)
                   .Include(g => g.PlayerB.GameShips)
                   .Include(g => g.PlayerB.PlayerBoardStates).FirstOrDefaultAsync();

            BattleShips = new BattleShips(Game);


            if (BattleShips.GetUnaddedGameShips().Count > 0)
            {
                CurrentGameShip = shipId != null?BattleShips.GetUnaddedGameShips().FirstOrDefault(s => s.GameShipId == shipId) : BattleShips.GetUnaddedGameShips().First();
            }

            IsGameOver = BattleShips.IsGameOver();

            if (x != null && y != null && CurrentGameShip != null)
            {
                var(isSuccess, playerBoardState) = BattleShips.AddShips(CurrentGameShip, x.Value, y.Value, IsShipHorizontal);
                if (isSuccess && playerBoardState != null)
                {
                    _context.Add(playerBoardState);
                    if (BattleShips.GetUnaddedGameShips().Count > 0)
                    {
                        CurrentGameShip = BattleShips.GetUnaddedGameShips().First();
                    }
                    else
                    {
                        CurrentGameShip = null;
                        _changePlayer   = true;
                    }
                }
            }
            else if (x != null && y != null && move == "shoot")
            {
                var(success, playerBoardState) = BattleShips.MakeMove(x.Value, y.Value);

                if (success[0] && !success[1] && playerBoardState != null)
                {
                    _context.Add(playerBoardState);
                    _changePlayer = true;
                }
                else if (success[1] && playerBoardState != null)
                {
                    _context.Add(playerBoardState);
                    IsGameOver = true;
                }
            }

            CurrentPlayerBoard = BattleShips.GetBoards().First();
            if (BattleShips.GetUnaddedGameShips().Count == 0 && !IsGameOver)
            {
                OtherPlayerBoard = BattleShips.GetBoards(true)[1];
            }
            else if (IsGameOver)
            {
                OtherPlayerBoard = BattleShips.GetBoards(false, true)[1];
            }

            if (CurrentGameShip != null)
            {
                HandlePosition(dir);
            }


            await _context.SaveChangesAsync();

            if (_changePlayer)
            {
                return(RedirectToPage("../NextTurn/Index", new { id = Game !.GameId }));