Example #1
0
        /// <summary>
        /// Called when the user selects to start the game
        /// Goes through different method calls to different classes to make up the structure of the turns in the game
        /// Runs through different method calls depending on whether the game type is Human vs. AI or AI vs. AI
        /// </summary>
        private void PlayGame()
        {
            //Creates new arrays for the CodeBreaker's guesses and the CodeMaker's responses
            CodeColours[]   codeBreakerGuess = new CodeColours[numPegs];
            AnswerColours[] guessResponse    = new AnswerColours[numPegs];

            //Runs through this loop whilst the game is still being played, has not yet been won by either player
            do
            {
                if (gameType == GameType.HumanvsAI)
                {
                    //Fills the codeBreakerGuess array using the return value from the TakeTurn method within the Player class
                    //The guessResponse array is then filled using the return value from the CheckGuess within the CodeMaker class by passing it the codeBreakerGuess array
                    //Finally the gameFinished bool is set by passing both arrays to the GenerateBoard method in the GameBoard class, if this returns a true value the do loop is exited
                    codeBreakerGuess = player.TakeTurn();
                    guessResponse    = codeMaker.CheckGuess(codeBreakerGuess);
                    gameFinished     = gameBoard.GenerateBoard(codeBreakerGuess, guessResponse);
                }
                else if (gameType == GameType.AIvsAI)
                {
                    //Runs through a similar structure as with the HumanvsAI game type however the codeBreakerGuess array is filled using the TakeTurn method in the CodeBreaker class
                    //Also the CheckRemainingCombos method is called in the CodeBreaker class passing the guessReponse array
                    Console.ReadLine();
                    codeBreakerGuess = codeBreaker.TakeTurn();
                    guessResponse    = codeMaker.CheckGuess(codeBreakerGuess);
                    codeBreaker.CheckRemainingCombos(guessResponse);
                    gameFinished = gameBoard.GenerateBoard(codeBreakerGuess, guessResponse);
                }
            }while (!gameFinished);
        }
Example #2
0
        /// <summary>
        /// Called from the constructor to generate the answer code
        /// </summary>
        /// <returns>returns the generated code as an array of CodeColours</returns>
        private CodeColours[] GenerateCode()
        {
            CodeColours[] generatedCode = new CodeColours[numPegs];
            int           randomNum;

            //Using the random int variable it goes through each entry in the array and assigns it a value of the CodeColours enum equal to the random int
            for (int i = 0; i < generatedCode.Length; i++)
            {
                randomNum        = rnd.Next(1, numColours + 1);
                generatedCode[i] = (CodeColours)randomNum;
            }

            return(generatedCode);
        }
Example #3
0
        /// <summary>
        /// Called when the user is required to enter their guess at the answer code
        /// </summary>
        /// <returns>The user's guess is returned in an array of CodeColour variables</returns>
        public CodeColours[] TakeTurn()
        {
            CodeColours[] playerGuess = new CodeColours[numPegs];

            bool validGuess = false;

            char[] inputArray;

            //Displayed text to the user prompts for them to enter their guess at the answer code
            //Tells the user the length of the required input and the letters that correspond to the available colours
            //This is done in a loop until the user inputs a valid guess
            do
            {
                Console.WriteLine("\nPlease enter your guess at the {0}-digit code:", numPegs);

                //Changes what available colours are displayed to the user to correctly match the numColours setting for the game
                switch (numColours)
                {
                case 2:
                    Console.WriteLine("R = Red, G = Green");
                    break;

                case 3:
                    Console.WriteLine("R = Red, G = Green, B = Blue");
                    break;

                case 4:
                    Console.WriteLine("R = Red, G = Green, B= Blue, Y = Yellow");
                    break;

                case 5:
                    Console.WriteLine("R = Red, G = Green, B = Blue, Y = Yellow, O = Orange");
                    break;

                case 6:
                    Console.WriteLine("R = Red, G = Green, B = Blue, Y = Yellow, O = Orange, P = Purple");
                    break;

                case 7:
                    Console.WriteLine("R = Red, G = Green, B = Blue, Y = Yellow, O = Orange, P = Purple, I = Indigo");
                    break;

                case 8:
                    Console.WriteLine("R = Red, G = Green, B = Blue, Y = Yellow, O = Orange, P = Purple, I = Indigo, V = Violet");
                    break;
                }

                //Takes the inputted string of letters and breaks it down into an array of type char so that each letter is an entry in the array
                inputArray = Console.ReadLine().ToCharArray();

                //First checks that the length of the array matches the numPegs variable
                if (inputArray.Length == numPegs)
                {
                    //Then runs through the array and tries to parse each letter to check that is a value within the CodeColours enum
                    //If all letters successfully parse they are added to the playerGuess array and is considered valid and is passed back to the calling function
                    //If one of the letters does not parse then the input is considered invalid and the loop repeats
                    for (int i = 0; i < numPegs; i++)
                    {
                        if (Enum.TryParse <CodeColours>(inputArray[i].ToString().ToUpper(), out CodeColours colour))
                        {
                            if ((int)(CodeColours)colour >= 1 && (int)(CodeColours)colour <= numColours)
                            {
                                playerGuess[i] = colour;
                                validGuess     = true;
                            }
                            else
                            {
                                validGuess = false;
                                break;
                            }
                        }
                        else
                        {
                            validGuess = false;
                            break;
                        }
                    }
                }
            }while (!validGuess);

            return(playerGuess);
        }