Beispiel #1
0
 public Game()
 {
     validDigits          = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
     secretNumber         = GenerateNumber().ToString();
     possibleNumbers      = GetAllPossibleAnswers();
     currentComputerGuess = possibleNumbers[new Random(DateTime.Now.Millisecond).Next(possibleNumbers.Count)];
     round      = 1;
     gameStatus = eGameConditions.ongoing;
 }
Beispiel #2
0
        // After computer makes a guess, gets hint from player
        // If there is not hint, computer removes the numbers with any digits that intersects the current guess's digits
        // If there is, apply the current guess to the all possible secret numbers between [1000,9999]
        // Numbers in pool that return the hint which equals to current player's hint, stay in the list. Otherwise, removed from the list
        // Each turn, list is reduced until one number remains in the list and that's the answer
        public void ComputerTurn()
        {
            if (!string.IsNullOrEmpty(currentPlayerHint))
            {
                currentPlayerHint = HintFormat(currentPlayerHint);
            }

            ++round;

            if (currentPlayerHint.Contains("+") || currentPlayerHint.Contains("-"))
            {
                for (int i = 0; i < possibleNumbers.Count; i++)
                {
                    string hintToCompare = CheckGuess(possibleNumbers[i], currentComputerGuess).hint.Replace(" ", string.Empty);

                    if (currentPlayerHint != hintToCompare)
                    {
                        possibleNumbers.RemoveAt(i);
                        i = -1;
                    }
                }

                try
                {
                    currentComputerGuess = possibleNumbers[new Random(DateTime.Now.Millisecond).Next(possibleNumbers.Count)];
                }
                catch (Exception)
                {
                    gameStatus = eGameConditions.wrongHintEntered;
                }
            }
            else
            {
                char[] digitsToExclude = NumberUtil.GetDigits(Int32.Parse(currentComputerGuess));
                for (int i = 0; i < possibleNumbers.Count; i++)
                {
                    if (possibleNumbers[i].Contains(digitsToExclude[0]) || possibleNumbers[i].Contains(digitsToExclude[1]) ||
                        possibleNumbers[i].Contains(digitsToExclude[2]) || possibleNumbers[i].Contains(digitsToExclude[3]))
                    {
                        possibleNumbers.RemoveAt(i);
                        i = -1;
                    }
                }

                try
                {
                    currentComputerGuess = possibleNumbers[new Random(DateTime.Now.Millisecond).Next(possibleNumbers.Count)];
                }
                catch (Exception)
                {
                    gameStatus = eGameConditions.wrongHintEntered;
                }
            }
        }
Beispiel #3
0
        // Player makes a guess and if false computer turn start if true game is over
        public (bool success, string hint) PlayerTurn()
        {
            bool   success = CheckGuess(secretNumber, currentPlayerGuess).success;
            string hint    = CheckGuess(secretNumber, currentPlayerGuess).hint;

            if (success)
            {
                gameStatus = eGameConditions.playerWon;
            }

            return(success, hint);
        }
Beispiel #4
0
 public void setGameStatus(eGameConditions status)
 {
     gameStatus = status;
 }