-1

I am currently making a hangman game in Java as a learning experience because I am fairly new to the Java language. I've done this project before in Python when I was first learning that and am now basically mirroring it in Java. Most hangman games people make seem to not actually show the body hung from the gallows every time you make a guess; this is the most practical way to make it as it is a console game. However, when I made the game in Python, and plan to do in Java as well, I actually printed the body out to the console. I very well understand this is a little silly because you only have characters to work with.

Now I get to the real question: what would be the best way to do this? In Python I had -- and I'm not exaggerating -- over 1000 lines of code with tons of if-elif statements with endless amounts of print statements to print out the various ways the body could be represented. This was an absolute mess, I know. The reason was because I had an settings menu in the game where you could change the amount of wrong guesses you had. As a result, more body parts were needed to be added at a more gradual rate. That is, instead of just the standard head, torso, arms (2), and legs (2) -- so 6 body parts in total --, I had, for example, head, eyes (2), nose, mouth, torso, arms (2), legs (2) -- so 10 body parts in total -- as an option.

Just to outdo myself, I'd like the player of my game to be able to select a number between 0 and 18 for the max amount of wrong guesses. Now as much as I'd love to place hundreds of calls to println() in deeply nested switch or if blocks (sarcasm), I'm curious if there's a better way to do this. That is, a more elegant way to calculate what body parts need to be printed and print them.

Thanks in advance.

RobertR
  • 745
  • 9
  • 27

1 Answers1

1

I have included a portion of code from my hangman game that uses a switch to determine what to print. The hangman is simply made of multiple line breaks and a few other simple characters. Here's a screenshot:

enter image description here

switch (numberOfIncorrectGuesses)
        {
            case 1:
                hangman = "\n" + "\n|" + "\n|" + "\n|" + "\n|" + "\n|" + "\n|_______________________\n";
                break;
            case 2:
                hangman = "\n_________" + "\n|" + "\n|" + "\n|" + "\n|" + "\n|" + "\n|_______________________\n";
                break;
            case 3:
                hangman =  "\n_________" + "\n|                   |" + "\n|" + "\n|" + "\n|" + "\n|" + "\n|_______________________\n";
                break;
            case 4:
                hangman =  "\n_________" + "\n|                   |" + "\n|                  O" + "\n|" + "\n|" + "\n|" + "\n|_______________________\n";
                break;
            case 5:
                hangman =  "\n_________" + "\n|                   |" + "\n|                  O" + "\n|                   |" + "\n|" + "\n|" + "\n|_______________________\n";
                break;
            case 6:
                hangman =  "\n_________" + "\n|                   |" + "\n|                  O" + "\n|               ---|" + "\n|" + "\n|" + "\n|_______________________\n";
                break;
            case 7:
                hangman =  "\n_________" + "\n|                   |" + "\n|                  O" + "\n|               ---|---" + "\n|" + "\n|" + "\n|_______________________\n";
                break;
            case 8:
                hangman = "\n_________" + "\n|                   |" + "\n|                  O" + "\n|               ---|---" + "\n|                  /" + "\n|                /" + "\n|_______________________\n";
                break;
            case 9:
                JOptionPane.showMessageDialog(null, author + "Incorrect" + "\n_________" + "\n|                   |" + "\n|                  O" + "\n|               ---|---" + "\n|                  /\\" + "\n|                /    \\" + "\n|_______________________" + "\nSorry, you lost.\nThe stick man is Dead!\nThe secret word was: \"" + secretWord + "\"");
  • Ahh yes I didn't even think of just making a `String` and putting newlines in it. I think I'll take this and try to make it work for my game. Thanks. – RobertR Dec 01 '15 at 15:37