From 0a354d18525eea5e30bbebfe3397b57e9e5dd9c6 Mon Sep 17 00:00:00 2001 From: dcarew Date: Thu, 22 May 2014 08:11:26 -0700 Subject: [PATCH 1/6] letterCheck() is now case insensitive --- src/Hangman.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Hangman.java b/src/Hangman.java index 056fd89..9a4c5d3 100644 --- a/src/Hangman.java +++ b/src/Hangman.java @@ -134,19 +134,18 @@ public void step() { //updates the hiddenWord if the character entered is correct private void letterCheck() { //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); } From 3444844617468673d253ec136cf78c8e0fb64d14 Mon Sep 17 00:00:00 2001 From: dcarew Date: Thu, 22 May 2014 08:24:15 -0700 Subject: [PATCH 2/6] Fixed body being drawn all at once --- src/HangmanCanvas.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/HangmanCanvas.java b/src/HangmanCanvas.java index 935e31b..531fcb2 100644 --- a/src/HangmanCanvas.java +++ b/src/HangmanCanvas.java @@ -60,18 +60,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(); } From 7d3871a4d2861b6700cf4f25c171009290026947 Mon Sep 17 00:00:00 2001 From: dcarew Date: Tue, 27 May 2014 08:19:01 -0700 Subject: [PATCH 3/6] Added New feature #1, incorrect guesses that have already been entered are no longer penalized --- README.md | 2 -- src/Hangman.java | 13 ++++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) 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 9a4c5d3..50ea435 100644 --- a/src/Hangman.java +++ b/src/Hangman.java @@ -62,7 +62,7 @@ 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); return pickedWord; } @@ -78,12 +78,12 @@ private String showNumberOfLetters() { private void getCharGuess() { String getChar = readLine("Your guess: "); - while (true) { + /*while (true) { if(getChar.length() > 1) { getChar = readLine("You can only guess one letter at a time. Try again: "); } if(getChar.length() == 1) break; - } + }*/ ch = getChar.charAt(0); } @@ -133,6 +133,13 @@ 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 && word.toLowerCase().indexOf(ch) == -1){ println("There are no " + ch + "'s in the word"); From c921b6a343f33ed1ca17f54fa4158b3c6ce4846e Mon Sep 17 00:00:00 2001 From: dcarew Date: Tue, 27 May 2014 08:44:05 -0700 Subject: [PATCH 4/6] testNoteIncorrectGuess now passes. Previously didn't account for label to display incorrect guesses --- test/TestHangmanCanvas.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); } } From b4b76a8088be37c5d565e9be6fda6e21688f5508 Mon Sep 17 00:00:00 2001 From: dcarew Date: Tue, 27 May 2014 08:58:14 -0700 Subject: [PATCH 5/6] testDisplayWord now passes. Label containing previous state of guessed letters was not being removed --- src/HangmanCanvas.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/HangmanCanvas.java b/src/HangmanCanvas.java index 531fcb2..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); - } /** From 113e745bb917c4d8b96f0d4a7cf6d632cc68974c Mon Sep 17 00:00:00 2001 From: dcarew Date: Tue, 27 May 2014 09:16:43 -0700 Subject: [PATCH 6/6] Added support for multi word phrases. --- src/Hangman.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Hangman.java b/src/Hangman.java index 50ea435..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() - 1)); + 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; } @@ -78,12 +82,12 @@ private String showNumberOfLetters() { private void getCharGuess() { String getChar = readLine("Your guess: "); - /*while (true) { + while (true) { if(getChar.length() > 1) { getChar = readLine("You can only guess one letter at a time. Try again: "); } if(getChar.length() == 1) break; - }*/ + } ch = getChar.charAt(0); }