計算機科学のブログ

encapsulation - Keep your privates… private - static, field, public, private

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 5(encapsulation - Keep your privates… private)、p.243(Exercise)の解答を求めてみる。

コード

HiLoGame.cs

using System;
namespace MyFirstConsoleApp
{
    public static class HiLoGame
    {
        public const int MAXIMUM = 10;
        private static Random random = new Random();
        private static int currentNumber = random.Next(1, MAXIMUM + 1);
        private static int pot = 11;

        public static int GetPot() { return pot; }
        public static void Guess(bool higher)
        {
            int nextNumber = random.Next(1, MAXIMUM + 1);
            if ((higher && nextNumber >= currentNumber) ||
                (!higher && nextNumber <= currentNumber))
            {
                Console.WriteLine("You guessed right!");
                pot++;
            }
            else
            {
                Console.WriteLine("Bad luck, you guessed wrong");
                pot--;
            }
            currentNumber = nextNumber;
            Console.WriteLine($"The current number is {currentNumber}");
        }
        public  static void Hint()
        {
            int halfNumber = MAXIMUM / 2;
            if (currentNumber >= halfNumber)
            {
                Console.WriteLine($"The number is at least {halfNumber}");
            }
            else
            {
                Console.WriteLine($"The number is at most {halfNumber}");
            }
            pot--;
        }
    }
}

入出力結果(Terminal, Zsh)

Welcome to HiLo.
Guess numbers between 1 and 10
The number is at least 5
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 10.
You guessed right!
The current number is 3
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 11.
Bad luck, you guessed wrong
The current number is 1
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 10.
You guessed right!
The current number is 3
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 11.
You guessed right!
The current number is 5
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 12.
You guessed right!
The current number is 9
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 13.
You guessed right!
The current number is 9
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 14.
Bad luck, you guessed wrong
The current number is 1
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 13.
The number is at most 5
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 12.
Bad luck, you guessed wrong
The current number is 8
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 11.
You guessed right!
The current number is 3
Press h for higher, l for lower, ? to by a hint,
or any other key to quit with 12.