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, you should print out all the guesses they've made and then ask them if they want to play again or end the program. If the user chooses to play again, and new StarBlind game with begin with a different random chosen sequence of colors as the answer.
Software Design
You will make use of the Color
enumeration from homework 10 with the addition of three class methods. For
the methods that require a color code argument, 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".
Color getColor(char code)
— returns the color specified by the color code passed to the method.Color getColor()
— returns one of the following colors randomly and with equal probability: R, G, B, C, M, and Y.String colorString(char code, String text)
— returns a string that contains the appropriate ANSI escape codes to make thetext
bold and in the specified color code.
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.- Hint, all enums have the builtin name() method that will return the String of the enum name. For example Color.RED.name() returns the string "RED".
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
or Color
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 of weeks. For this assignment,
if you want to compare two Star or Color objects you can compare the Strings returned by their toString() methods,
since the String class does have an implemented equals()
method.
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 is how you will print out the list
of past guesses when the user wins. This class is required to have two public constructors and
four public methods:
- No-arg constructor — Creates an object that creates a
Stars
object with random colors and sets that as the target star colors to be guessed by the user. - Single-arg constructor — Creates an object using the passed in
Stars
object and sets that as the target star colors to be guessed by the user. guess(Stars guess)
— Processes the users guess by storing it and returns the similarity (i.e., colored bars) between the guess and the target star colors.getNumberOfGuesses()
— Returns the number of guesses that have been made.hasWon()
— Returnstrue
if and only if the user has guessed the target 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 similarity (i.e., colored bars) between the guess and the target star colors should be included.
Driver
The Driver
class contains the main program and three private class methods:
displayIntro()
— Displays the welcome message and rules of the game. This should match the sample output shown.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.
StarBlindTester
A StarBlindTester
class has been provided to help you test and debug your implementation. The
tester will use the second StarBlind tester to create a StarBlind game where the answer color
sequence is known. It then tests the guess() method with various guesses and prints out the
results. Use this tester to make sure your program is producing the correct number
of yellow and green bars.
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.