Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Called once the Player or CodeBreaker class have entered a guess at the answer code
        /// Compares the guess with the answer to produce a response which is returned to the calling method
        /// </summary>
        /// <param name="playerGuess">The code guess as an array of CodeColour enum values</param>
        /// <returns>Returns the response as an array of AnswerColours enum values</returns>
        public AnswerColours[] CheckGuess(CodeColours[] playerGuess)
        {
            //The AnswerColours array which will be filled and returned as the response to the code guess
            AnswerColours[] guessAnswer = new AnswerColours[numPegs];

            //Creates lists which will hold the values from the playerGuess array and codeAnswer array
            List <CodeColours> guessList = new List <CodeColours>(), answerList = new List <CodeColours>();
            List <int>         blackPegIndexes = new List <int>();

            bool removedFromList;
            int  numRemoved = 0, numRight = 0;

            //First adds the entries from the arrays into the lists
            for (int i = 0; i < numPegs; i++)
            {
                guessList.Add(playerGuess[i]);
                answerList.Add(codeAnswer[i]);
            }

            //Next checks for any black peg repsonses; where a colour peg from the guess is in the same position as a peg from the answer
            //Done by looping through both lists and looks for the same CodeColour value and the same index
            //If found then a AnswerColours.B value is added to the response array and the current index is added to the integer list blackPegIndexes
            for (int i = 0; i < guessList.Count; i++)
            {
                for (int x = 0; x < answerList.Count; x++)
                {
                    if (guessList[i] == answerList[x] && i == x)
                    {
                        guessAnswer[numRight] = AnswerColours.B;
                        blackPegIndexes.Add(x);
                        numRight++;
                        break;
                    }
                }
            }

            //Once all black peg matches have been found, those pegs are removed from the guess and answer lists
            //Doing this eliminates pegs that have already generated black pegs responses from also generating white peg responses
            //Done by using the integer values in the blackPegIndexes list to do RemoveAt functions on the two lists
            for (int i = 0; i < blackPegIndexes.Count; i++)
            {
                guessList.RemoveAt(blackPegIndexes[i] - numRemoved);
                answerList.RemoveAt(blackPegIndexes[i] - numRemoved);
                numRemoved++;
            }

            //Finally check for white peg responses; where a colour peg in the guess is present in the answer but is not in the same position
            //Done by looping through both lists and checking for the same CodeColour value in each list
            //If a match is found the a white peg value is added to the response array and the value from the guess and answer lists are removed at the indexes
            //When entries are removed from the lists, the for loops are broken out of and the process is repeated within the do loop
            //this avoids entries being skipped over due to index numbers changing
            do
            {
                removedFromList = false;
                for (int i = 0; i < guessList.Count; i++)
                {
                    for (int x = 0; x < answerList.Count; x++)
                    {
                        if (guessList[i] == answerList[x])
                        {
                            guessAnswer[numRight] = AnswerColours.W;
                            guessList.RemoveAt(i);
                            answerList.RemoveAt(x);

                            numRight++;
                            removedFromList = true;
                            break;
                        }
                    }
                    if (removedFromList)
                    {
                        break;
                    }
                }
            }while (removedFromList);

            guessAnswer = guessAnswer.OrderBy(x => rnd.Next()).ToArray();
            return(guessAnswer);
        }