public async Task <IActionResult> OnGet(int id, int?choice) { Game = await _context.Games.FirstOrDefaultAsync(m => m.GameId == id); if (choice == 1) { if (!BLL.ShipPlacement.RndPlacement(Game, false)) { Message = "You have set too small board for those ship sizes!"; return(Page()); } _context.Update(Game); _context.SaveChanges(); return(RedirectToPage("PlayGame", new { id = Game.GameId })); } return(Page()); }
public async Task <IActionResult> OnGet(int id, int?shipNo, int?row, int?col, int?vert) { Game = await _context.Games.FirstOrDefaultAsync(m => m.GameId == id); if (shipNo != null && row != null && col != null && vert != null) { var ship = Game.GameShips[shipNo.Value]; ShipNo = shipNo.Value; if (vert.Value == 1) { ship.ShipLayout = ShipLayout.Vertical; } if (ship.ShipLayout == ShipLayout.Vertical && row.Value + ship.ShipLength <= Game.Rows || ship.ShipLayout == ShipLayout.Horizontal && col.Value + ship.ShipLength <= Game.Cols) { if (BLL.ShipPlacement.CanPlaceShip(Game, ship, row.Value, col.Value)) { BLL.InitBoard.PlaceShip(Game, ship, row.Value, col.Value); _context.Update(Game); _context.SaveChanges(); ShipNo--; if (ShipNo < 0) { if (Game.PlayerOneTurn) { Game.PlayerOneTurn = false; if (Game.Ai) { if (!BLL.ShipPlacement.RndPlacement(Game, true)) { Message = "You have set too small board for those ship sizes!"; return(Page()); } Game.PlayerOneTurn = true; _context.Update(Game); _context.SaveChanges(); return(RedirectToPage("PlayGame", new { id = Game.GameId })); } _context.Update(Game); _context.SaveChanges(); return(RedirectToPage("CustomPlacement", new { id = Game.GameId })); } Game.PlayerOneTurn = true; _context.Update(Game); _context.SaveChanges(); return(RedirectToPage("PlayGame", new { id = Game.GameId })); } } else { Message = "Other ship at coordinates"; } } else { var layOut = vert == 1 ? "vertically" : "horizontally"; Message = $"Ship cannot be placed on coordinates ({row}, {col}) {layOut}"; } } else { ShipNo = Game.NumberOfShips - 1; } if (ShipNo >= 0) { CurrentShip = Game.GameShips[ShipNo].ShipLength; } if (Game.PlayerOneTurn) { Player = Game.PlayerOne; Board = Game.PlayerOneBoard; } else { Player = Game.PlayerTwo; Board = Game.PlayerTwoBoard; } return(Page()); }
public async Task <IActionResult> OnGet(int id, int?row, int?col) { Game = await _context.Games.FirstOrDefaultAsync(m => m.GameId == id); Me = Game.PlayerTwo; Enemy = Game.PlayerOne; EnemyBoard = Game.PlayerOneBoard; MyBoard = Game.PlayerTwoBoard; if (Game.PlayerOneTurn) { Me = Game.PlayerOne; Enemy = Game.PlayerTwo; EnemyBoard = Game.PlayerTwoBoard; MyBoard = Game.PlayerOneBoard; } if (row != null && col != null) { switch (EnemyBoard[row.Value][col.Value]) { case CellState.Hit: Message = "You have already bombed this coordinate!"; break; case CellState.Empty: EnemyBoard[row.Value][col.Value] = CellState.Miss; if (Game.Ai) { BLL.Ai.MakeMove(Game); } else { Game.PlayerOneTurn = !Game.PlayerOneTurn; } break; case CellState.Miss: Message = "You have already bombed this coordinate!"; break; case CellState.Ship: EnemyBoard[row.Value][col.Value] = CellState.Hit; BLL.GameState.IsShipWrecked(Game, row.Value, col.Value); if (Game.Ai) { BLL.Ai.MakeMove(Game); } else { Game.PlayerOneTurn = !Game.PlayerOneTurn; } var msg = BLL.GameState.HasPlayerWon(Game); if (msg != "No") { Message = $"{msg} has won the game!"; Game.GameOver = true; } break; case CellState.Wreck: Message = "You have already bombed this coordinate!"; break; default: throw new ArgumentOutOfRangeException(); } Me = Game.PlayerTwo; Enemy = Game.PlayerOne; EnemyBoard = Game.PlayerOneBoard; MyBoard = Game.PlayerTwoBoard; if (Game.PlayerOneTurn) { Me = Game.PlayerOne; Enemy = Game.PlayerTwo; EnemyBoard = Game.PlayerTwoBoard; MyBoard = Game.PlayerOneBoard; } _context.Update(Game); _context.SaveChanges(); } if (Game == null) { return(NotFound()); } return(Page()); }