Results 1 to 8 of 8
  1. #1
    mouufloww is offline Member
    Join Date
    Apr 2017
    Posts
    4
    Rep Power
    0

    Default Help with simple Hangman Game

    Hi Guys, so I have to develop a Hangman Game in BlueJ (Java) for school (11th grade). I am pretty much ready, but I have 2 problems:
    - I was not able to insert the "hanged guy"
    - The counter of how many tries you have left is going up, if there are for example 3 times the letter e in a word it raises up 3.

    Here´s my program: ( The comments are in german and the word list form the internet as well :D)

    Java Code:
    import java.util.Scanner;                   
    import java.util.Random;                    
    import java.util.LinkedList;                
    import java.net.*;
    import java.io.*;
    
    public class Hangman
    {
        private static int falsch;
        
        public static void main(String[] args)
        {
            Scanner scanny = new Scanner(System.in);
            Random zufallsgenerator = new Random();
            LinkedList<String> Eingabe = getwordlist();  //gesuchtes Wort = eines aus der Wortliste
            
            boolean inGame = true;                                      //solange diese Variable auf true ist, läuft das Spiel. Sobald sie false wird, endet das Spiel
            
                while(inGame)                                           //Spieleloop
                {
                    System.out.println("Willkommen zu Galgenmännchen.");
                    int Versuche = 0;
                    char[] randomWord = Eingabe.get(zufallsgenerator.nextInt(Eingabe.size())).toCharArray();    //.toCharArray() --> Bsp.: Galgenmännchen -> G,a,l,g,e,n,m,ä,n,n,c,h,e,n
                    int Versuchsanzahl = 7;                                                                     //Insgesamt 7 Versuche                                                                                 
                    char[] playerGuess = new char[randomWord.length];                                           //bildet zusammen mit dem for Loop die Unterstriche für das Wort, und zwar genau so viele, wie lang das Wort ist
                    
                   
                        for(int i=0; i < randomWord.length; i++)
                        {
                            playerGuess[i] = '_';
                        }
            
            
                   boolean wordisguessed = false;                      //Solange diese variable auf false ist, wurde das Wort nicht erraten. Sobald sie true wird, hat man das Spiel gewonnen
                                                     
                  
                   while(!wordisguessed && Versuche != Versuchsanzahl)                    //während das Wort noch nicht erraten wurde und die Versuche nicht gleich der Versuchsanzahl (7) sind, wird.....
                   {
                       printArray(playerGuess);
                       System.out.printf("Sie haben %d Versuche übrig.\n", Versuchsanzahl - Versuche);
                       System.out.println("Geben Sie nun einen Buchstabe ein:");
                                    
                       char input = scanny.nextLine().charAt(0);
                                    
                       Versuche++;   
                                
                       if (input == '-')           //wenn der Buchstabe "-" ist, endet das Spiel
                       {
                           inGame = false;
                           wordisguessed = true;
                       }
                       else                        
                       {
                           for(int i=0; i < playerGuess.length; i++)
                           {
                               if (randomWord[i] == input)         //wenn der Buchstabe richtig ist ...
                               {
                                   playerGuess[i] = input;         //Buchstabe wird eingetragen
                                   Versuchsanzahl = Versuchsanzahl +2-i;
                                          
                               }
                           }
                       } 
                   }
                                    
                        
                                                
                if (isTheWordGuessed(playerGuess))          
                {
                    wordisguessed = true;                                   //wenn das wort erraten wurden, Text: Du hast gewonnen
                    System.out.println("Glückwunsch, du hast gewonnen");
                }
                          
                             
                if (!wordisguessed)                 //wenn das Wort nicht erraten wurde.....                                         
                {
                    System.out.println("Du hast keine Versuche mehr");  
                    System.out.println("Willst du nochmal spielen? (ja/nein)");     //erneutes Spiel?
                    String naechstesSpiel = scanny.nextLine();                      //nächste Zeile lesne (ja/nein)
                             
                    if (naechstesSpiel.equals("nein"))                              //wenn nein....
                    {
                        inGame = false;                                             //Ende des Spiels. Alles andere -> neues Spiel
                        System.out.println("Game over!!!");
                    }
                }
            }
        }
        
                     
                        
                    
        public static void printArray(char[] array)
        {
            for(int i=0; i < array.length; i++)
            {
                System.out.print(array[i] + " ");
            }
        }
                        
                            
                            
        public static boolean isTheWordGuessed(char[] array)
        {
            for(int i=0; i < array.length; i++)
            {
                if (array[i] == '_') 
                {
                    return false;
                }
            }
            return true;          
        }
          
          
        
        public static LinkedList<String> getwordlist()                  //Alle Wörter der Liste (http://www.netzmafia.de/software/wordlists/deutsch.txt) werden heruntergeladen und aus diesen wird ein zufälliges ausgewählt
        {
            LinkedList<String> list = new LinkedList<String>();
              
            try
            {
                URL wordlist = new URL("http://www.netzmafia.de/software/wordlists/deutsch.txt");
                BufferedReader in = new BufferedReader(new InputStreamReader(wordlist.openStream()));
                String inputLine;         
                while ((inputLine = in.readLine()) != null)
                {
                    list.add(inputLine);
                }            
                in.close();
            }   
            catch (Exception e)
            {
                e.printStackTrace();
            }                
            return list;
        }
    }

    Please help me guys :D Thank you <3
    Last edited by mouufloww; 04-22-2017 at 10:50 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with simple Hangman Game

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mouufloww is offline Member
    Join Date
    Apr 2017
    Posts
    4
    Rep Power
    0

    Default Re: Help with simple Hangman Game

    Thanks for the tipp (I´m new to the site)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with simple Hangman Game

    Can you post the program's output that shows what you are talking about?
    Add comments to describe the problems (<<<< HERE IT SHOULD BE 4)
    and show what you want the output to be.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mouufloww is offline Member
    Join Date
    Apr 2017
    Posts
    4
    Rep Power
    0

    Default Re: Help with simple Hangman Game

    Help with simple Hangman Game-unbenannt.png -> Beginning

    Help with simple Hangman Game-unbenannt1.png -> wrong letter

    Help with simple Hangman Game-unbenannt2.png -> correct letter ( you can see the counter going up from 6 to 8 because i had 3 correct e´s...)

    Help with simple Hangman Game-unbenannt3.png -> end (didn´t guess all the letters correct)


    I want to insert a hangman and for every wrong letter there should be the next body part like in a normal hanggame...

    Next problem: counter goes up if there are like more than one correct letter(3 times e)

    Last problem: How can I show, what the correct word would have been after you failed guessing it??

    Thanks for your Help!! :D

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with simple Hangman Game

    While debugging the code several values should be printed as the code executes:
    the word to guess
    the count

    How can I show, what the correct word would have been after you failed guessing it??
    What is wrong with printing it like all the other messages the program prints?

    I don't read German and don't understand the program's print outs and the variable names.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    mouufloww is offline Member
    Join Date
    Apr 2017
    Posts
    4
    Rep Power
    0

    Default Re: Help with simple Hangman Game

    If I do like:
    System.out.println ("The correct word was: " +randomWord) it turns into: the correct word was: c6o9_dsg or something like that (non sense)

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help with simple Hangman Game

    Look at the String class to see how to convert a char array to a String.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Hangman Game
    By getshum in forum New To Java
    Replies: 1
    Last Post: 02-26-2011, 04:01 AM
  2. Hangman Game Help Please
    By 9tjh in forum New To Java
    Replies: 4
    Last Post: 12-04-2009, 03:19 AM
  3. Need Some Help, Simple Loop For Hangman Game
    By Juo in forum New To Java
    Replies: 2
    Last Post: 11-14-2009, 07:51 PM
  4. Hangman Game..
    By iPetey in forum New To Java
    Replies: 4
    Last Post: 05-07-2009, 02:24 PM
  5. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •