StarBlind Game
In this assignment you will create the StarBlind game which is based on the Mastermind board game.
In your game, the computer will, at random, select colors for four stars. The user will guess the colors of the stars and receive feedback on the accuracy of their guess. Here is an example of the game play for the program:
Rules
- The user guesses by entering four letters representing the star colors (R - red, G - green, B - blue, Y - yellow, M - magenta, and C - cyan).
- The order of the letters matters
- Colors can be expressed with either uppercase or lowercase letters.
- If the user enters an invalid guess (not exactly four characters or containing letters other than R, G, B, Y, M, or C), a useful message must be displayed and the user will be given another opportunity to guess. This continues until a valid guess is entered.
- Green and yellow bars are used to provide feedback to the user on their guess. All green bars should appear prior to any yellow bars.
- Stars and Bars should be displayed in bold text. You can produce bold characters by prefixing the characters with \u001B[1m.
- The game continues until the user guesses all four colors correctly.
- Once the user wins, the user can choose to play again or end the program.
Software Design
You will make use of the Color
enumeration from homework 10 with the addition
of three class methods:
String colorString(Color color, String text)
— returns a string that contains the appropriate ANSI escape codes to make thetext
bold and in the specifiedcolor
. The end of the returned string should include the "Reset" escape code.Color getColor()
— returns one of the following colors randomly and with equal probability: R, G, B, C, M, and Y.Color getColor(char color)
— returns the color specified by the argument passed to the method. Valid characters (uppercase or lowercase) include R, G, B, C, M, Y, W for white, and K for black. If any other character is passed to the method, the method should return the "color" corresponding to "Reset".
There are three additional classes for you to implement.
Stars
The Stars
class represents a guess made by the player (the four colored stars). This class
is required to have three public constructors and two methods:
- No-arg constructor — Creates an object with the colors of the four stars randomly selected.
- Single-arg constructor — Creates an object with the colors specified in the string passed.
- Four-arg constructor — Creates an object with the specified colors in the specified order.
toString()
— Returns the string representation of the object. This should render the stars in color and in bold. The end of the returned string should contain the "Reset" escape sequence.getSimilarity(Stars that)
— returns a string containing the appropriate number of colored bars based on the comparison between the calling object and the object passed to this method. The end of the returned string should contain the "Reset" escape sequence.
equals()
to compare two Star
objects,
will only return true if the two references point to exactly the same object. If the
two references point to different objects that contain the same sequence of colored
stars, equals()
will return false. You learn why in the next couple weeks.
StarBlind
The StarBlind
class represents the game. An object from this class must retain all of
the guesses the user makes in an ArrayList<Stars>
. This class has one constructor and
four public methods:
- No-arg constructor — Creates an object from the class that includes a
Stars
object that contains the star colors to be guessed by the user. guess(Stars guess)
— Processes the users guess.getNumberOfGuesses()
— Returns the number of guesses that have been made.hasWon()
— Returnstrue
if and only if the user has guess the star colors correctly.toString()
— Returns a string representation of the object. The representation must include each guess on a separate line. In addition to the guess, the appropriate number of colored bars should be included on the corresponding line.
Driver
The Driver
class contains the main program and three private class methods:
displayIntro()
— Displays the welcome message and rules of the game.getGuess(Scanner in)
— Asks the user for a guess and returns the guess as aStars
object. If the user does not enter a valid guess, the method repeatedly asks the user for a guess until a valid guess is entered.isValidEntry(String guess)
— Returnstrue
if and only if theguess
is four characters long and contains only the letters that represent the six colors used in the game.
For Motivated Students
Once you have completed all of the requirements for the assignment, motivated students may wish to add additional features. Some ideas:
- Allow the user to change the number of stars to be something other than four.
- Limit the total number of guesses allowed and end the game, declaring the user a loser if they do not correctly guess the star colors.
- Propose an alternative software design (class and/or method modifications). Be sure to commit your solution that includes the required design prior to updating your implementation with an alternative design.
Acknowledgement
This laboratory assignment was developed by Dr. Chris Taylor.