예제 #1
0
        private static string PlayBattleShips(BattleShips battleShips, DbContextOptions <ApplicationDbContext> dbOptions)
        {
            if (battleShips.GetUnaddedGameShips().Count > 0)
            {
                var userWantsToQuit = AddShips(battleShips, dbOptions);
                if (userWantsToQuit)
                {
                    return("");
                }
                Console.Clear();
            }
            else if (battleShips.IsGameOver())
            {
                BattleShipsConsoleUi.GameOverUi(battleShips);
                return("");
            }

            do
            {
                Console.WriteLine("Press ESC to return back to menu");
                var(x, y) = GetCoordinates(battleShips);
                // Exit Game if x == 100 & y == 100
                if (x == 100 && y == 100)
                {
                    SaveGameMenu(battleShips, dbOptions);
                    break;
                }

                var(successfulMove, playerBoardState) = battleShips.MakeMove(x, y);
                //isGameOver
                if (successfulMove[1])
                {
                    BattleShipsConsoleUi.GameOverUi(battleShips);
                    SaveGameMenu(battleShips, dbOptions);
                    break;
                }
            } while (true);

            return("");
        }
예제 #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 }));