This site requires JavaScript, please enable it in your browser!
Greenfoot back
BellesStruggle
BellesStruggle wrote ...

2017/6/3

Hangman

1
2
3
BellesStruggle BellesStruggle

2017/6/3

#
So I'm supposed to create a Hangman game using greenfoot. Problem is I don't know how Greenfoot works. Help. So I have the following classes: Player, Word/Text and Gallows. The class Player is supposed to contain the main game logic. Can anyone tell me where best to begin??? I was planning to use an array containing all the words that can be guessed and now I don't know how to "choose" a random word from the list whenever the "new word" button is pressed... Thanks for any possible help.
danpost danpost

2017/6/3

#
BellesStruggle wrote...
I don't know how to "choose" a random word from the list whenever the "new word" button is pressed
A list has a size and each element in the list has an index. So, a random number can be generated to point to a random element.
BellesStruggle BellesStruggle

2017/6/10

#
Oh sorry, I forgot to reply. Thank you. So I'm a quite a bit further now. The basics are running, like having an array list and it chooses a random word etc. But for some reason whenever I try to build in a scanner to get user input, it goes into an infinite loop and breaks down. So I'm trying to use greenfoot.ask to get said input now. However, greenfoot.ask returns a string and not a char as I wanted, so I converted it to a char but now the method IndexOf isn't working with a char but only with an Integer. Is there any way I can get the index of char?? Since i can't very well use integers to guess a word... Thanks for your help!
Super_Hippo Super_Hippo

2017/6/10

#
You could also use
1
2
3
4
5
String key = Greenfoot.getKey();
if (key != null && key.length() == 1)
{
    //...
}
to get the input. Yes, it is still a string, but your words (which should be guessed) are also strings, aren't they?
BellesStruggle BellesStruggle

2017/6/10

#
OH cool I didn't know that command. I tried using it since greenfoot.ask was quite annoying, but it when I start to run the game it keeps opening the ternimal window saying "java.lang.NullPointerException" followed by some other stuff. Is there any chance you might know why?
Super_Hippo Super_Hippo

2017/6/10

#
I think it would be best if you would show what code you have right now since the lines I gave you don't give me any error.
BellesStruggle BellesStruggle

2017/6/10

#
so this is the main part of the code. The things that are being displayed in the world are on german but that shouldnt really matter I guess
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
if (Greenfoot.mouseClicked(this))
        {
            Random random = new Random();
            wordToGuess = myStringArray[(int) (Math.random() * myStringArray.length)];
            wortLaenge = wordToGuess.length();
            getWorld().showText("Das Wort das du erraten musst hat " + wortLaenge + " Buchstaben", 500, 20);
            lettersRemaining = wortLaenge;
 
            for (position = 0; position < wortLaenge; position++) {
                sb.append("_ ");
            }
 
            getWorld().showText(sb.toString(),500,540);
            getWorld().showText("Rate einen Buchstaben!", 500, 40);
 
            while (livesLost < 9)
            {
                String input = Greenfoot.getKey();
                if (input != null && input.length() == 1)
                {
                    guessInWord = (wordToGuess.indexOf(input)) != -1;
 
                    if (guessInWord == false) {
                        livesLost++;
                        totalLives --;
                        getWorld().showText("Sorry, du hast ein Leben verloren, du hast noch " + totalLives + " Leben übrig", 600, 70);
 
                    } else {
                        getWorld().showText("Sehr gut!", 600, 50);
 
                        for (position = 0; position < wortLaenge; position++) {
                            if (wordToGuess == input) {
 
                                getWorld().showText(input, 500, 540);
                                lettersRemaining--;
                            } else {
                                getWorld().showText("_ ", 500, 540);
                            }
                        }
                    }
 
                    prevGuessedLetters = buffer.append(input);
                    getWorld().showText("Bereits geratene Buchstaben: " + prevGuessedLetters, 800, 100);
                }
            }
 
            if (livesLost == totalLives) {
                getWorld().showText("Verloren!", 500, 250);
                Greenfoot.stop();
            } else {
                getWorld().showText("Sehr gut! Das Wort war: " + wordToGuess, 500, 250);
                Greenfoot.stop();
            }
        }
Super_Hippo Super_Hippo

2017/6/10

#
Okay, so your whole scenario is executed in one act cycle... What is 'sb'? Btw, you can use the Greenfoot.getRandomNumber method which is easier to use.
BellesStruggle BellesStruggle

2017/6/10

#
sb is a string buffer which is defined at the beginning. And the getting a random word part isn't the problem. Thats actually the only thing that works XD I press the button, it chooses a random word and displays the according number of "_" and tells me how many letters it has. But once i get t guessing letters, I'm stuck
Super_Hippo Super_Hippo

2017/6/10

#
To what line is the NullPointer Exception pointing? Btw, I think you should remove line 25 and change the next line or it will show that you lost if you didn't lose any life. Anyways, I think you don't have to use complicated things such as StringBuffer.
BellesStruggle BellesStruggle

2017/6/10

#
Its pointing at line 21
Super_Hippo Super_Hippo

2017/6/10

#
So this line?
1
guessInWord = (wordToGuess.indexOf(input)) != -1;
Or line 21 in the class? Also, do you immediately receive the error or after you press a key?
BellesStruggle BellesStruggle

2017/6/10

#
Thats the line. And the error comes before i press anything. just as soon as I click on the button that starts the game aka the one that is being clicked in line 1 of the code above
Super_Hippo Super_Hippo

2017/6/10

#
Okay, insert the following line right before line 21
1
System.out.println("input: "+input+" --- wordToGuess: "+wordToGuess);
and report back what it prints out.
BellesStruggle BellesStruggle

2017/6/10

#
Still the same thing as before, still pointing to line 21. Well 22 no since we added one before.
There are more replies on the next page.
1
2
3