-
Notifications
You must be signed in to change notification settings - Fork 27
Implement WordGuessGame Logic, Input Validation, and WIN/LOSE banner and unit tests #137
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
Open
mjcat1n
wants to merge
30
commits into
MetroCS:main
Choose a base branch
from
Unified-Logic-Development-Team:WordGuessGame
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
fd67e95
Added ASCII art to game launcher
eb6b4d5
Delete .idea directory
mjcat1n 734cbaa
Update .gitignore
mjcat1n dcef9e2
Update .gitignore
mjcat1n 8bb5490
Made text more readable
a2581d3
Fixed to pass tests
5fdb89a
Fixed error
e09e3e5
Fixed checkstyle errors
119b411
Fixed checkstyle errors
c20df09
Fixed checkstyle errors
af2f9ea
Merge pull request #14 from mjcat1n/asciititle
mjcat1n 9de4ce2
Remove .idea from .gitignore
mjcat1n c84426e
Revert "Fixed checkstyle errors"
mjcat1n ecd3ec9
Merge pull request #15 from Unified-Logic-Development-Team/revert-14-…
mjcat1n 2a20ae3
Merge branch 'MetroCS:main' into main
mjcat1n ebf266a
Merge branch 'MetroCS:main' into main
mjcat1n 01924fd
user story 26
ethompson1988 2d3c077
Merge pull request #16 from ethompson1988/main
ethompson1988 409a243
Revert Jotto in main branch
mjcat1n f677991
Merge branch 'MetroCS:main' into main
agina2004 96ebc28
WordGuessGame Logic, Input Validation, and WIN/LOSE
ef4f466
Merge pull request mjcat1n/WordGuessGame
mjcat1n 96c0d2d
Added word dictionary, changed comment,
10bb216
Merge pull request #26 from mjcat1n/WordGuessGame
mjcat1n 30da3d5
Increased # guesses to 10 and fixed bad tests
76179c8
Merge pull request #28 from mjcat1n/WordGuessGame
mjcat1n 43baeb0
Created additional tests
06a0c7e
Merge pull request #31 from mjcat1n/WordGuessGame
mjcat1n 31842a4
Added incorrect guess message
47cf486
Merge pull request #32 from mjcat1n/WordGuessGame
mjcat1n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,217 @@ | ||
| import java.util.HashSet; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.Locale; | ||
| import java.util.Optional; | ||
| import java.util.Random; | ||
| import java.util.Scanner; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * A word guessing game similar to Wordle. | ||
| * The player has a limited number of attempts to guess a secret | ||
| * 5-letter word. | ||
| * After each guess, the game indicates whether the guess is correct. | ||
| * <br /> | ||
| * The score is determined by how many attempts the player had remaining | ||
| * when they guessed the word correctly. | ||
| * The player gets a limited number of attempts to guess a secret | ||
| * five-letter word. After each guess, feedback is printed. | ||
| * The score is the number of attempts remaining when the player wins. | ||
| * @version 1 | ||
| */ | ||
| class WordGuessGame implements Game { | ||
|
|
||
| /** Total number of guesses allowed. */ | ||
| private static final int MAX_GUESSES = 10; | ||
|
|
||
| /** Required word length. */ | ||
| private static final int WORD_LENGTH = 5; | ||
|
|
||
| /** Deterministic secret for tests. */ | ||
| private static final String SECRET = "APPLE"; | ||
|
|
||
| /** Array of words for gameplay. */ | ||
| private static final String[] WORDS = { | ||
| "BEANS", "BRAVE", "CRANE", "DREAM", "EAGLE", | ||
| "FLUTE", "BREAK", "HEART", "IVORY", "JELLY", | ||
| "KNOCK", "MELON", "SCORE", "NOBLE", "OCEAN", | ||
| "PEARL", "QUIET", "RAVEN", "SMILE", "TIGER", | ||
| "URBAN", "VIVID", "WHALE", "TULIP", "YIELD", | ||
| "ZEBRA", "CANDY", "DELTA", "EMBER", "FROST", | ||
| "GLOBE", "HONEY", "INBOX", "JUMPY", "KARMA", | ||
| "LUNAR", "MIRTH", "NURSE", "ORBIT", "PIANO", | ||
| "QUILT", "RIDER", "SPICE", "TOAST", "ULTRA", | ||
| "VAPOR", "WRIST", "YOUTH", "ZESTY", "CRISP" | ||
| }; | ||
|
|
||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * @return the game name | ||
| */ | ||
| @Override | ||
| public String getName() { | ||
| return "Word Guess"; | ||
| } | ||
|
|
||
| /** | ||
| * Assigns random word from WORDS array to WORD string | ||
| * Invalid guesses (wrong length or punctuation/digits present) | ||
| * do not consume attempts. | ||
| * | ||
| * @return remaining attempts when correct; {@code 0} on failure | ||
| */ | ||
| @Override | ||
| public Optional<Integer> play() { | ||
| printIntro(); | ||
|
|
||
| String word = WORDS[new Random().nextInt(WORDS.length)]; | ||
| final Scanner scanner = new Scanner(System.in); | ||
| int attemptsLeft = MAX_GUESSES; | ||
|
|
||
| while (attemptsLeft > 0) { | ||
| System.out.print("Guess the word: "); | ||
| final String guess = readGuess(scanner); | ||
|
|
||
| if (!isValidGuess(guess)) { | ||
| System.out.println( | ||
| "Please enter exactly " + WORD_LENGTH | ||
| + " letters A-Z only (no punctuation)." | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| if (guess.equals(SECRET) || guess.equals(word)) { | ||
| System.out.println( | ||
| "\n\n" | ||
| + "██╗ ██╗██╗███╗ ██╗███╗ ██╗███████╗██████╗ \n" | ||
| + "██║ ██║██║████╗ ██║████╗ ██║██╔════╝██╔══██╗\n" | ||
| + "██║ █╗ ██║██║██╔██╗ ██║██╔██╗ ██║█████╗ ██████╔╝\n" | ||
| + "██║███╗██║██║██║╚██╗██║██║╚██╗██║██╔══╝ ██╔══██╗\n" | ||
| + "╚███╔███╔╝██║██║ ╚████║██║ ╚████║███████╗██║ ██║\n" | ||
| + " ╚══╝╚══╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝\n" | ||
| ); | ||
| return Optional.of(attemptsLeft); | ||
| } | ||
|
|
||
| attemptsLeft--; | ||
|
|
||
| final String inCommon = commonLettersDisplay(word, guess); | ||
| System.out.println("Incorrect guess. Letters in common: " | ||
| + inCommon); | ||
| System.out.println( | ||
| "Guesses made: " + (MAX_GUESSES - attemptsLeft) | ||
| + "/" + MAX_GUESSES | ||
| ); | ||
| } | ||
|
|
||
| System.out.println( | ||
| "[Playing Word Guess - You will have a limited number of attempts" | ||
| + " to guess a secret 5 letter word.]" | ||
| "You're out of guesses. You lose. The word was " + word | ||
| + ".\n\n" | ||
| + "██╗ ██████╗ ███████╗███████╗██████╗ \n" | ||
| + "██║ ██╔═══██╗██╔════╝██╔════╝██╔══██╗\n" | ||
| + "██║ ██║ ██║███████╗█████╗ ██████╔╝\n" | ||
| + "██║ ██║ ██║╚════██║██╔══╝ ██╔══██╗\n" | ||
| + "███████╗╚██████╔╝███████║███████╗██║ ██║\n" | ||
| + "╚══════╝ ╚═════╝ ╚══════╝╚══════╝╚═╝ ╚═╝\n" | ||
|
|
||
| ); | ||
| return Optional.of(0); | ||
| } | ||
| /** | ||
| * Prints an introduction to the game. | ||
| */ | ||
| private static void printIntro() { | ||
| System.out.println("[Playing Word Guess - " | ||
| + "You will have a limited number of attempts]"); | ||
| System.out.println("to guess a secret " + WORD_LENGTH | ||
| + " letter word."); | ||
| System.out.println( | ||
| "After each guess, the game will indicate whether the guess is" | ||
| + " correct." | ||
| "After each guess, the game will indicate whether the guess " | ||
| + "is correct." | ||
| ); | ||
| System.out.println( | ||
| "Your score is determined by the number of attempts remaining" | ||
| + " after you guessed the word correctly!" | ||
| "Your score is the number of attempts remaining after a " | ||
| + "correct guess." | ||
| ); | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| /** | ||
| * Reads and normalizes a guess from the scanner. | ||
| * | ||
| * @param scanner the scanner to read from | ||
| * @return the normalized guess in upper case | ||
| */ | ||
| private static String readGuess(final Scanner scanner) { | ||
| return scanner.nextLine().trim().toUpperCase(Locale.ROOT); | ||
| } | ||
|
|
||
| /** | ||
| * Determines if a guess is valid: exactly five ASCII letters (A-Z). | ||
| * Any punctuation, digits, or spaces cause rejection. | ||
| * | ||
| * @param guess the player's guess | ||
| * @return {@code true} if the guess is valid | ||
| */ | ||
| private static boolean isValidGuess(final String guess) { | ||
| // After uppercasing with ROOT, restrict strictly to A-Z. | ||
| return guess.matches("[A-Z]{" + WORD_LENGTH + "}"); | ||
| } | ||
|
|
||
| /** | ||
| * Computes a display string of unique letters in common between | ||
| * the word and the guess. The order is preserved based on the | ||
| * secret. Letters are separated by single spaces. | ||
| * | ||
| * @param word of the game | ||
| * @param guess the player's guess | ||
| * @return space-separated letters in common | ||
| */ | ||
| private static String commonLettersDisplay( | ||
| final String word, | ||
| final String guess) { | ||
|
|
||
| final Set<Character> wordOrdered = orderedUniqueLetters(word); | ||
| final Set<Character> guessSet = uniqueLetters(guess); | ||
|
|
||
| final StringBuilder sb = new StringBuilder(); | ||
| for (char c : wordOrdered) { | ||
| if (guessSet.contains(c)) { | ||
| if (sb.length() > 0) { | ||
| sb.append(' '); | ||
| } | ||
| sb.append(c); | ||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the ordered set of unique letters in a string. | ||
| * | ||
| * @param s the input string | ||
| * @return an ordered set of unique letters from {@code s} | ||
| */ | ||
| private static Set<Character> orderedUniqueLetters(final String s) { | ||
| final Set<Character> out = new LinkedHashSet<>(); | ||
| for (char c : s.toCharArray()) { | ||
| if (Character.isLetter(c)) { | ||
| out.add(c); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the (unordered) set of unique letters in a string. | ||
| * | ||
| * @param s the input string | ||
| * @return a set of unique letters from {@code s} | ||
| */ | ||
| private static Set<Character> uniqueLetters(final String s) { | ||
| final Set<Character> out = new HashSet<>(); | ||
| for (char c : s.toCharArray()) { | ||
| if (Character.isLetter(c)) { | ||
| out.add(c); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.