0

I have a 'playGame' method which creates different numbers of human and computer players based on number given. Human and computer players have different implementations of the 'createSecret' and 'makeAGuess' methods.

public void playGame() {
    if(humanPlayers == 2) {
        HumanPlayer codeMaker = new HumanPlayer();
        HumanPlayer codeBreaker = new HumanPlayer();
    }
    else if(humanPlayers == 1) {
        CPUPlayer codeMaker = new CPUPlayer();
        HumanPlayer codeBreaker = new HumanPlayer();
    }
    else {
        CPUPlayer codeMaker = new CPUPlayer();
        CPUPlayer codeBreaker = new CPUPlayer();
    }
    int i = 0;
    secret = codeMaker.createSecret(numberOfColours);
    while(i < numberOfGuesses) {
        guess = codeBreaker.makeAGuess(numberOfColours);
        checkGuess();
        System.out.println("Black pegs: " + blackPegs);
        System.out.println("White pegs: " + whitePegs );
        if(blackPegs == 4) {
            System.out.println("You won the game!");
            return;
        }
        resetPegs();
        i++;
    }
    System.out.println("You lost");
}

The methods work fine until I nest the creation of human and CPU players in if statements, then 'cannot find symbol' error is received. How can I adapt this so that I do not have to repeat the main game code within each if statement?

jp963
  • 77
  • 7
  • Declare the else above the if and then just reassign new values to the variables in if. You need to declare them as their superclass/interface though – Jan Dec 05 '15 at 19:10
  • Check my additional answer on http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean/34115842#34115842 if still not working. – Jan Dec 06 '15 at 09:21
  • @Jan Thank you, declaring them as their interface and rearranging the 'ifs' worked. – jp963 Dec 06 '15 at 13:19

0 Answers0