Ejemplo n.º 1
0
        public IMenu ProcessAnswer(GameRunnerParameters gameRunnerParameters, string answer)
        {
            int value;

            if (int.TryParse(answer, out value))
            {
                switch (value)
                {
                    case 1:
                    {
                        return new PlayMenu();
                    }

                    case 2:
                    {
                        return new ReplayLoggedGameMenu();
                    }

                    case 3:
                    {
                        gameRunnerParameters.QuitWithoutPlaying = true;
                        return null;
                    }
                }
            }

            Console.WriteLine("Invalid option: {0}", answer);
            return this;
        }
Ejemplo n.º 2
0
        public IMenu ProcessAnswer(GameRunnerParameters gameRunnerParameters, string answer)
        {
            if (string.IsNullOrWhiteSpace(answer))
            {
                return new MainMenu();
            }

            int answerNumber;
            if (int.TryParse(answer, out answerNumber))
            {
                if (answerNumber == 0)
                {
                    if (_currentPage > 1)
                    {
                        _currentPage--;
                        return this;
                    }
                }
                else if (answerNumber == 10)
                {
                    if (_currentPage < _totalPages)
                    {
                        _currentPage++;
                        return this;
                    }
                }
                else if (answerNumber > 0 && answerNumber < 10)
                {
                    int index = (answerNumber - 1) + ((_currentPage - 1) * PageSize);
                    if (index < _loggedGameFilenames.Length)
                    {
                        var gameFile = _loggedGameFilenames[index];
                        try
                        {
                            var loggedGame = ActionLogReader.ReadLog(gameFile.FullName);
                            gameRunnerParameters.GameSeed = loggedGame.GameSeed;
                            gameRunnerParameters.ReplayActions = loggedGame.Actions;

                            return null;
                        }
                        catch (Exception ex)
                        {
                            if (ex is IOException || ex is UnauthorizedAccessException)
                            {
                                Console.WriteLine("The selected file could not be read");
                                return this;
                            }

                            throw;
                        }
                    }
                }
            }

            // If we got here, we had invalid input
            Console.WriteLine("Invalid input");
            return this;
        }
Ejemplo n.º 3
0
        public static void RunGame(GameRunnerParameters parameters)
        {
            if (parameters == null)
                throw new ArgumentNullException("parameters");

            var output = new OutputProcessor();
            var actionProcessor = new GameActionProcessor(parameters.GameSeed);

            var state = actionProcessor.GenerateInitialBoard();
            output.DisplayGame(state, actionProcessor.Seed);

            bool isReplayingLoggedGame = parameters.ReplayActions.Any();
            int replayActionIndex = 0;

            var writer = isReplayingLoggedGame
                             ? null
                             : new ActionLogWriter(GetNewLogName(), actionProcessor.Seed);

            while (state.Status == GameStatus.InProgress)
            {
                GameAction action;

                if (isReplayingLoggedGame)
                {
                    // Replaying logged game
                    if (replayActionIndex >= parameters.ReplayActions.Length)
                    {
                        Console.WriteLine("Logged game ended without any end condition being met!");
                        break;
                    }

                    action = parameters.ReplayActions[replayActionIndex];
                    replayActionIndex++;

                    Thread.Sleep((int)(SecondsBetweenComputerControlledActions * 1000));
                }
                else
                {
                    // Player is playing the game
                    action = GetActionFromKeyPress();
                }

                actionProcessor.RunGameAction(state, action);

                output.DisplayGame(state, actionProcessor.Seed);

                if (writer != null)
                {
                    writer.LogAction(action);
                }
            }

            if (writer != null)
            {
                writer.Dispose();
            }
        }
Ejemplo n.º 4
0
        public IMenu ProcessAnswer(GameRunnerParameters gameRunnerParameters, string answer)
        {
            int seed;
            if (int.TryParse(answer, out seed))
            {
                gameRunnerParameters.GameSeed = seed;
            }

            return null;
        }
Ejemplo n.º 5
0
        public static GameRunnerParameters DisplayMenu(IMenu menu, GameRunnerParameters parameters = null)
        {
            if (menu == null)
                throw new ArgumentNullException("menu");

            if (parameters == null)
                parameters = new GameRunnerParameters();

            Console.Write(menu.Question);
            Console.WriteLine();
            var answer = Console.ReadLine();

            Console.WriteLine();

            var result = menu.ProcessAnswer(parameters, answer);
            if (result != null)
                DisplayMenu(result, parameters);

            return parameters;
        }