static string PlayBattle(SeaBattle game) { if (game.Game != null) { SeabBattleConsoleUI.DrawBoard(game.Game.Cells.Where(g => g.IsPlayer).ToList(), game, true, true); SeabBattleConsoleUI.DrawBoard(game.Game.Cells.Where(g => !g.IsPlayer).ToList(), game, false, true); var menu = new Menu(MenuLevel.Level1); menu.AddMenuItem(new MenuItem( $"Player MakeMove", userChoice: "p", () => { var(x, y) = GetMoveCordinates(game); game.MakeAMove(x, y); game.MakeAMoveByBot(); game.Save(); SeabBattleConsoleUI.DrawBoard(game.Game.Cells.Where(g => g.IsPlayer).ToList(), game, true, true); SeabBattleConsoleUI.DrawBoard(game.Game.Cells.Where(g => !g.IsPlayer).ToList(), game, false, true); return(""); }) ); menu.RunMenu(); } return(""); }
public void TestGetCountOfShips() { INextShot stubConsoleReader = new FakeConsoleReader(); SeaBattle seaBattle = new SeaBattle(stubConsoleReader); Assert.IsTrue(seaBattle.GetNumberOfShipsOnStart() <= 17); }
public void TestSeaBattleCompareCountOfShipsAndShots() { INextShot stubConsoleReader = new FakeConsoleReader(); SeaBattle seaBattle = new SeaBattle(stubConsoleReader); seaBattle.StartBattle(); Assert.IsTrue(seaBattle.GetNumberOfShipsOnStart() < seaBattle.GetCountOfShots()); }
public void TestGetCountOfShotsOnTheEnd() { INextShot stubConsoleReader = new FakeConsoleReader(); SeaBattle seaBattle = new SeaBattle(stubConsoleReader); seaBattle.StartBattle(); Assert.IsTrue(seaBattle.GetCountOfShots() <= 100); }
static SeaBattle LoadGameAction(SeaBattle game) { Console.WriteLine("Games, Which to load"); int i = 0; List <Game> games = game.GetGames(); foreach (Game g in games) { i++; Console.WriteLine("[" + i + "] " + g.Game_Name); } game.LoadGame(games[int.Parse(Console.ReadLine()) - V].Id); return(game); }
static (int x, int y) GetMoveCordinates(SeaBattle game) { if (game.Game != null) { Console.WriteLine("Upper left corner is (1,1)!"); Console.Write("Give X (1-" + game.Game.Width + "), Y (1-" + game.Game.Heigth + "):"); var userValue = Console.ReadLine().Split(','); var x = V; var y = V; try { x = int.Parse(userValue[0].Trim()); y = int.Parse(userValue[V].Trim()); } catch { return(GetMoveCordinates(game)); } return(x, y); } return(0, 0); }
public OriginalSettingsGame(BattleShipDbContext context) { _context = context; GameBrain = new SeaBattle(_context); }
public static void DrawBoard(List <Cell> cells, SeaBattle game, bool IsPlayer = true, bool isPlayMode = false) { if (game.Game != null) { List <char> Letters = new List <char>() { '0', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; string log = ""; Doline(game.Game.Width + 1); for (int i = 0; i < game.Game.Heigth + 1; i++) { log += "|" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)); for (int j = 0; j < game.Game.Width + 1; j++) { if (i > 0 && j > 0) { if (isPlayMode) { if (cells.Single(c => c.X == j && c.Y == i).ShipName != null && cells.Single(c => c.X == j && c.Y == i).IsHit) { log += "XS" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)) + "|" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)); } else if (cells.Single(c => c.X == j && c.Y == i).ShipName == null && cells.Single(c => c.X == j && c.Y == i).IsHit) { log += " X" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)) + "|" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)); } else if (cells.Single(c => c.X == j && c.Y == i).ShipName != null && !cells.Single(c => c.X == j && c.Y == i).IsHit&& IsPlayer) { log += " S" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)) + "|" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)); } else { log += " " + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)) + "|" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)); } } else { if (cells.Single(c => c.X == j && c.Y == i).ShipName != null && IsPlayer) { log += " S" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)) + "|" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)); } else { log += " " + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)) + "|" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)); } } } if (i == 0 && j == 0) { log += " " + String.Concat(Enumerable.Repeat(" ", 111.ToString().Length)) + "|"; } else if (i == 0 && j.ToString().Length == 2) { log += String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)) + j + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)) + "|"; } else if (i == 0) { log += String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)) + j + String.Concat(Enumerable.Repeat(" ", 111.ToString().Length)) + "|"; } else if (j == 0) { log += Letters[i] + String.Concat(Enumerable.Repeat(" ", 111.ToString().Length)) + "|" + String.Concat(Enumerable.Repeat(" ", 11.ToString().Length)); } } Console.WriteLine(log); log = ""; } Doline(game.Game.Width + 1); } }
static string SeaBattle() { var dbOptions = new DbContextOptionsBuilder <BattleShipDbContext>().UseSqlServer( "Server = (localdb)\\mssqllocaldb; Database = BattleShipDbContext; Trusted_Connection = True; MultipleActiveResultSets = true" ).Options; using var dbCtx = new BattleShipDbContext(dbOptions); SeaBattle?game = new SeaBattle(dbCtx); Menu? menu = new Menu(MenuLevel.Level0); if (game != null) { menu.AddMenuItem(new MenuItem( $"Player Place a ship", userChoice: "p", () => { if (game.Game != null) { var(x, y) = GetMoveCordinates(game); game.CreateShip(GetshipType(), true); game.MoveShip(x, y, true, isRotate()); if (game.SelectedShip != null) { game.CreateFieldForBot(game.SelectedShip); game.Save(); SeabBattleConsoleUI.DrawBoard(game.Game.Cells.Where(g => g.IsPlayer).ToList(), game); } else { Console.WriteLine("Do not working("); } } else { Console.WriteLine("Please Create Game or load"); } return(""); }) ); menu.AddMenuItem(new MenuItem( $"Random", userChoice: "random", () => { if (game.Game != null) { game.DoRandom(true); if (game.SelectedShip != null) { game.CreateFieldForBot(game.SelectedShip); game.Save(); SeabBattleConsoleUI.DrawBoard(game.Game.Cells.Where(g => g.IsPlayer).ToList(), game); } } else { Console.WriteLine("Please Create Game or load"); } return(""); }) ); menu.AddMenuItem(new MenuItem( $"Play game", userChoice: "play", () => { if (game.Game != null) { return(PlayBattle(game)); } else { Console.WriteLine("Please Create Game or load"); return(""); } }) ); menu.AddMenuItem(new MenuItem( $"Create game with original settings", userChoice: "s", () => { game.CreateGame(); return(""); }) ); menu.AddMenuItem(new MenuItem( $"Load game", userChoice: "l", () => { game = LoadGameAction(game); return(""); }) ); menu.AddMenuItem(new MenuItem( $"Create game with custom settings", userChoice: "e", () => { Game gameToCreate = new Game(); gameToCreate.Game_Name = "CustomGame_" + game.GetGames().Count(); gameToCreate.Width = GetData("Width", V); gameToCreate.Heigth = GetData("Heigth", V); gameToCreate.BattleshipCount = GetData("Battleship", V); gameToCreate.CruiserCount = GetData("Cruiser", V); gameToCreate.SubmarineCount = GetData("Submarine", V); gameToCreate.PatrolCount = GetData("Patrol", V); gameToCreate.CarrierCount = GetData("Carrier", V); gameToCreate.CanGoToAnother = IsShipsCanGo(); game.SaveGame(gameToCreate); game.Game = gameToCreate; game.Game.Cells = game.GenerateField(); Ship shipTocreate; foreach (var shiptype in game.ShipsTypes) { for (int i = 0; i < 2; i++) { shipTocreate = game.CreateCustomShip(shiptype); shipTocreate.Length = GetData(shiptype, 2); shipTocreate.IsPlayer = i == V; game.SaveShip(shipTocreate); } } game.LoadGame(game.Game.Id); return(""); }) ); var userChoice = menu.RunMenu(); return(userChoice); } return(""); }
static string SaveGameAction(SeaBattle game) { // 2020-10-12 return(""); }