Beispiel #1
0
 private void ShowGameHistory(GameStateModel game)
 {
     Console.WriteLine();
     Console.WriteLine("History");
     foreach (var state in game.History)
     {
         Console.WriteLine($"Left bound:{state.LeftBound}    Right bound:{state.RightBound}    SpecifiedNumber:{state.SpecifiedNumber}");
     }
 }
Beispiel #2
0
        private void PlayOneRound(GameStateModel game)
        {
            Console.WriteLine($"Please enter number in [{game.LeftBound},{game.RightBound}]");

            int number;

            while (!IsValidNumber(game, number = ReadNumber()))
            {
                Console.WriteLine($"Mistake!!! Please enter number in [{game.LeftBound},{game.RightBound}]");
            }
            game.SpecifiedNumber = number;
            game.History.Add(new GameStateModel
            {
                DesiredNumber   = game.DesiredNumber,
                LeftBound       = game.LeftBound,
                RightBound      = game.RightBound,
                SpecifiedNumber = game.SpecifiedNumber
            });

            if (number > game.DesiredNumber)
            {
                Console.WriteLine("Desired number is less than number you provided");
                game.RightBound = number - 1;
            }

            if (number < game.DesiredNumber)
            {
                Console.WriteLine("Desired number is bigger than number you provided");
                game.LeftBound = number + 1;
            }

            if (!IsGameEnded(game))
            {
                ShowGameHistory(game);
            }

            Console.WriteLine();
        }
Beispiel #3
0
        public void PlayGame()
        {
            var randomizer    = new Random();
            int left          = 0;
            int right         = 100;
            var unknownNumber = randomizer.Next(0, 100);

            var game = new GameStateModel
            {
                LeftBound       = left,
                RightBound      = right,
                DesiredNumber   = unknownNumber,
                SpecifiedNumber = -1,
                History         = new List <GameStateModel>()
            };

            while (!IsGameEnded(game))
            {
                PlayOneRound(game);
            }

            FinishGame(game);
        }
Beispiel #4
0
 private void FinishGame(GameStateModel game)
 {
     Console.WriteLine("You won this game");
     ShowGameHistory(game);
 }
Beispiel #5
0
 private bool IsGameEnded(GameStateModel game)
 {
     return(game.SpecifiedNumber == game.DesiredNumber);
 }
Beispiel #6
0
 public bool IsValidNumber(GameStateModel game, int number)
 {
     return(number >= game.LeftBound && number <= game.RightBound);
 }