Exemple #1
0
        public void Run()
        {
            Stopwatch timer = new Stopwatch(); //Adding timer to track the time.

            timer.Start();
            helper.AskPlayerName();                                                                    // Asks user for Name.
            string playerName = Console.ReadLine();                                                    //Gets user name.

            this.gameInitializationStrategy.Initialize(this.hiddenGrid, this.visibleGrid, this.ships); //Initialize hidden and visible grid.
            this.renderer.RenderGrid(this.visibleGrid);
            this.renderer.RenderMessage(GlobalConstants.EnterCoordinatesMsg);

            while (true)
            {
                this.renderer.RenderStatusMessage(this.gameStatus.ToString());

                UserCommands command = this.userInterface.GetCommandFromInput();
                this.renderer.ClearError(); //Clears the console from the error message.

                try
                {
                    if (command == UserCommands.Shoot)
                    {
                        context.ExecuteCommands(command, this.hiddenGrid, this.visibleGrid, this.shotPosition = this.userInterface.GetShotPositionFromInput(),
                                                this.totalAttempts, this.playerData, this.Ships);
                    }
                    else
                    {
                        context.ExecuteCommands(command, this.hiddenGrid, this.visibleGrid, this.shotPosition,
                                                this.totalAttempts, this.playerData, this.Ships);
                    }
                }
                catch (Exception e)
                {
                    this.gameStatus = GameStatus.Error;
                    this.renderer.RenderStatusMessage(this.gameStatus.ToString());
                    this.renderer.RenderErrorMessage(e.Message);
                }
                this.renderer.UpdateGrid(this.visibleGrid, this.shotPosition);

                if (helper.AreAllShipsSunk(Ships))
                {
                    timer.Stop();
                    double timePlayed = timer.Elapsed.Seconds;
                    int    score      = GlobalConstants.MaxScore - totalAttempts;
                    this.dataCreator.CreateNewPlayerFile(playerName, timePlayed, score, playerFactory);
                    dataCreator.CreateNewPlayerFile(playerName, timePlayed, score, playerFactory);
                    this.gameStatus = GameStatus.End;
                }
            }
        }