-
Notifications
You must be signed in to change notification settings - Fork 27
[USER STORY] Add input validation to WordGuessGame #25 #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d62c849
0d34043
d0ebecc
998b951
34cf9bd
879e103
3d705cf
da7e85d
601a087
7f53469
720cc0e
849e003
aff990c
6d1a78a
c74c435
bdc24a5
a254d59
0fdc580
d53d3ff
df68c6a
7b23d26
d71b4e8
5b478a1
a15af0d
804a873
397bc13
ff9d621
535af72
ed048be
828a1ee
832236a
173188a
2eb983b
f52bffd
a7dec0d
f1ae98c
3e2b9e2
c9b4083
77933fd
7b3abd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Integer> 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}")){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are numerals included in input validation? The Acceptance Criteria for #25 indicates that only "alphabetic" inputs are accepted and more specifically: "reject inputs that contain numbers". |
||
| System.out.println("your guess needs to be 5 letters long"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the guess is length 5 but contains non-letters, this is a misleading error message. |
||
| guess = EnterGuess(); | ||
| guess = GuessData(guess); | ||
| return guess; | ||
| }else { | ||
| return guess; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<Integer> result = game.play(); | ||
|
|
||
| assertTrue(result.isPresent()); | ||
| assertEquals(5, result.get()); | ||
|
Comment on lines
+39
to
+40
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this test whether input is valid or not? |
||
|
|
||
| System.setIn(originalIn); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIncorrectThenCorrectGuess() { | ||
| String simulatedInput = "MANGO\nAPPLE\n"; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does input validation to WordGuessGame involve changes to JottoGame.java?