diff --git a/JottoGame.java b/JottoGame.java index f08af55..e62b507 100644 --- a/JottoGame.java +++ b/JottoGame.java @@ -61,9 +61,10 @@ public Optional play() { + "letters: \"B\" and \"I\". "); int attemptsLeft = MAX_GUESSES; - while (attemptsLeft > 0) { - System.out.print("Enter guess: "); - String guess = scanner.nextLine().trim().toUpperCase(); + while (attemptsLeft > 0) { + System.out.println("Remaining number of guesses: " + attemptsLeft); + System.out.print("Enter guess: "); + String guess = scanner.nextLine().trim().toUpperCase(); if (guess.length() != WORD_LENGTH || !guess.matches("[A-Z]+")) { System.out.println("Invalid input. " diff --git a/WordGuessGame.java b/WordGuessGame.java index e40967d..beedd02 100644 --- a/WordGuessGame.java +++ b/WordGuessGame.java @@ -1,4 +1,6 @@ import java.util.Optional; +import java.util.Scanner; + /** * A word guessing game similar to Wordle. @@ -11,7 +13,10 @@ * @version 1 */ class WordGuessGame implements Game { - + int NumberOfGuesses = 6; + Scanner input = new Scanner(System.in); + String secretWord = "APPLE"; + @Override public String getName() { return "Word Guess"; @@ -31,6 +36,26 @@ public Optional play() { "Your score is determined by the number of attempts remaining" + " after you guessed the word correctly!" ); - return Optional.empty(); + + String guess = EnterGuess(); + guess = GuessData(guess); + NumberOfGuesses--; + return Optional.of(NumberOfGuesses); } + public String EnterGuess(){ + System.out.println("enter your 5 letter guess"); + return input.nextLine(); +} + + public String GuessData(String guess){ + int guessLength = guess.length(); + if (guessLength != 5 || !guess.matches("[a-zA-Z0-9]{5}")){ + System.out.println("your guess needs to be 5 letters long"); + guess = EnterGuess(); + guess = GuessData(guess); + return guess; + }else { + return guess; + } +} } diff --git a/WordGuessGameTest.java b/WordGuessGameTest.java index aa85077..e2f11cc 100644 --- a/WordGuessGameTest.java +++ b/WordGuessGameTest.java @@ -27,6 +27,21 @@ public void testCorrectGuessOnFirstTry() { System.setIn(originalIn); } + @Test + public void testIfAllowNumberInput() { + String simulatedInput = "MANG0\n"; + InputStream originalIn = System.in; + System.setIn(new ByteArrayInputStream(simulatedInput.getBytes())); + + WordGuessGame game = new WordGuessGame(); + Optional result = game.play(); + + assertTrue(result.isPresent()); + assertEquals(5, result.get()); + + System.setIn(originalIn); + } + @Test public void testIncorrectThenCorrectGuess() { String simulatedInput = "MANGO\nAPPLE\n";