public void CreateGame() { Game game = new Game(); game.Heigth = 10; game.Width = 10; game.Game_Name = "Game" + _context.Games.Count(); game.BattleshipCount = 1; game.CarrierCount = 1; game.SubmarineCount = 1; game.PatrolCount = 1; game.CruiserCount = 1; game.IsPlayerMove = true; _context.Games?.Add(game); _context.SaveChanges(); Game = game; CreateShips(); game.Cells = GenerateField(); }
public async Task OnGetAsync(int?GameId) { if (GameId != null) { _context.Games?.Remove(_context.Games.Single(x => x.Id == GameId.Value)); _context.Cells?.RemoveRange(_context.Cells.Where(x => x.GameId == GameId.Value)); _context.Ships?.RemoveRange(_context.Ships.Where(x => x.GameId == GameId.Value)); _context.SaveChanges(); } Games = await _context.Games.ToListAsync(); }
public async Task <IActionResult> OnPostSubmit() { if (Game != null) { _context.Games?.Remove(Game); } Game newGame = new Game(); newGame.Width = int.Parse(Request.Form["gameWidth"]); newGame.Heigth = int.Parse(Request.Form["gameHeigth"]); newGame.CarrierCount = int.Parse(Request.Form["CarrierNumber"]); newGame.SubmarineCount = int.Parse(Request.Form["SubmarineNumber"]); newGame.CruiserCount = int.Parse(Request.Form["CruiserNumber"]); newGame.BattleshipCount = int.Parse(Request.Form["BattleshipNumber"]); newGame.PatrolCount = int.Parse(Request.Form["PatrolNumber"]); newGame.CanGoToAnother = Request.Form["can"] == "on"; _context.Games?.Add(newGame); _context.SaveChanges(); foreach (string shipType in ShipsTypes) { Ship newShip = new Ship(); newShip.Type = shipType; newShip.Length = int.Parse(Request.Form[shipType + "Size"]); newShip.GameId = newGame.Id; newShip.IsPlayer = true; shipsForSquare.Add(new ShipVM(newShip, int.Parse(Request.Form[shipType + "Number"]))); _context.Ships?.Add(newShip); } foreach (string shipType in ShipsTypes) { Ship newShip = new Ship(); newShip.Type = shipType; newShip.Length = int.Parse(Request.Form[shipType + "Size"]); newShip.GameId = newGame.Id; newShip.IsPlayer = false; _context.Ships?.Add(newShip); } await _context.SaveChangesAsync(); if (!CheckSquare(newGame.Width * newGame.Heigth, shipsForSquare, newGame.CanGoToAnother)) { return(Redirect("./Settings?gameId=" + newGame.Id + "&error=on")); } return(Redirect("./OriginalSettingsGame?gameId=" + newGame.Id + "&settings=custom")); }
public void OnGet(int gameId, int?x, int?y) { Game = _context.Games.Single(x => x.Id == gameId); Game.Cells = _context.Cells.Where(x => x.GameId == gameId).ToList(); IsBotWinner = Game.Cells.Where(c => c.ShipName != null && c.IsPlayer).Count() == Game.Cells.Where(c => c.ShipName != null && c.IsPlayer && c.IsHit).Count(); IsPlayerWinner = Game.Cells.Where(c => c.ShipName != null && !c.IsPlayer).Count() == Game.Cells.Where(c => c.ShipName != null && !c.IsPlayer && c.IsHit).Count(); if (!IsBotWinner && !IsPlayerWinner) { MakeAMove(x, y); MakeAMoveByBot(); _context.SaveChanges(); } }
public static BattleShipDbContext Create() { var config = new DbContextOptionsBuilder <BattleShipDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; var _battleShipDbContext = new BattleShipDbContext(config); _battleShipDbContext.Database.EnsureCreated(); var match = new Match() { MatchId = 1 }; _battleShipDbContext.Matches.Add(match); var board = new Board() { Match = match, X = 10, Y = 10 }; _battleShipDbContext.Boards.Add(board); var bs = new BattleShip() { Board = board }; _battleShipDbContext.BattleShips.Add(bs); _battleShipDbContext.BattleShipCoordinates.Add(new BattleShipCoordinate() { BattleShip = bs, X = 2, Y = 2 }); _battleShipDbContext.BattleShipCoordinates.Add(new BattleShipCoordinate() { BattleShip = bs, X = 2, Y = 3 }); _battleShipDbContext.SaveChanges(); return(_battleShipDbContext); }
public void OnGet(int?GameId, string?ShipId, string?settings, int?x, int?y, bool?isNewShip, string?shipType, int?random, bool?isRotate) { if (GameId == null && settings == "original") { GameBrain.CreateGame(); GameBrain.CreateShips(); GameBrain.GenerateField(); } else { if (GameId != null) { GameBrain.LoadGame(GameId.Value); } } GameBrain.GenerateField(); GameBrain.ReCalcCounters(); if (isNewShip == true && shipType != null) { GameBrain.CreateShip(shipType, true); GameBrain.Save(); } if (ShipId != null) { if (isNewShip != true) { GameBrain.SelectedShip = _context.ShipGameAssignments.Single(x => x.ShipName == ShipId && x.IsPlayer); if (GameBrain.Game != null) { GameBrain.SelectedShip.Cells = GameBrain.Game.Cells.Where(x => x.IsPlayer && x.ShipName == GameBrain.SelectedShip.ShipName).ToList(); } } if (GameBrain.SelectedShip != null && GameBrain.Game != null) { GameBrain.SelectedShip.Game_Id = GameBrain.Game.Id; if (isRotate == true) { GameBrain.MoveShip(x, y, true, true); GameBrain.SelectedShip.IsRotated = !GameBrain.SelectedShip.IsRotated; } else { GameBrain.MoveShip(x, y, true); } } } if (random == 1) { GameBrain.DoRandom(true); } if (GameBrain.SelectedShip != null) { GameBrain.CreateFieldForBot(GameBrain.SelectedShip); } Game = GameBrain.Game; SelectedShip = GameBrain.SelectedShip; _context.SaveChanges(); }