/// <summary> /// Draw game boards /// </summary> /// <param name="sea">Current game</param> private void DrawBoard(SeaBattleGame sea) { for (int i = 0; i < 12; i++) { for (int j = 0; j < 12; j++) { BoardDrawer(sea.HumanPlayer.OpponentsBoard, i, j); } for (int j = 0; j < 12; j++) { BoardDrawer(sea.HumanPlayer.MyBoard, i, j); } if (sea.Hack) { for (int j = 0; j < 12; j++) { BoardDrawer(sea.ComputerPlayer.MyBoard, i, j); } } Console.WriteLine(); } DrawAim(sea.HumanPlayer.CurrentPosition); Console.ForegroundColor = ConsoleColor.Gray; Console.SetCursorPosition(0, 12); }
/// <summary> /// Open game from file /// </summary> /// <param name="path">Path of file</param> public void OpenGame(string path) { BinaryFormatter openGame = new BinaryFormatter(); try { using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { _game = (SeaBattleGame)openGame.Deserialize(stream); } Console.WriteLine("Game was opened successful! Now you can continue your past game!"); } catch (FileNotFoundException) { Console.WriteLine("Error! file is not found"); } catch (SerializationException) { Console.WriteLine("Error! This file is invalid!"); } catch (ArgumentException) { Console.WriteLine("Empty string is not allowed!"); } }
public void DrawPicture(SeaBattleGame currentGame) { Console.SetCursorPosition(0, 0); DrawBoard(currentGame); }
public void NewGame() { _game = new SeaBattleGame(); }
private void StartGame() { Console.Clear(); _gameDrawer.DrawPicture(_game); while (true) { if (_game.CurrentTurn) { ConsoleKey pressedKey; do { switch (pressedKey = Console.ReadKey(true).Key) { case ConsoleKey.UpArrow: if (_game.HumanPlayer.CurrentPosition.X != 1) { _game.HumanPlayer.CurrentPosition.X--; } break; case ConsoleKey.DownArrow: if (_game.HumanPlayer.CurrentPosition.X != 10) { _game.HumanPlayer.CurrentPosition.X++; } break; case ConsoleKey.LeftArrow: if (_game.HumanPlayer.CurrentPosition.Y != 1) { _game.HumanPlayer.CurrentPosition.Y--; } break; case ConsoleKey.RightArrow: if (_game.HumanPlayer.CurrentPosition.Y != 10) { _game.HumanPlayer.CurrentPosition.Y++; } break; case ConsoleKey.F12: _game.Hack = !_game.Hack; Console.Clear(); break; case ConsoleKey.Escape: MenuItems(); return; } _gameDrawer.DrawPicture(_game); } while (pressedKey != ConsoleKey.Spacebar); } _game.Turn(); _gameDrawer.DrawPicture(_game); //Check for winner if (_game.HumanPlayer.MyBoard.Ships.Count(s => s.Health > 0) == 0) { Console.WriteLine("You lose!"); //delay Console.ReadKey(true); _game = null; return; } if (_game.ComputerPlayer.MyBoard.Ships.Count(s => s.Health > 0) == 0) { Console.WriteLine("You win"); //delay Console.ReadKey(true); _game = null; return; } } }