Exemplo n.º 1
0
        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task <IActionResult> OnPostAsync()
        {
            // Console.WriteLine(GameOption!.EShipsCanTouch);
            // if (!ModelState.IsValid)
            // {
            //     Console.WriteLine("Not Valid");
            //     return Page();
            // }

            var game = new Game()
            {
                GameOption = GameOption !,
                PlayerA    = PlayerA !,
                PlayerB    = PlayerB !
            };
            BattleShips battleShips = new(game);
            var         ships       = battleShips.GetAvailableShips();


            foreach (var ship in ships.Where(ship => !_context.Ships.Any(s => s.Size == ship.Size && s.Name == ship.Name)))
            {
                _context.Ships.Add(ship);
            }

            await _context.SaveChangesAsync();

            _context.Games.Add(game);
            //_context.GameOptions.Add(game.GameOption);
            foreach (var gameOptionShip in battleShips.GetGameOptionShips())
            {
                var dbShip = _context.Ships.First(s => s.Size == gameOptionShip.Ship.Size && s.Name == gameOptionShip.Ship.Name);
                gameOptionShip.Ship = dbShip;
                _context.GameOptionShips.Add(gameOptionShip);
            }

            foreach (var gameShip in battleShips.GetGameShips(true))
            {
                _context.GameShips.Add(gameShip);
            }

            foreach (var gameShip in battleShips.GetGameShips(false))
            {
                _context.GameShips.Add(gameShip);
            }
            await _context.SaveChangesAsync();

            return(RedirectToPage("../GamePlay/Index", new { id = game.GameId }));
        }
    }
Exemplo n.º 2
0
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(string Player1Name, string Player2Name, int x, int y, int TouchingRule)
        {
            BattleShip = new BattleShip(x, y, Player1Name, Player2Name);
            InsertBoat(ECellState.Carrier, 1);
            InsertBoat(ECellState.Battleship, 1);
            InsertBoat(ECellState.Submarine, 1);
            InsertBoat(ECellState.Cruiser, 1);
            InsertBoat(ECellState.Patrol, 1);
            BattleShip.TouchingRule = ETouchingRule.CanTouch;

            Domain.GameState gameState = new Domain.GameState()
            {
                DT   = DateTime.Now,
                Json = BattleShip.GetSerializedGameState(),
                Game = new Game()
                {
                    CreationDT = DateTime.Now,
                    PlayerA    = BattleShip.Player1Name,
                    PlayerB    = BattleShip.Player2Name
                }
            }
            ;
            // createInitialGamestate
            _context.GameStates.Add(gameState);
            await _context.SaveChangesAsync();

            /*_context.Games.Add(Game);
             * await _context.SaveChangesAsync();*/
            return(RedirectToPage("../Setup/Index"));

            //return Page();
        }
Exemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync(string Carrier_A, string Battleship_A, string Submarine_A, string Cruiser_A, string Patrol_A,
                                                      string Carrier_B, string Battleship_B, string Submarine_B, string Cruiser_B, string Patrol_B)
        {
            GameStates = await _context.GameStates.Include(g => g.Game).ToListAsync();

            var j = GameStates.Last().Json;
            var g = GameStates.Last().Game;

            gamee = g;
            BattleShip game = new BattleShip(5, 5, "unknown1", "unknown2");

            game.SetGameStateFromJsonString(j);
            ships11 = game.Ships1;
            ships22 = game.Ships2;
            x       = game._x;
            y       = game._y;
            b1      = game._board1;
            b2      = game._board2;
            TheGame = game;
            TheGame.ShipCountPerPlayer = 5;
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Carrier_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Battleship_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Submarine_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Cruiser_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships1, b2, Patrol_A, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Carrier_B, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Battleship_B, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Submarine_B, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Cruiser_B, false);
            ConsoleApp.Program.PlaceShips(TheGame, TheGame.Ships2, b1, Patrol_B, false);
            string json = TheGame.GetSerializedGameState();

            Domain.GameState gs = new Domain.GameState()
            {
                DT   = DateTime.Now,
                Game = gamee,
                Json = json
            };
            _context.GameStates.Add(gs);
            await _context.SaveChangesAsync();

            int gsid = gs.Id;
            int gid  = gs.GameId;

            return(RedirectToPage("../Fight/Index", new { gameid = gid, gamestateid = gsid }));
        }
Exemplo n.º 4
0
        public async Task OnGetAsync(int gameid, int gamestateid, int y = 999, int x = 999)
        {
            gid  = gameid;
            gsid = gamestateid;
            //GameStates = await _context.GameStates.Include(g=> g.Game).ToListAsync();
            GameState = _context.GameStates.Find(gsid);

            if (GameState == null)
            {
                GameState = _context.GameStates.Find(gsid - 1);
            }
            var        j    = GameState.Json;
            var        g    = _context.Games.Find(gameid);
            BattleShip game = new BattleShip(5, 5, "unknown1", "unknown2");

            game.SetGameStateFromJsonString(j);


            if (x != 999 && y != 999)
            {
                game.MakeAMove(x, y);
                game._nextMoveByPlayer2 = game._nextMoveByPlayer2;
                string           json = game.GetSerializedGameState();
                Domain.GameState gs   = new Domain.GameState()
                {
                    DT   = DateTime.Now,
                    Game = g,
                    Json = json
                };
                // createInitialGamestate
                _context.GameStates.Add(gs);
                await _context.SaveChangesAsync();

                /*int gsid = gs.Id;
                 * int gid = gs.GameId;*/
            }

            width             = game._x;
            height            = game._y;
            player1name       = game.Player1Name;
            player2name       = game.Player2Name;
            nextmovebyplayer2 = game._nextMoveByPlayer2;
            if (game._nextMoveByPlayer2)
            {
                b1 = game._board1;
                b2 = game._board2;
            }
            else
            {
                b1 = game._board2;
                b2 = game._board1;
            }

            if (game.GameOver)
            {
                GameOver = true;
            }


            //return RedirectToPage("/Index");
        }
Exemplo n.º 5
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 }));