here's my code for all my classes.
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?
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);
}
}
