Hello, I am almost finished with my Hangman game, but I have two major problems. The first is that I get an infinite loop whenever I click on any letter (I click to see if the letter is found in the hangman phrase) that is part of the unknown phrase.
My second problem is replacing the underscores with the letters if they are correct. Also, I don't know how to detect if I have won the game or not.
Any help is greatly appreciated.
Here's my code for the Mask class (The unknown phrase):
(It is a subclass of the imported Label class. I have not changed the Label class.)
Here's the code for the World class, in case it helps:
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | import greenfoot.*; import java.awt.Color; /** * The mask is a label that displays underscores to hide letters of the phrase. */ public class Mask extends Label { private String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; public String phrase; private String mask = "" ; String newMask; public Mask(String msg) { super (msg, 25 ); setFillColor(Color.BLACK); setPhrase(msg); } /** * Resets the phrase and creates a new mask. */ public void setPhrase(String msg) { phrase = msg; mask = createMask(); setValue(mask); } /** * Replaces each letter of the phrase with an underscore. */ public String createMask() { for ( int pos = 0 ;pos<phrase.length();pos++){ char symbol = phrase.charAt(pos); //If it's a space, it isn't in alphabet. It's not a letter. if (ALPHABET.indexOf(Character.toString(symbol).toUpperCase())==- 1 ){ mask+=symbol+ " " ; } else { mask+= "_ " ; } } //add code here return mask; } /** * accessor method */ public String getMask() { return mask; } /** * Replaces all underscores in the mask that corresponds to the * letter parameter and returns true is that letter was found in the phrase * or false if the letter was not found in the phrase. */ public boolean showLetter(String letter) { phrase = phrase.toUpperCase(); int pos = phrase.indexOf(letter); if (pos != - 1 ) { do { //Cut the "a" pos = phrase.indexOf(letter); //Make a new string and take the digit out. //add code here newMask = phrase.substring( 0 ,pos)+phrase.charAt(pos)+phrase.substring(pos); } while (pos != - 1 ); setValue(mask); } else { return false ; } return true ; } /** * The player has won if there are no more underscores in the phrase. */ public boolean checkForWin() { return true ; //add code here to check if all blanks have been filled } } |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.*; import java.io.*; public class OldWest extends World { private String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; private Mask theMask; private String theMessage; private Scaffold scaffold; private Label result; public OldWest() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 800 , 400 , 1 ); scaffold = new Scaffold(); addObject(scaffold, getWidth()/ 2 , getHeight()/ 2 ); addLetters(); theMessage = "all's well that ends well." ; theMask = new Mask(theMessage); addObject(theMask,getWidth()/ 2 ,getHeight()- 70 ); } /** * Adds the letter tiles to the top of the world */ private void addLetters() { for ( int pos = 0 ;pos<ALPHABET.length();pos++){ addObject( new Letter(ALPHABET.substring(pos,pos+ 1 )), pos* 31 + 13 , 30 ); } } /** * After a letter tile has been clicked, check to see if the letter * is in the phrase. If it is, reveal it in the mask. Otherwise, display the * next image on the scaffold. */ public void checkForLetter(String letter) { if (theMask.showLetter(letter)) { if (theMask.checkForWin()) { result = new Label( "You win" , 50 ); addObject(result,getWidth()/ 2 ,getHeight()/ 2 ); } } else { if (!scaffold.endOfGame()) { scaffold.nextImage(); } else { result = new Label( "Game Over. You Lose" , 50 ); addObject(result,getWidth()/ 2 ,getHeight()/ 2 ); } } } } |