Exemple #1
0
        //use menu to select which child codebreaker will be used.
        public Game(int _menuSelect)
        {
            switch (_menuSelect)
            {
            case 1:
                cb = new Human();
                break;

            case 2:
                cb = new FGA();
                break;

            case 3:
                cb = new GA();
                break;

            default:
                break;
            }
            //setup board
            b.setup(13);
            //get codemaker to create its code
            cm.createCode();
        }
Exemple #2
0
        /// <summary>
        /// Constructor for class, initialises variables and creates objects of other classes
        /// </summary>
        public Game()
        {
            isPlaying = true;

            //Generates new random seed to be used throughout the program
            rnd = new Random();
            int tempPegNum, tempColourNum;

            //Sets initial settings for the game
            numPegs    = 4;
            numColours = 6;
            numGuesses = 12;

            do
            {
                gameFinished = false;
                int menuChoice;

                //Calls method to display the main menu to the user, stores the returned int as the menu choice variable
                menuChoice = MainMenu();

                tempPegNum    = numPegs;
                tempColourNum = numColours;

                //Using the menu choice variable in a switch statement the program creates the correct objects for the chosen gamemode
                switch (menuChoice)
                {
                case 1:
                    //Creates an instance of the Player class for the user to play as the codebreaker
                    player   = new Player(numPegs, numColours);
                    gameType = GameType.HumanvsAI;
                    break;

                case 2:
                    //Creates an instance of the CodeBreaker class for the AI as the codebreaker
                    //Also caps the number of pegs and colours available for the AI codebreaker, this is to avoid overly long processing times and out of bounds errors with higher settings
                    if (tempPegNum > 6)
                    {
                        tempPegNum = 6;
                    }

                    if (tempColourNum > 7)
                    {
                        tempColourNum = 7;
                    }

                    codeBreaker = new CodeBreaker(rnd, tempPegNum, tempColourNum);
                    gameType    = GameType.AIvsAI;
                    break;

                case 5:
                    //If the user selects to exit the program, the console window is closed
                    Environment.Exit(0);
                    break;
                }

                //Regardless of the game mode chosen by the user, instances of the CodeMaker and GameBoard classes are created, passing the game settings
                codeMaker = new CodeMaker(rnd, tempPegNum, tempColourNum);
                gameBoard = new GameBoard(codeMaker, tempPegNum, numGuesses);

                //PlayGame method is finally called which starts the game
                PlayGame();
                Console.ReadLine();
            }while (isPlaying);
        }