天天看點

文曲星猜數遊戲的一個簡單實作

文曲星猜數遊戲的一個簡單實作
文曲星猜數遊戲的一個簡單實作

namespace GuessGame

{

    public class GuessGame

    {

        public static readonly int NUMBER_COUNT = 4;

        private int[] answers = null;

        private int rightNumberCount;

        private int wrongNumberCount;

        private int wrongPositionCount;

        public GuessGame()

        {

            Init();

            GenerateDistinctRandomNumbers();

        }

        private void Init()

            rightNumberCount = 0;

            wrongNumberCount = 0;

            wrongPositionCount = 0;

        private void Reset()

        private void GenerateDistinctRandomNumbers()

            answers = new int[NUMBER_COUNT];

            Random random = new Random();

            bool allowZero = true;

            for (int i = 0; i < NUMBER_COUNT; i++)

            {

                int n = random.Next(10);

                if (n == 0 && allowZero)

                {

                    answers[i] = n;

                    allowZero = false;

                }

                else

                    while (answers.Contains(n))

                    {

                        n = random.Next(10);

                    }

            }

        public bool IsRight

            get

                return (rightNumberCount == NUMBER_COUNT);

        public string Result

                return string.Format("{0}A{1}B", rightNumberCount, wrongPositionCount);

        public void Calculate(int[] inputs)

            if (inputs == null || inputs.Length != NUMBER_COUNT)

                throw new Exception("Invalid inputs.");

            Reset();

                int index = Array.IndexOf(answers, inputs[i]);

                if (index < 0)

                    wrongNumberCount++;

                else if (index != i)

                    wrongPositionCount++;

                    rightNumberCount++;

    }

    class Program

        private static readonly int GUESS_TIME = 6;

        static void Main(string[] args)

            PlayGuessGame();

            Console.ReadLine();

        private static void PlayGuessGame()

            int[] inputs = null;

            GuessGame game = new GuessGame();

            int i = 1;

            while (i <= GUESS_TIME)

                inputs = GetInput();

                game.Calculate(inputs);

                Console.WriteLine(game.Result);

                if (game.IsRight)

                    Console.WriteLine("Congratulations!");

                    break;

                i++;

            if (!game.IsRight)

                Console.WriteLine("i think you are not very clever

文曲星猜數遊戲的一個簡單實作

");

        private static int[] GetInput()

            int numberCount = GuessGame.NUMBER_COUNT;

            int[] inputs = new int[numberCount];

            Console.WriteLine("Input {0} numbers: ", numberCount);

            for (int i = 0; i < numberCount; i++)

                Console.Write("  number {0}: ", i + 1);

                inputs[i] = Convert.ToInt32(Console.ReadLine());

            return inputs;

}

文曲星猜數遊戲的一個簡單實作

本文轉自一個程式員的自省部落格園部落格,原文連結:http://www.cnblogs.com/anderslly/archive/2009/07/08/guess-game-without-tdd.html,如需轉載請自行聯系原作者。

繼續閱讀