CSE-271: Object-Oriented Programming Homework #2: Word Throw
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CSE-271: Object-Oriented Programming
Homework #2: Word Throw
Maximum Points: 23
Delayed (by no more than 24-hours) submissions earn only 80% credit
● NOTE: Violating of CSE programming style guidelines is an error! Your program should not have any style errors.
● Delayed submission: Only 80% points: Submission delayed by no more than 24-hours will be accepted for a partial credit of maximum 80% of the points.
● Formatting & Documentation: 2 x 3 = 6 points – Reserved for good Javadoc style documentation for each method you implement. If your Javadoc is low quality, then you lose points in this category.
Project Overview:
Word Throw is a word guessing game (similar to hangman) in which a user may guess 1-letter at a time or the whole word. The core working is already implemented in a class called WordThrow.java supplied to you. In addition to the main method, the supplied WordThrow class has 2 guess methods that serve as its API. The interesting part of these two methods is that they don’t return values and instead, they throw exceptions to signify different outcomes. So, are expected to suitably handle exceptions to help a user play the word guessing game.
Starter code:
The WordThrow class does not provide any user interactions. Accordingly, in this project you will be implementing the GameConsole class to provide necessary user interactions as described below. For this project, you have been provided with the following files:
For this project, you have been provided with the following files:
1. word_list.txt: This file contains words from where a word is randomly selected for the user to guess. You should not modify or submit this file.
2. WordThrow.java: This file contains the core logic for the game. You should not modify or submit this file. Review the documentation in the source code, particularly for the two guess methods.
3. **GameConsole.java: This file contains basic starter code. You will be implementing the necessary features in this class, starting with the play method. Of course, you may add as many helper methods or additional static instance variables, as you see fit. It is up to you how you choose to implement the required features.
Directions:
Implement the following features in the GameConsole.java class. The WordThrow class starts using GameConsole.java methods by calling the play method. So, you should start writing codes inside the play method and add new methods (you can have as many method as you like) for different tasks as explained below:
1. Startup operations [4 points]: First you are to determine the number of characters in the secret word by repeatedly calling the guess method from the WordThrow class with words of increasing length (e.g., “*”, “**”, “***”, …) until the guess method stops throwing the custom
WordThrow.WordLengthMismatchException exception (if you study the WordThrow.guess method and understand how it works this instructions should make sense to you easily).
Then initialize the currWord variable with that many *s (for printing on the display later on) right after you figured out the number of characters in the secret word.
Then, Inform the user about the number of characters via the message “Guess the 5 character word.”.
Keep that in mind you don’t want to do all these inside the play method, instead you should create other methods and just call them from the play method.
2. Input loop [3 points]: continue the implementation of the play method by repeating the following steps until the secret word is guessed (you should create helper methods for doing the following):
a. Prompt the user for input via System.out.printfusing INPUT_PROMPT (see the constant variable defined in the starter code). You will lose points if you don’t use printf and the INPUT_PROMPT variable.
b. Read a word from the user (yes, using a Scanner) and perform the following operations (possibly using helper methods) based on the input:
1. Phase 1: If input is a ‘*’ then the loop ends and the play method must return false, because it would be the quit option (check the sample run).
2. Phase 1: If input is a single character do the process-character operation described below.
3. Phase 2: If the input has more than 1-character (it is assumed to be a word), perform the process-word operation described below.
4. Phase 2: If the input is ‘?’ print a hint as described below.
5. Phase 2: If input is a ‘+’ then perform one step of AI as described below.
3. Phase 1: Process-character [5 points]: When the user enters a single letter (a–z) call the WordThrow.guess method to check if-and-where the letter occurs in the secret word and handle the following exceptions:
a. WordThrow.MismatchException: The letter does not occur in the secret word. Simply print the message (as shown in the sample run) in the exception.
b. WordThrow.MatchAndOccursException: The letter occurs 1-or-more times in the word. The exception’s message has the index positions where the letter occurs – e.g., “2 3 5” , i.e., the letter occurs at zero-based index positions 2, 3, and 5 in the secret word. Process this exception as follows:
i. Appropriately print the message (shown on the sample run) using the format in MATCH_MESSAGE variable. Which means you must use printf again.
ii. Next, update the letters in the currWord variable at appropriate index positions. For example, if currWord is “*****” and the input letter by the user is ‘e’ and it occurs at index positions “3 4” , then currWord should be changed to “***ee” . Recollect that the index positions are in the exception’s message – Get the message from the exception (which would be a string value), and connect it to
a scanner object so you can go through it and read numbers. These numbers are indexes inside the currWord that should be changed from * to the correct word e.g ‘e’ from the previous example. So “*****” becomes “***ee” .
iii. If the user has guessed all of the letters in currWord then print the message “You guessed the word! Congratulations!” and the input processing loop must end.
4. Phase 2: Hint [4 points]: If the user enters ‘?’, then print the letters (in alphabetical order) that the user has not guessed and ‘-‘ (a hyphen) for letters that the user has guessed. For example, if the user has already tried to guess all the vowels, then the hint that should be printed is “-bcd-fgh-jklmn-pqrst-vwxyz” . For this functionality, you will always need to track the letters guessed by the user in the process character step (from Phase 1).
5. Phase 2: Process-word [4 points]: If the user enters more than 1-character (i.e., a word), check if the word matches the secret word by calling WordThrow.guess method and handle exceptions as follows:
a. WordThrow.WordLengthMismatchException: Print the message “Your guessed word did not have same length.”
b. WordThrow.CorrectWordException: Print the message “You guessed the word! Congratulations!” and the input processing loop must end.
c. If no exceptions are thrown, then print the message “Good try, but you guessed wrong.”
6. Phase 2: AI step [3 points]: If the user enters the character ‘+’, then your program should find a letter in the secret word. Your code must reuse the process-character functionality (same process as option 3 from phase 1) on behalf of the user.
Testing & Sample output:
In order to ease testing, the starter code enables you to specify a fixed word as a command-line argument. Recollect that command-line arguments are passed as parameters to the main method. You can specify command-line arguments in Eclipse via the Run → Run Configurations … main menu in Eclipse. You can set the command-line arguments in the Arguments tab as shown in the adjacent screenshot. The example outputs have been generated with the command-line argument set to “three” as shown in the adjacent screenshot. Note that user inputs are show in green color:
Phase #1: Basic functionality
Guess the 5 character word.
Word so far: *****
Enter a word or a letter [*: quit, +: AI, ?: hint]: *
The secret word was: three
Number of API calls: 5
Phase #1: Letter guess functionality
Guess the 5 character word.
Word so far: *****
Enter a word or a letter [*: quit, +: AI, ?: hint]: e
You guessed a character correctly!
The character'e' occurs at index position(s) 3 4
Word so far: ***ee
Enter a word or a letter [*: quit, +: AI, ?: hint]: *
The secret word was: three
Number of API calls: 6
Phase #1: Letter guess functionality
Guess the 5 character word.
Word so far: *****
Enter a word or a letter [*: quit, +: AI, ?: hint]: z
The character 'z' is not in the word.
Word so far: *****
Enter a word or a letter [*: quit, +: AI, ?: hint]: q
The character'q'is not in the word.
Word so far: *****
Enter a word or a letter [*: quit, +: AI, ?: hint]: r
You guessed a character correctly!
The character 'r' occurs at index position(s) 2
Word so far: **r**
Enter a word or a letter [*: quit, +: AI, ?: hint]: e
You guessed a character correctly!
The character'e' occurs at index position(s) 3 4
Word so far: **ree
Enter a word or a letter [*: quit, +: AI, ?: hint]: h
You guessed a character correctly!
The character 'h' occurs at index position(s) 1
Word so far: *hree
Enter a word or a letter [*: quit, +: AI, ?: hint]: s
The character's'is not in the word.
Word so far: *hree
Enter a word or a letter [*: quit, +: AI, ?: hint]: t
You guessed a character correctly!
The character't' occurs at index position(s) 0
You guessed the word! Congratulations!
The secret word was: three
Number of API calls: 12
Phase #2: Hint functionality
Guess the 5 character word.
Word so far: *****
Enter a word or a letter [*: quit, +: AI, ?: hint]: ?
Letters left to guess: abcdefghijklmnopqurstuvwxyz
Word so far: *****
Enter a word or a letter [*: quit, +: AI, ?: hint]: e
You guessed a character correctly!
The character'e' occurs at index position(s) 3 4
Word so far: ***ee
Enter a word or a letter [*: quit, +: AI, ?: hint]: i
The character 'i' is not in the word.
Word so far: ***ee
Enter a word or a letter [*: quit, +: AI, ?: hint]: o
The character'o'is not in the word.
Word so far: ***ee
Enter a word or a letter [*: quit, +: AI, ?: hint]: ?
Letters left to guess: abcd-fgh-jklmn-pqurstuvwxyz
Word so far: ***ee
USE DEBUGGER:
Please Please Please Please Please Please Please
use the debugger. Especially for this assignment you will need it more than
ever. Use it to follow your code to see where it’s not working as expected. If
you wrote your code, you should know what each line should be doing,
unless you just throw some code to see what happens and that’s how you
write programs which would be a new way of programming that should be
introduced to the whole community of the programmers all around the
world (just kidding…).
When you run your code, and you are getting unexpected results, or
runtime errors, you can easily use a debugger to follow the code along with
watching the value of the variables to figure out where that error is coming
from. That’s exactly what I do when you send me your code and ask
me why my code is not working.
Frequently asked questions:
***************
Why the style checker keep saying that
Member name 'SECRET' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. WordThrow.java
/homework2/src
line 22
Checkstyle Problem since it's in the WordThrow.java file?
● Don’t worry about it, you can just ignore it. It won’t be a problem later on Code.
***************
How would I get the private or public variables/methods from the WordThrow class into the
GameConsole class and how would I make static references to the non-static methods?
You can’t use the private variables/methods inside your code. That’s why they are private, so others can’t use them.
You can only use public ones and if you recall from cse174 the non-static methods can be only used through an object of that class. If you look at the parameter variable of the play method, you can see that this method gets a WordThrow object through its parameter variable which is called wt, and that’s the object you should use to call public methods/variables.
***************
My code works perfectly on eclipse but when I submit I'm getting an exception NoSuchElement for tests 3, 4, and 5. I keep running it in eclipse and am not getting this error. I am confused where to look so any input would be beneficial.
● It’s a problem with the scanner and the way you are using it to read from the keyboard. Don’t define them in loops and close them at the end of methods. Also, if you define it once in a method, ensure you are not calling that method constantly inside a loop.
***************
● Yes, the only place for using it, is on phase 1 when you want to figure out how many *s you should have inside currWord variable. One way is to use the substring method to grab a value from this constant until it’s enough.
Another way which doesn’t use this constant is to add one * at the time to currWord inside the loop.
Either one is fine and if you don’t use this variable you can remove it from your code.
***************
How to get the length of the Secret word?
So basically you need a loop to keep calling the guess method with "*" and then "**" and "***" and so on. At some point the guess method won't throw any exceptions and that's where you should stop looping and your last string you used to call the guess method will be the value of the currWord. You can call the guess method inside a loop and use the debugger to see what happens yourself.
***************
For phase 2 AI step, it states that we have to reuse the process-character functionality: does this mean we have to call the processes character method within the method we write for the AI step? Or do we just write up something similar to the process character (without calling the method itself) within the AI step?
● You can do either one, but calling the process-character functionality is probably easier than writing another method again to do basically the same thing. This is not a good programming practice. You always want to use your own code as much as you can.
***************
How are we supposed to use the same process-character functionality for the AI part where we don’t need to print “You guessed this character correct!”. So, that means we must change that
method because it will have different output, I’m confused …
Not necessarily, you can make it work for both cases. What I did was I added a Boolean variable to the parameter variables of the process-character function and just put an if condition in front of the printf statement.
***************
2023-09-14