public void CheckForVictory(IFireShotResponse response, IPlayer player) { if (response.ShotStatus == ShotStatus.HitAndSunk) { if (player.Board.Ships.All(s => s.IsSunk)) { response.ShotStatus = ShotStatus.Victory; } } }
public ShotManager( IShipManager shipManager, IFireShotResponse fireShotResponse, IBoard board, ILog log) { _shipManager = shipManager ?? throw new ArgumentNullException(nameof(shipManager)); _fireShotResponse = fireShotResponse ?? throw new ArgumentNullException(nameof(fireShotResponse)); _board = board ?? throw new ArgumentNullException(nameof(board)); _log = log ?? throw new ArgumentNullException(nameof(log)); }
public void CheckShipsForHit(ICoordinate coordinate, IFireShotResponse response, IPlayer player) { try { _log.Info("BTG - Check ship for hits!"); response.ShotStatus = ShotStatus.Miss; foreach (var ship in player.Board.Ships) { _ship = ship; if (ship.IsSunk) { continue; } var status = FireAtShip(coordinate); switch (status) { case ShotStatus.HitAndSunk: response.ShotStatus = ShotStatus.HitAndSunk; response.ShipImpactedName = ship.ShipName; player.Board.ShotHistoryList.Add(coordinate, ShotHistory.Hit); break; case ShotStatus.Hit: response.ShotStatus = ShotStatus.Hit; response.ShipImpactedName = ship.ShipName; player.Board.ShotHistoryList.Add(coordinate, ShotHistory.Hit); break; } if (status != ShotStatus.Miss) { break; } } if (response.ShotStatus == ShotStatus.Miss) { player.Board.ShotHistoryList.Add(coordinate, ShotHistory.Miss); } } catch (Exception e) { _log.Error($"BTG - Check ship for hits: {e}"); } }
public void ShowShotResult(IFireShotResponse shotResponse, ICoordinate coordinate, IPlayer player) { var str = ""; switch (shotResponse.ShotStatus) { case ShotStatus.Duplicate: Console.ForegroundColor = ConsoleColor.Red; str = "Shot location: " + _helper.GetLetterFromNumber(coordinate.XCoordinate) + coordinate.YCoordinate.ToString() + "\t result: Duplicate shot location!"; break; case ShotStatus.Hit: Console.ForegroundColor = ConsoleColor.Green; str = "Shot location: " + _helper.GetLetterFromNumber(coordinate.XCoordinate) + coordinate.YCoordinate.ToString() + "\t result: Hit!"; break; case ShotStatus.HitAndSunk: Console.ForegroundColor = ConsoleColor.Green; str = "Shot location: " + _helper.GetLetterFromNumber(coordinate.XCoordinate) + coordinate.YCoordinate.ToString() + "\t result: Hit and Sunk, " + shotResponse.ShipImpactedName + "!"; break; case ShotStatus.Invalid: Console.ForegroundColor = ConsoleColor.Red; str = "Shot location: " + _helper.GetLetterFromNumber(coordinate.XCoordinate) + coordinate.YCoordinate.ToString() + "\t result: Invalid hit location!"; break; case ShotStatus.Miss: Console.ForegroundColor = ConsoleColor.White; str = "Shot location: " + _helper.GetLetterFromNumber(coordinate.XCoordinate) + coordinate.YCoordinate.ToString() + "\t result: Miss!"; break; case ShotStatus.Victory: Console.ForegroundColor = ConsoleColor.Green; str = "Shot location: " + _helper.GetLetterFromNumber(coordinate.XCoordinate) + coordinate.YCoordinate.ToString() + "\t result: Hit and Sunk, " + shotResponse.ShipImpactedName + "! \n\n"; str += " ******\n"; str += " ******\n"; str += " **** \n"; str += " ** \n"; str += " ** \n"; str += " ******\n"; str += "Game Over, " + player.Name + " wins!"; break; } Console.WriteLine(str); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(""); }
public void Start() { try { _log.Info("BTG - Initialization Started."); _outputManager.ShowFlashScreen(); _outputManager.ShowHeader(); _gameConfig.Player.Name = _inputManager.GetUserName(); _gameConfig.Player.IsComputer = false; _gameConfig.Computer.Name = "ZORRO"; _gameConfig.Computer.IsComputer = true; _outputManager.ShowStats(_gameConfig); _gameConfig.IsPlayerTurn = _randomManager.WhoseFirst(); _log.Info("BTG - Initialization Completed."); } catch (Exception e) { _log.Error($"BTG - Initialization Error {e}."); } do { SetBoard(); do { try { _log.Info("BTG - Game Flow Started!"); _outputManager.ShowWhoseTurn(_gameConfig.IsPlayerTurn ? _gameConfig.Player : _gameConfig.Computer); _outputManager.DrawHistory(_gameConfig.IsPlayerTurn ? _gameConfig.Computer : _gameConfig.Player); _fireShotResponse = Shot(_gameConfig.IsPlayerTurn ? _gameConfig.Computer : _gameConfig.Player, _gameConfig.IsPlayerTurn ? _gameConfig.Player : _gameConfig.Computer, out _shotPoint); _outputManager.Clear(); _outputManager.ShowHeader(); _outputManager.ShowStats(_gameConfig); _outputManager.ShowWhoseTurn(_gameConfig.IsPlayerTurn ? _gameConfig.Player : _gameConfig.Computer); _outputManager.DrawHistory(_gameConfig.IsPlayerTurn ? _gameConfig.Computer : _gameConfig.Player); _outputManager.ShowShotResult(_fireShotResponse, _shotPoint, _gameConfig.IsPlayerTurn ? _gameConfig.Player : _gameConfig.Computer); if (_fireShotResponse.ShotStatus != ShotStatus.Victory) { _outputManager.WriteLine("Press any key to continue to switch to " + (_gameConfig.IsPlayerTurn ? _gameConfig.Computer.Name : _gameConfig.Player.Name), ConsoleColor.Cyan); _gameConfig.IsPlayerTurn = !_gameConfig.IsPlayerTurn; _inputManager.Read(); _outputManager.Clear(); _outputManager.ShowHeader(); _outputManager.ShowStats(_gameConfig); } } catch (Exception e) { _log.Error($"BTG - Game flow error: {e}"); } } while (_fireShotResponse.ShotStatus != ShotStatus.Victory); } while (!_inputManager.CheckQuit()); _log.Info("BTG - Exit Game."); }