diff --git a/README.md b/README.md index 1450b2f..56e13ee 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Hangman.java b/src/Hangman.java index 056fd89..00fc67a 100644 --- a/src/Hangman.java +++ b/src/Hangman.java @@ -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; } @@ -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); } diff --git a/src/HangmanCanvas.java b/src/HangmanCanvas.java index 935e31b..51adcba 100644 --- a/src/HangmanCanvas.java +++ b/src/HangmanCanvas.java @@ -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(); @@ -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); - } /** @@ -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(); } diff --git a/test/TestHangmanCanvas.java b/test/TestHangmanCanvas.java index 2833f1f..24773a7 100644 --- a/test/TestHangmanCanvas.java +++ b/test/TestHangmanCanvas.java @@ -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); } }