string.indexOf(letter) checks for the first occurence. what can check for all occurences
public class Hangman extends ConsoleProgram {
private static final int MAX_NUMBER = 9;
private RandomGenerator rgen = new RandomGenerator();
HangmanLexicon lexicon = new HangmanLexicon ();
private static boolean end;
private HangmanCanvas canvas;
private static int x = 30;
private static int z = 1;
private static String[] blank = new String[100];
private static String it = " ";
private static String blessed = " ";
public void init() {
canvas = new HangmanCanvas();
add(canvas);
}
public void run() {
println("Welcome to the Hangman!");
println ("You have 8 guesses left");
println("ONE LETTER AT A TIME");
// Resets the screen
canvas.reset();
//Randomly generates a word from the lexicon
int guessNo = rgen.nextInt(0, 9);
String word = lexicon.getWord(guessNo);
// initializing the no of guesses
int noOfGuesses = 1;
//let the game begin
while(noOfGuesses < MAX_NUMBER && end == false){
String usersletter = readLine("Your guess: " );
String userlet = usersletter.toUpperCase();
/*ATTENTION*/ /* checking if only one letter was typed */
int check = userlet.length();
if (check > 1) {
println("Easy Dude, One Letter at a time.");
/* some code missing */
}
char letter = userlet.charAt(0);
String library = "";
library += letter;
noOfGuesses++;
int y = getHeight() - 20;
canvas.libraryPost(library, x, y);
x += 10;
int index = word.indexOf(userlet); //where the letter is in the word
//creating an array of the word length
if (z == 1){
for (int i=0; i<word.length();i++){
blank[i] = "-";
}
}
z++;
//if the letter is present in the word
if (index != -1 ){
println("The guess is correct");
noOfGuesses = noOfGuesses -1;
blank[index] = userlet;
String finale = " ";
for (int k = 0; k<word.length(); k++){
finale += blank[k];
blessed = finale;
}
it = blessed;
println("The word now looks like this: " + it );
}else if (index == -1){
String finale = " ";
for (int k = 0; k<word.length(); k++){
finale += blank[k];
blessed = finale;
}
it = blessed;
canvas.noteIncorrectGuess(letter);
println("There are no " + letter + "\'s in the word " );
println("The word now looks like this: " + it );
}
int guessesLeft = MAX_NUMBER - noOfGuesses;
println("You have " + guessesLeft+ " guesses left");
//winning
String check1 = "-";
int what = it.indexOf(check1);
if (what == -1){
println("You have guessed all the letters in the word correctly");
println("You Win TaDa");
end = true;
}
if (noOfGuesses == MAX_NUMBER){
println("You are completely hung");
println("The word was "+ word);
println("Game Over");
}
}
}
}
//////////////////////////////////////////////////////////////////////
import acm.graphics.*;
public class HangmanCanvas extends GCanvas {
private static int noOfWrongGuess;
/** Resets the display so that only the scaffold appears */
public void reset() {
scaffold();
}
/**
* Updates the display to correspond to an incorrect guess by the
* user. Calling this method causes the next body part to appear
* on the scaffold and adds the letter to the list of incorrect
* guesses that appears at the bottom of the window.
*/
public void noteIncorrectGuess(char letter) {
++noOfWrongGuess;
if (noOfWrongGuess == 1){
firstPart();
}else if (noOfWrongGuess == 2){
secondPart();
}else if (noOfWrongGuess == 3){
thirdPart();
}else if (noOfWrongGuess == 4){
fourthPart();
}else if (noOfWrongGuess == 5){
fifthPart();
}else if (noOfWrongGuess == 6){
sixthPart();
}else if (noOfWrongGuess == 7){
seventhPart();
}else if (noOfWrongGuess == 8){
eightPart();
}
}
/* Constants for the simple version of the picture (in pixels) */
private static final int SCAFFOLD_HEIGHT = 360;
private static final int BEAM_LENGTH = 144;
private static final int ROPE_LENGTH = 18;
private static final int HEAD_RADIUS = 36;
private static final int BODY_LENGTH = 144;
private static final int ARM_OFFSET_FROM_HEAD = 28;
private static final int UPPER_ARM_LENGTH = 72;
private static final int LOWER_ARM_LENGTH = 44;
private static final int HIP_WIDTH = 36;
private static final int LEG_LENGTH = 108;
private static final int FOOT_LENGTH = 28;
private void scaffold(){
GLine pole = new GLine (30,30, 30, SCAFFOLD_HEIGHT );
add(pole);
GLine horiPole = new GLine (30,30, BEAM_LENGTH + 30, 30);
add(horiPole);
GLine rope = new GLine (BEAM_LENGTH + 30 ,30, BEAM_LENGTH + 30, ROPE_LENGTH + 30);
add(rope);
}
private void firstPart(){
GOval head = new GOval ((BEAM_LENGTH + 30) -HEAD_RADIUS/2, ROPE_LENGTH + 30 ,HEAD_RADIUS, HEAD_RADIUS );
add(head);
}
private void secondPart(){
GLine body = new GLine (BEAM_LENGTH + 30, (ROPE_LENGTH + HEAD_RADIUS)+ 30, BEAM_LENGTH + 30, (ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 );
add(body);
}
private void thirdPart(){
GLine upperarm1 = new GLine ((BEAM_LENGTH + 30) - UPPER_ARM_LENGTH/2, (ARM_OFFSET_FROM_HEAD+ROPE_LENGTH + HEAD_RADIUS + 30), (BEAM_LENGTH + 30), (ARM_OFFSET_FROM_HEAD+ROPE_LENGTH + HEAD_RADIUS + 30));
add(upperarm1);
GLine lowerarm1 = new GLine((BEAM_LENGTH + 30) - UPPER_ARM_LENGTH/2, (ARM_OFFSET_FROM_HEAD+ROPE_LENGTH + HEAD_RADIUS + 30),(BEAM_LENGTH + 30) - UPPER_ARM_LENGTH/2 ,(ARM_OFFSET_FROM_HEAD+ROPE_LENGTH + HEAD_RADIUS + LOWER_ARM_LENGTH + 30));
add(lowerarm1);
}
private void fourthPart(){
GLine upperarm2 = new GLine ((BEAM_LENGTH + 30), (ARM_OFFSET_FROM_HEAD+ROPE_LENGTH + HEAD_RADIUS + 30), (BEAM_LENGTH + 30) + UPPER_ARM_LENGTH/2, (ARM_OFFSET_FROM_HEAD+ROPE_LENGTH + HEAD_RADIUS + 30) );
add(upperarm2);
GLine lowerarm2 = new GLine((BEAM_LENGTH + 30) + UPPER_ARM_LENGTH/2, (ARM_OFFSET_FROM_HEAD+ROPE_LENGTH + HEAD_RADIUS + 30), (BEAM_LENGTH + 30) + UPPER_ARM_LENGTH/2, (ARM_OFFSET_FROM_HEAD+ROPE_LENGTH + HEAD_RADIUS + LOWER_ARM_LENGTH + 30));
add(lowerarm2);
}
private void fifthPart(){
GLine hip = new GLine ((30+BEAM_LENGTH - HIP_WIDTH/2),((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 ), (30+BEAM_LENGTH + HIP_WIDTH/2), ((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 ));
add(hip);
}
private void sixthPart(){
GLine leg1 = new GLine((30+BEAM_LENGTH - HIP_WIDTH/2), ((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 ), (30+BEAM_LENGTH - HIP_WIDTH/2), ((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 )+LEG_LENGTH);
add(leg1);
}
private void seventhPart(){
GLine leg2 = new GLine((30+BEAM_LENGTH + HIP_WIDTH/2), ((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 ), (30+BEAM_LENGTH + HIP_WIDTH/2), ((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 )+LEG_LENGTH);
add(leg2);
}
private void eightPart(){
GLine feet1 = new GLine ((30+BEAM_LENGTH - HIP_WIDTH/2), ((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 )+LEG_LENGTH, (30+BEAM_LENGTH - HIP_WIDTH/2) - FOOT_LENGTH, ((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 )+LEG_LENGTH);
add(feet1);
GLine feet2 = new GLine ((30+BEAM_LENGTH + HIP_WIDTH/2), ((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 )+LEG_LENGTH, (30+BEAM_LENGTH + HIP_WIDTH/2)+ FOOT_LENGTH, ((ROPE_LENGTH + HEAD_RADIUS+ BODY_LENGTH)+ 30 )+LEG_LENGTH);
add(feet2);
}
public void libraryPost(String library, int x, int y){
GLabel lib = new GLabel(library, x ,y);
add (lib);
}
}
//////////////////////////////////////////////////////////////////////////////////
import acm.util.*;
public class HangmanLexicon {
/** Returns the number of words in the lexicon. */
public int getWordCount() {
return 10;
}
/** Returns the word at the specified index. */
public String getWord(int index) {
switch (index) {
case 0: return "BUOY";
case 1: return "COMPUTER";
case 2: return "CONNOISSEUR";
case 3: return "DEHYDRATE";
case 4: return "FUZZY";
case 5: return "HUBBUB";
case 6: return "KEYHOLE";
case 7: return "QUAGMIRE";
case 8: return "SLITHER";
case 9: return "ZIRCON";
default: throw new ErrorException("getWord: Illegal index");
}
}
}