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

2019/6/20

I need help with hangman

coder123 coder123

2019/6/20

#
here's my code for all my classes.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Scanner;
/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game extends Actor
{
    /**
     * Act - do whatever the Game wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static int mistakes = 0;
    private Scanner a = new Scanner(System.in);
    public static String guessWord;
    public Game() {
        guessWord = getGuessWord(guessWord);
    }
    public void act() 
    {
        String hiddenWord = getHiddenWord(guessWord);
        while(!guessWord.equals(hiddenWord)) {
            printHiddenWord(hiddenWord);
            char guessLetter = getGuessLetter();
            hiddenWord = updateHiddenWord(guessWord, hiddenWord, guessLetter);
            Greenfoot.delay(100);
        }
    }    
    private String getGuessWord(String guessWord) {
    	guessWord = Greenfoot.ask("Enter the word to guess in the game of hangman:");
    	return guessWord;
    }
    private String getHiddenWord(String guessWord) {
    	String hiddenWord = "";
    	for (int i = 0; i < guessWord.length(); i++) {
    		hiddenWord += "*";
    	}
    	return hiddenWord;
    }
    private void printHiddenWord(String hiddenWord) {
    	getWorld().showText("The current hidden word is: \n" + hiddenWord, 406, 167);
    }
    private char getGuessLetter() {
		String b = Greenfoot.ask("Enter a letter to guess on the hidden word");
    	return b.charAt(0);
    }
    private String updateHiddenWord(String guessWord, String hiddenWord, char guessLetter) {
    	String newHiddenWord = hiddenWord;
    	for (int i = 0; i < hiddenWord.length(); i++) {
    		if (guessLetter == guessWord.charAt(i)) {
    			newHiddenWord = newHiddenWord.substring(0,i) + guessLetter + newHiddenWord.substring(i+1);
    		}
    	}
    	if (newHiddenWord.equals(hiddenWord)) mistakes++;
		return newHiddenWord;
    }
    private void printGameCompleteMessage(String guessWord) {
    	getWorld().showText("You have guessed all the letters in the word \"" + guessWord + "\" Nicely done!", 406, 167);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Hangman_Image here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hangman_Image extends Actor
{
    /**
     * Act - do whatever the Hangman_Image wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private final Game a = new Game();
    public void act() 
    {
        if (a.mistakes == 1) setImage("Hangman1.png");
        else if (a.mistakes == 2) setImage("Hangman2.png");
        else if (a.mistakes == 3) setImage ("Hangman3.png");
        else if (a.mistakes == 4) setImage ("Hangman4.png");
        else if (a.mistakes == 5) setImage ("Hangman5.png");
        else if (a.mistakes == 6) {
            setImage("Hangman6.png");
            getWorld().showText("The word was " + a.guessWord + "!", 300, 200);
            Greenfoot.stop();
        }
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Hangman_Image hangman_image = new Hangman_Image();
        addObject(hangman_image,123,191);
        Game game = new Game();
        addObject(game,406,167);
    }
}
when I run this, the correct guessed letter won't pop up until you guess another, and the hangman statue won't change. can somebody help?
danpost danpost

2019/6/20

#
One big problem is that the Game object in the field a in your Hangman_Image class is not the same one as that which is in the world. So, anything the Hangman_Image object does to its Game object will not be reflected in the world.
coder123 coder123

2019/6/20

#
So, how do I fix that?
coder123 coder123

2019/6/20

#
here's my new code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Hangman_Image hangman_image = new Hangman_Image();
        addObject(hangman_image,123,191);
        addObject(hangman_image.a,406,167);
    }
}
all I did was call the field a in the Hangman_Image class
coder123 coder123

2019/6/20

#
nothing changed
danpost danpost

2019/6/20

#
coder123 wrote...
<< Code Omitted >> all I did was call the field a in the Hangman_Image class
nothing changed
Did you give it public access?
coder123 coder123

2019/6/20

#
I forgot to make the variable static. Oops
coder123 coder123

2019/6/20

#
Still doesn’t work
coder123 coder123

2019/6/21

#
Can somebody pleeeeeease help me? I need this done by tomorrow
danpost danpost

2019/6/21

#
coder123 wrote...
I forgot to make the variable static. Oops
Probably should not be static. In fact, I do not think anything in your project should be static. Your a field should not be final either.
coder123 coder123

2019/6/21

#
Tks I will try that
coder123 coder123

2019/6/21

#
here's my most recent code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Scanner;
/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game extends Actor
{
    /**
     * Act - do whatever the Game wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static int mistakes = 0;
    private Scanner a = new Scanner(System.in);
    public static String guessWord;
    private static String hiddenWord;
    public Game() {
        getGuessWord();
        getHiddenWord();
    }
    public void act() 
    {
        while(!guessWord.equals(hiddenWord)) {

                //Greenfoot.delay(100);
                char guessLetter = getGuessLetter();
            
            hiddenWord = updateHiddenWord(guessLetter);
        }
        printGameCompleteMessage(guessWord);
    }    
    private void getGuessWord() {
        guessWord = Greenfoot.ask("Enter the word to guess in the game of hangman:");
    }
    private void getHiddenWord() {
        String hiddenWord = "";
            for (int i = 0; i < guessWord.length(); i++) {
            hiddenWord += "*";
        }
    }
    private void printHiddenWord() {
        getWorld().showText("The current hidden word is: \n" + hiddenWord, 406, 167);
    }
    private char getGuessLetter() {
        // printHiddenWord();
        String b = Greenfoot.ask("Enter a letter to guess on the hidden word");
        char guessLetter = b.charAt(0);
        return guessLetter;
    }
    private String updateHiddenWord(char guessLetter) {
        String newHiddenWord = hiddenWord;
        for (int i = 0; i < guessWord.length(); i++) {
            if (guessLetter == guessWord.charAt(i)) {
                newHiddenWord = newHiddenWord.substring(0,i) + guessLetter + newHiddenWord.substring(i+1);
            }
        }
        /*if (newHiddenWord.equals(hiddenWord)) {
            mistakes++;
        }*/
        return newHiddenWord;
    }
    private void printGameCompleteMessage(String guessWord) {
        getWorld().showText("You have guessed all the letters in the word \"" + guessWord + "\" Nicely done!", 406, 167);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Hangman_Image here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hangman_Image extends Actor
{
    /**
     * Act - do whatever the Hangman_Image wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    public Hangman_Image() {
        setImage("Hangman.png");
    }
    public void act() 
    {
        if (Game.mistakes == 0) setImage("Hangman.png");
        if (Game.mistakes == 1) setImage("Hangman1.png");
        if (Game.mistakes == 2) setImage("Hangman2.png");
        if (Game.mistakes == 3) setImage ("Hangman3.png");
        if (Game.mistakes == 4) setImage ("Hangman4.png");
        if (Game.mistakes == 5) setImage ("Hangman5.png");
        if (Game.mistakes == 6) {
            setImage("Hangman6.png");
            getWorld().showText("The word was " + Game.guessWord + "!", 300, 200);
            Greenfoot.stop();
        }
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Hangman_Image hangman_image = new Hangman_Image();
        //addObject(hangman_image,123,191);
        addObject(new Game(),406,167);
    }
}
nothing is changing
coder123 coder123

2019/6/21

#
I get an error: java.lang.NullPointerException at Game.getHiddenWord(Game.java:39) at Game.<init>(Game.java:21) at MyWorld.prepare(MyWorld.java:31) at MyWorld.<init>(MyWorld.java:20) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at greenfoot.core.Simulation.newInstance(Simulation.java:617) at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$7(WorldHandlerDelegateIDE.java:430) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502) at greenfoot.core.Simulation.maybePause(Simulation.java:305) at greenfoot.core.Simulation.runContent(Simulation.java:218) at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2019/6/21

#
coder123 wrote...
I get an error: << Error Trace Omitted >>
Okay -- quick fix. Make the two methods called from the constructor static (at lines 34 and 37).
coder123 coder123

2019/6/22

#
I got my code to work on my own. Thanks for the help btw
You need to login to post a reply.