Skip to content
This repository has been archived by the owner on Feb 21, 2021. It is now read-only.

Hangman updates #18

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ Bugs

There are a number of bugs in the project that you need to fix:

1. If the user inputs a lower-case letter, the program counts it as an incorrect guess even if the word contains the letter.
2. Occasionally, the test for the failure condition (```testLosing```) in ```TestHangman``` fails when run. It doesn't always fail.
3. When the user makes an incorrect guess, the entire hangman diagram is drawn on screen. The game continues to run as expected, but the graphical display is inconsistent with the game state.
4. Two of the tests in ```TestHangmanCanvas``` are failing, specifically ```testNoteIncorrectGuess``` and ```testDisplayWord```.
5. There's an inconsistent, rare crash when starting the game. The stack trace is included in bug #5 on GitHub.

Expand Down
20 changes: 15 additions & 5 deletions src/Hangman.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ public void setUpGame() {
//Generates a random word selected from the HangmanLexicon
private String pickWord() {
hangmanWords = new HangmanLexicon("HangmanLexicon.txt");
int randomWord = rgen.nextInt(0, (hangmanWords.getWordCount()));
int randomWord= rgen.nextInt(0, (hangmanWords.getWordCount() - 1));
String pickedWord = hangmanWords.getWord(randomWord);
for(int i = 0; i < rgen.nextInt(1, 3); i++){
randomWord = rgen.nextInt(0, (hangmanWords.getWordCount() - 1));
pickedWord += " " + hangmanWords.getWord(randomWord);
}
return pickedWord;
}

Expand Down Expand Up @@ -133,20 +137,26 @@ public void step() {

//updates the hiddenWord if the character entered is correct
private void letterCheck() {
//check if the guessed letter has already been tried
for(int i = 0; i < incorrectLetters.length(); i++){
if(incorrectLetters.charAt(i) == ch || incorrectLetters.toLowerCase().charAt(i) == ch){
println(ch + " has already been guessed");
return;
}
}
//checks to see if the guessed letter is in the word
if(word.indexOf(ch) == -1) {
if(word.indexOf(ch) == -1 && word.toLowerCase().indexOf(ch) == -1){
println("There are no " + ch + "'s in the word");
guessCounter--;
incorrectLetters = incorrectLetters + ch;
canvas.noteIncorrectGuess(incorrectLetters);
}
if(word.indexOf(ch) != -1) {
}else{
println("The guess is correct.");
}
//goes through each of the letters in the word and checks if it matches the guessed letter,
//if it's a match, updates the hidden word to reveal the position of the guessed letter
for(int i = 0; i < word.length(); i++) {
if (ch == word.charAt(i)) {
if (ch == word.charAt(i) || ch == word.toLowerCase().charAt(i)) {
if (i > 0) {
hiddenWord = hiddenWord.substring(0, i) + ch + hiddenWord.substring(i + 1);
}
Expand Down
15 changes: 12 additions & 3 deletions src/HangmanCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import acm.graphics.*;

public class HangmanCanvas extends GCanvas {

private GLabel unGuessedWord;
/** Resets the display so that only the scaffold appears */
public void reset() {
drawScaffold();
Expand Down Expand Up @@ -37,10 +37,12 @@ public void displayWord(String word) {
//adds the label with the correctly guessed letters
double x = getWidth()/4;
double y = getHeight() - HEAD_RADIUS*2;
GLabel unGuessedWord = new GLabel(word, x, y);
if(unGuessedWord != null){
remove(unGuessedWord);
}
unGuessedWord = new GLabel(word, x, y);
unGuessedWord.setFont("Halvetica-24");
add(unGuessedWord);

}

/**
Expand All @@ -60,18 +62,25 @@ public void noteIncorrectGuess(String incorrectGuesses) {
switch (incorrectGuesses.length()) {
case 1:
drawHead();
break;
case 2:
drawBody();
break;
case 3:
drawLeftArm();
break;
case 4:
drawRightArm();
break;
case 5:
drawLeftLeg();
break;
case 6:
drawRightLeg();
break;
case 7:
drawLeftFoot();
break;
default:
drawRightFoot();
}
Expand Down
2 changes: 1 addition & 1 deletion test/TestHangmanCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public void testNoteIncorrectGuess() {
int numElementsBefore = canvas.getElementCount();
canvas.noteIncorrectGuess("INCORRECT_GUESS");
int numElementsAfter = canvas.getElementCount();
assertEquals("Guessing incorrectly should add 1 element to the canvas", numElementsBefore + 1, numElementsAfter);
assertEquals("Guessing incorrectly should add 2 element to the canvas", numElementsBefore + 2, numElementsAfter);
}
}