Example #1
0
        static void Main(string[] args)
        {
            LoadConfiguration();

            _presenter = new ConsolePresenter();

            _gameMap = new Map();
            _gameMap.SetupMap(_mapXSize, _mapYSize);


            computerPlayer = new ComputerPlayer(_gameMap);
            try
            {
                foreach (var pair in shipsConfig)
                {
                    computerPlayer.PlaceShips(pair.Key, pair.Value);
                }
            }
            catch (BattleOfTheShipsData.Exceptions.ShipException shipEx)
            {
                _presenter.ShowMessage("Error while placing ship: " + shipEx.Message, true);
                return;
            }

            string targetInput = string.Empty;
            bool   correctTarget;

            while (!computerPlayer.IsGameOver)
            {
                try
                {
                    correctTarget = false;
                    while (!correctTarget)
                    {
                        _presenter.ShowMap(_gameMap);

                        _presenter.ShowMessage("Please enter coordinates for your next shot in the form of: C4  (or type q! to quit)", false);
                        targetInput = Console.ReadLine().Trim().ToUpper();

                        System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(@"^[a-wA-W]\d{1,2}$");

                        correctTarget = regx.IsMatch(targetInput);
                        if (!correctTarget && targetInput == "Q!")
                        {
                            return;
                        }
                    }

                    var mapTarget = _presenter.ConvertUserInputToMapPoint(targetInput);

                    var shotResult = computerPlayer.CheckShot(mapTarget);
                    _presenter.ShowHitResult(mapTarget, shotResult.WasHit, shotResult.WasSank);
                    _presenter.ShowMessage("(Press any key to continue)", true);
                }
                catch (Exception ex)
                {
                    _presenter.ShowMessage("Error while taking a shot: " + ex.Message, true);
                }
            }

            _presenter.ShowMap(_gameMap);
            _presenter.ShowMessage("Good job! You won!", true);
        }