Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Hangman winner issue!

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman winner issue!

     
    import java.io.*; //for file
    import java.util.*; //for scanner
     
    public class Lab9 {
        static String userG = "";
        public static void main(String[] args)throws FileNotFoundException{
     
            int wrong = 0; // keeps track of user guesses
            String word = readwords();
            boolean temp = false;
     
            String[] words = new String[word.length()];
            String[] words2 = new String[word.length()];
     
            //methods to be used
            userGuess(word, words, words2);
            printhm(wrong);
     
            //introduce user to game
            System.out.println("Welcome to hangman!");
            System.out.println("Begin guessing letters!");
     
     
     
            //keep track of wrong user guesses  
             for (int i=0; i < words.length; i++){
                words[i] = "-";
            }
     
     
     
            while(wrong < 6 && !checkwon(words)) {   
                temp = add(words, words2);
                printarray(words);
     
                if(temp == false) { 
                    wrong++;
                    printhm(wrong);
                    System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
                    System.out.println();
     
     
     
                }else{    //user guess is correct
     
                    printhm(wrong);
     
                    System.out.println("Correct guess! " + (6 - wrong) + " more guesses left!");
     
                }
            }
        }
        //display hangman
        public static void printhm(int wrong) {
     
            //hangman display 
            if(wrong == 0) {
              System.out.println();
                System.out.println("  ------");
                System.out.println(" |      |");
                System.out.println("        |");
                System.out.println("        |");
                System.out.println("        |");
                System.out.println("        |");
            }
            else if (wrong == 1){
              System.out.println();
                System.out.println("  --------");
                System.out.println(" |        |");
                System.out.println(" 0        |");
                System.out.println("          |");
                System.out.println("          |");
                System.out.println("          |");
            }
            else if (wrong == 2) {
              System.out.println();
                System.out.println("  --------");
                System.out.println(" |        |");
                System.out.println(" 0        |");
                System.out.println(" |        |");
                System.out.println("          |");
                System.out.println("          |");
            }
            else if (wrong == 3) {
              System.out.println();
                System.out.println("  --------");
                System.out.println(" |        |");
                System.out.println(" 0        |");
                System.out.println("/|        |");
                System.out.println("          |");
                System.out.println("          |");
            }
            else if (wrong == 4){
              System.out.println();
                System.out.println("  ---------");
                System.out.println(" |         |");
                System.out.println(" 0         |");
                System.out.println("/|\\       |");
                System.out.println("           |");
                System.out.println("           |");
            }
            else if (wrong == 5){
                System.out.println();
                System.out.println("  ---------");
                System.out.println(" |         |");
                System.out.println(" 0         |");
                System.out.println("/|\\       |");
                System.out.println("/          |");
                System.out.println("           |");
            }
            else if (wrong == 6) {
                System.out.println();
                System.out.println("  ---------");
                System.out.println(" |         |");
                System.out.println(" 0         |");
                System.out.println("/|\\       |");
                System.out.println("/ \\       |");
                System.out.println("           |");
                System.out.println( "You lost! Better luck next time!!" );
            }
     
     
        }
        // keep track of wrong guesses
        public static String readwords() throws FileNotFoundException {  
            Scanner input = new Scanner(new File("hangman.txt"));   
            String[] word = new String[10];  
            String rword = "";  
     
            for(int i = 0; i < word.length; i++){  
                if (input.hasNext())  {
     
                    word[i] = input.next();    
     
                }else {
                    break;  
     
                }
            }        
     
            Random rand = new Random();  
     
            int r = rand.nextInt(9);  
     
            rword = word[r];  
            return rword;  
        } 
     
        public static void userGuess(String y, String[] m, String[] words2){
     
            for (int x = 0; x < y.length(); x++){
                if (x + 1 < y.length())
                    words2[x] = y.substring(x, x + 1);
                else
                    words2[x] = y.substring(x, y.length());
     
            }
        }
     
        public static boolean add(String[] m, String[] ma) {  //find if letters are in the word
            Scanner console = new Scanner(System.in);
            String letters = console.next();
            boolean found = false;
     
            for(int i = 0; i < ma.length; i++) {
              if(ma [i].equals(letters)){
                    m[i] = letters;
                    found = true;
                }
            }
            if(found) {
                return true;
            }
            else {
                return false;
            }
        }
     
        public static boolean checkwon(String[] array) {
          boolean haswon=true;
            for(int i = 0; i < array.length; i++) { 
     
                if (array[i]=="-")
     
                  haswon=false;
            }
     
            return haswon;
     
        }
     
    public static void printarray (String[] array) {
            for(int i = 0; i < array.length; i++) { 
              System.out.print(array[i]);
     
            }
     
        }  
     
    }




    All I need help with is figuring out how to print congratulations when the user wins the game. it is very simple, but I am too worn out to figure it out as I've already tried for about an hour. Please help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hangman winner issue!

    how to print congratulations when the user wins
    if (didUserWin()) {
      print congrats message
    }
    The write the method: didUserWin() that returns true when the user has won.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman winner issue!

    Quote Originally Posted by Norm View Post
    if (didUserWin()) {
      print congrats message
    }
    The write the method: didUserWin() that returns true when the user has won.
    public static boolean didUserWin() {
          if (didUserWin()) {
            System.out.println("Congratulations! You won!");
     
            return didUserWin;
          }

    Why won't this work? I'm doing what you said but it keeps giving errors.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hangman winner issue!

    it keeps giving errors.
    Please copy the full text of the error messages and paste it here.

    My code was an example, not something that can be copied and pasted into your program. You will need to write your own methods.
    What does checkwon() do?
    Has it been tested? Call it with some arrays (both winners and losers) and see if it returns the correct value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Tic Tac Toe - Checking for a winner
    By FrederikB in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 4th, 2013, 11:04 AM
  2. Declaring a winner in a Race Car Program
    By csharp100 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: September 30th, 2012, 01:46 PM
  3. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  4. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 06:04 AM

Tags for this Thread