7 Replies - 6815 Views - Last Post: 05 August 2012 - 02:50 PM Rate Topic: -----

#1 selinium_salt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-August 12

How can I break a loop when an entire array of booleans is true?

Posted 05 August 2012 - 08:28 AM

So I am one step away from being done with this program, and the last thing I need to do is break a loop when every element of the array is true.

Here is the code:
 import java.util.Scanner;

class hangman{

    //assigning the five words that will be used in this game
    public static String[] words = {"GIRAFFE", "THESIS", "LEOPARD", "PEANUT", "PILLOW"};

    //main function
    public static void main(String[] args){

	//setting up scanner
	Scanner scanner = new Scanner (System.in);

	//welcome message
	System.out.print("Welcome to hangman! Would you like to play (y/n)? ");
	char reply = scanner.nextLine().charAt(0);

	//lets users exit program if they don't want to play
	if (reply=='n') System.exit(0);
	if (reply!='y') {
	    System.out.println("Sorry, that is not a valid statement. Please try again.");
	    System.exit(0);
	}

	System.out.println("Ok, let's play!");
	System.out.println();

	//chooses random word
	String word2Guess;
	word2Guess = words[(int)(Math.random()*words.length)];
	
	//defining booleans
	boolean[] alphabet = new boolean[26];
	boolean[] answer = new boolean[word2Guess.length()];

	//gives the number of guesses a user has.
	int tries = 7;
	while (tries > 0){
		System.out.println("You have " + tries +"/7 tries left");

	//asks user for guess
	System.out.println("Please enter your guess: ");
	char guess = scanner.nextLine().charAt(0);
	
	//makes guess a capital letter
	if (guess>='a' && guess<='z'){
	    guess +=('A'-'a');
	}

	//calls checkGuess
	boolean gotItRight = checkGuess(answer, word2Guess, guess, alphabet);

	//calls printAlphabet function
	System.out.println("Your available letters are: ");
	printAlphabet(alphabet);

	//calls printLines function
	printLines(word2Guess, answer, guess);
	
	
			if (!checkGuess(answer, word2Guess, guess, alphabet)){
				tries--;
			}
			for (int i=0; i<word2Guess.length(); i++){
			if(printLines(word2Guess, answer, guess) == true ){
				System.out.println("Congratulations, you won!");
				break;
			}
			}
		    if (tries==0){
			System.out.println();
			System.out.println("You ran out of chances, game over!");
			System.exit(0);
		    } 
		
	}
    }


//checkGuess will evaluate the user's guess
	public static boolean checkGuess(boolean[] answer, String word2Guess, char guess, boolean[] alphabet){ 
	    boolean found = false; //This boolean tells us if we have found the letter
	    for (int i=0; i<word2Guess.length(); i++){ 
	        if (guess == word2Guess.charAt(i)){ 
	            answer[i] = true; 
	            found = true;
	        } 
	    }
	    if (!found) { 
	        System.out.println("I'm sorry, that guess isn't correct."); 
	    }
	
	    //Mark off the letter, newline and return the found letter status
	    alphabet[guess - 'A'] = true;
	    System.out.println();
	    return found;
	}


    //printLines gives the appropriate number of underscores
    public static void printLines(String word2Guess, boolean[] answer, char guess){
	for (int i=0; i<word2Guess.length(); i++){
	    if (answer[i] == false){
			System.out.print("_");
	}
		if (answer[i] == true){
			System.out.print(word2Guess.charAt(i));
		}
	}
	System.out.println();
    }
		
    //printAlphabet will print the letters that are availiable to guess
    public static void printAlphabet(boolean[] alphabet){
		for (int i=0; i<26; i++){
		if(!alphabet[i]){
			 System.out.print((char)('A' + i));
	    }
		else {
			System.out.print(" ");
		}
		}
	System.out.println();
}
} 


So in this code,
 for (int i=0; i<word2Guess.length(); i++){
			if(printLines(word2Guess, answer, guess) == true ){
				System.out.println("Congratulations, you won!");
				break;
			} 


That is what I'm looking at, and what I've tried so far, but I'm not sure how to evaluate it. I pretty much just need a success statement.

Is This A Good Question/Topic? 0
  • +

Replies To: How can I break a loop when an entire array of booleans is true?

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How can I break a loop when an entire array of booleans is true?

Posted 05 August 2012 - 08:30 AM

Just yes or no: Can you use StringBuilder?
Was This Post Helpful? 0
  • +
  • -

#3 selinium_salt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-August 12

Re: How can I break a loop when an entire array of booleans is true?

Posted 05 August 2012 - 08:35 AM

no
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How can I break a loop when an entire array of booleans is true?

Posted 05 August 2012 - 08:51 AM

Okay. That would have made life easier, but we can live without it.

In fact, your approach,

if( printLines(word2Guess, answer, guess) == true )

Should work, but printLines() will need to be modified to return a boolean.

And, the if condition does not need the '==', just

if( printLines(word2Guess, answer, guess) )

will do.

So, modify printLines() so that it determines if the word2Guess has been completely guessed or filled in, and then return true if it has, false if not.

Let us know if you need help.
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,980
  • Joined: 20-September 08

Re: How can I break a loop when an entire array of booleans is true?

Posted 05 August 2012 - 11:36 AM

Quote

public static void printLines(String word2Guess, boolean[] answer, char guess) {

What is the purpose of 'guess' here?
Was This Post Helpful? 0
  • +
  • -

#6 selinium_salt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-August 12

Re: How can I break a loop when an entire array of booleans is true?

Posted 05 August 2012 - 02:15 PM

Ok, so I got it to work but there is one more bug and I can't figure it out.
So now the program will stop and say "congrats" if you guess the entire word, but it also says that if you just guess the last letter. So if the word were Thesis, and I guessed 's', it would dispay:

_ _ _ S _ S
Congratulations, you won!

code for loop and function:

loop:
for (int i=0; i<word2Guess.length(); i++){
			if(doesItWork){
				System.out.println("Congratulations, you won!");
				System.exit(0); 


function:
    public static boolean printLines(String word2Guess, boolean[] answer, char guess){
	boolean lines = false;
	for (int i=0; i<word2Guess.length(); i++){
	    if (answer[i] == false){
			lines = false;
			System.out.print("_");
	}
		if (answer[i] == true){
			lines = true;
			System.out.print(word2Guess.charAt(i));
		}
	}
	System.out.println();
	return lines;
    } 

Was This Post Helpful? 0
  • +
  • -

#7 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,980
  • Joined: 20-September 08

Re: How can I break a loop when an entire array of booleans is true?

Posted 05 August 2012 - 02:21 PM

Hypothesize that the guesses are correct and falsify that if and when necessary:


    public static boolean printLines(String word2Guess, boolean[] answer,
        char guess) {
        boolean allCorrect = true;

        for (int i = 0; i < word2Guess.length(); i++) {
            if (answer[i] == false) {
                allCorrect = false;
                System.out.print("_");
            }

            if (answer[i] == true) {
                System.out.print(word2Guess.charAt(i));
            }
        }

        System.out.println();

        return allCorrect;
    }


Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: How can I break a loop when an entire array of booleans is true?

Posted 05 August 2012 - 02:50 PM

I built a String object, result, of the same characters generated by the if blocks in printLines() and then

return !result.contains( "_" );
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1