Exemplo n.º 1
0
        public void PlaceShipsOnBoard(IPlayer player)
        {
            try
            {
                _log.Info("BTG - trying to place ships on board!");

                var ships = new[] { ShipType.Battleship, ShipType.Destroyer, ShipType.Destroyer };
                var i     = 0;
                foreach (var ship in ships)
                {
                    _ship          = _shipFactory.CreateShip(ship);
                    _shipDirection = _randomManager.GetDirection();
                    do
                    {
                        _coordinate = _coordinateManager.GetRandomCoordinate();
                    }while (!_board.IsValidCoordinate(_coordinate, _shipDirection, _ship.Coordinates.Length) ||
                            _board.IsOverlap(player.Board.Ships, _coordinate));

                    _ship = PlaceShip();

                    player.Board.Ships[i++] = _ship;
                }

                _outputManager.WriteLine($"Ships for {player.Name} placed - completed!", ConsoleColor.Yellow);
            }
            catch (Exception e)
            {
                _log.Error($"BTG - Error when trying to place the ships on board: {e}");
            }
        }
Exemplo n.º 2
0
        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.");
        }