jeudi 2 juin 2016

Win Detection For Hangman Game Not Working?

I'm almost finished with a program to simulate a Hangman game, but I can't figure out why my method of Win Detection (Having a counter variable corLetters which is incremented every time a correct letter is printed, and then including the if-statement:

if(corLetters == word.length())

followed by the actions to notify the user of a win isn't working.

For reference, this occurs:

Followed yet again by "What letter do you guess?" and so on, with every guess from there out registering as incorrect. Here are my classes:

My Main Class:

import java.util.Scanner;
import java.util.Random;

public class Main 
{

    public static void main(String[] args) 
    {
        Hangman manHang = new Hangman();    
    }

}

My Hangman Class:

import java.util.Random;


public class Hangman
{
    Random r;
    GetData get;
    String[] Bank = {"consider","minute","accord","evident","practice","intend","concern","commit","issue","approach","establish","utter","conduct","engage","obtain","scarce","policy","straight","stock","apparent","property","fancy","concept","court","appoint","ambiguous","arbitrary","alliteration","arrogant","benevolent","belligerent","boycott","cynical","connotation","cessation","contemporary","craving","grandiose","gratuitous","guile","harbinger","impetuous","incandescent","indigent","inexorable","injunction","insipid","insurgent","languish","magnate","abjure","abrogate","abstemious", "acumen", "antebellum","auspicious","belie","bellicose","bowdlerize","chicanery","chromosome","churlish","circumlocution","circumnavigate","deciduous","deleterious","diffident","enervate","enfranchise","epiphany","equinox","evanescent","expurgate","facetious", "fallacious"};
    String word;//Stores the random word used
    int incGuessCount = 0;
    int corLetters = 0;
    boolean[] lettersFound;//Used to mark which letters have been found
    String guessedLetter=" ";//Used to store guesses
    boolean gameOver = false;


    public Hangman()
    {
        r = new Random();
        get = new GetData();
        word=Bank[r.nextInt(Bank.length)]; //Selects a random word and assigns word the value
        lettersFound = new boolean[word.length()]; //Creates a boolean array the length of the word


        do
        {
            drawGallows(); //Show the Gallows depending on how many incorrect guesses there are
            displayWord();
            getGuess();
            checkGuess();
        }
        while(incGuessCount<5 && gameOver == false);

        if (incGuessCount>=5)
        {

            fiveWrong();//Displays full Hangman

        }

        if(corLetters == word.length())
            gameOver = true;

        if (incGuessCount<5 && gameOver)
        {
            System.out.println("\u000c");
            System.out.println("Congratulations!");
            System.out.println("You have won!");
            System.out.println("Rerun the program to try again.");
        }


    }

    public void getGuess()
    {
        System.out.println("\u000C");
        System.out.println(" ");
        System.out.println("What letter do you guess?");
        System.out.println("You have "+(5-incGuessCount)+" guesses left.");
        System.out.print("Enter guess:");
        guessedLetter = get.aWord();//Uses scanners to take in the guesses
    }


    public boolean displayWord()            
    {
        boolean goodGuess = false;//Assumes guess is bad automatically
        char letter = guessedLetter.charAt(0);

        for(int i = 0;i<word.length();i++)//Goes through all the letters to check guess's status
            if (lettersFound[i]==true)//Checks if a letter was already revealed at that position
            {
                System.out.print(word.charAt(i)+" ");
                corLetters++;
            }

            else if (word.charAt(i)==letter)//Prints the correctly guessed letter at the position
            {
                System.out.print(word.charAt(i)+" ");
                lettersFound[i] = true;
                goodGuess = true;
                corLetters++;
            }
            else//Fills in non-applicable spaces with an underscore
                    System.out.print("_ ");


        return goodGuess;           
    }   

    public void checkGuess() {
         boolean disW = displayWord();

         if (!disW && incGuessCount == 5)
             fiveWrong();
         else if (!disW && incGuessCount < 5) {
             incGuessCount++;

         }
    }


    public void defaultMan()
    {
        System.out.println('\u000C');
        System.out.println(" ________");
        System.out.println(" |      | ");
        System.out.println(" |      | ");                        
        System.out.println(" |      | ");                 
        System.out.println(" |       ");                   
        System.out.println(" |_________ ");
        System.out.println(" ");
    }

    public void oneWrong()
    {
        System.out.println('\u000C');
        System.out.println(" ________");
        System.out.println(" |      | ");
        System.out.println(" |      | ");                        
        System.out.println(" |      | ");                 
        System.out.println(" |       \\ ");                   
        System.out.println(" |_________ ");
        System.out.println(" ");
    }

    public void twoWrong()
    {
        System.out.println('\u000C');
        System.out.println(" ________");
        System.out.println(" |      | ");
        System.out.println(" |      | ");                        
        System.out.println(" |      | ");                 
        System.out.println(" |     / \\ ");                   
        System.out.println(" |_________ ");
        System.out.println(" ");
    }

    public void threeWrong()
    {
        System.out.println('\u000C');
        System.out.println(" ________");
        System.out.println(" |      | ");
        System.out.println(" |      | ");                        
        System.out.println(" |     /| ");                 
        System.out.println(" |     / \\ ");                   
        System.out.println(" |_________ "); 
        System.out.println(" ");
    }

    public void fourWrong()
    {
        System.out.println('\u000C');
        System.out.println(" ________");
        System.out.println(" |      | ");
        System.out.println(" |      | ");                        
        System.out.println(" |     /|\\ ");                 
        System.out.println(" |     / \\ ");                   
        System.out.println(" |_________ ");
        System.out.println(" ");
    }

    public void fiveWrong()
    {
        System.out.println('\u000C');
        System.out.println(" ________");
        System.out.println(" |      | ");
        System.out.println(" |     ( ) ");                        
        System.out.println(" |     /|\\ ");                 
        System.out.println(" |     / \\ ");                   
        System.out.println(" |_________ ");
        System.out.println(" ");
        System.out.println("You have lost! The word was "+word+".");
        System.out.println("Rerun the program to try again.");
        gameOver=true;
    }

    public void drawGallows()
    {
        if(incGuessCount==0)
        {
            defaultMan();                   
        }

        if(incGuessCount==1)
        {
            oneWrong();                   
        }

        if(incGuessCount==2)
        {
            twoWrong();                  
        }

        if(incGuessCount==3)
        {
             threeWrong();                 
        }

        if(incGuessCount==4)
        {
            fourWrong();                   
        }

        if(incGuessCount==5)
        {
            fiveWrong();

        }

    }
} 

Also, the GetData Class:

import java.util.Scanner;

public class GetData
{ 
  private Scanner input; 

  public GetData()//Produces a scanner to take in input
  { input = new Scanner(System.in); } 

  public String aWord()//Gets the input as a guess/string
  { return input.next(); } 

  public int aNumber()//Gets the input as a number
  { return input.nextInt(); }

}

Thanks :)

Aucun commentaire:

Enregistrer un commentaire