Programming Assignment 03
Bulls and Cows
Due: Friday, September 21st [8:00 AM for code and 9:00
AM for paperwork]
[Please note, we will likely do in-person
grading for this PA during the week of the 24th-25th]
The Task - Implement a simple version of Bulls and Cows.
For this assignment you will build a program that allows the user to play an
old game called "Bulls and Cows."
The Original Rules
- The game is played between two players - the code-maker and the
code-breaker.
- The code-maker begins the game by selecting a "secret code" consisting of
4 digits where each digit could be a number 0-9.
- For example, the secret code might be 1724.
- The code-maker may repeat "elements" in the sequence.
- For example, 0220 is a legal code.
- It is the goal of the code-breaker to guess the code-maker's secret code.
- To do this, the code-breaker makes a series of guesses. After each
guess, the code-maker gives the code-breaker feedback by indicating how many
"matches" there were. There are two types of matches "right
element in
the right position" - called "Bulls" - and "right element in the wrong position"
- called "Cows."
- For example, if the secret code is "1724" and the code-breaker's guess is
"1234" then the 1 and the 4 are both "right element in the right position"
while the 2 is a "right element in the wrong position" The code-maker
would respond "2 Bulls and 1 Cow"
- Each element can only be "matched" once, and if an element could match two
different ways, the "higher" match always takes precedent.
- For example, if the secret code was "1122" and the guess was "1234" the
feedback would be "One Bull, One Cow." This is because the 1 in the
guess could match with either the first 1 or the second 1 in the secret code.
Since it can only match once, the higher ("Bull") match takes priority.
The 2 in the second position does match two different "cow" matches in the
secret code, but since there is only one 2 in the guess, we only report one
cow.
- Play continues until the code-breaker guesses the code-maker's code (a
feedback of "Four Bulls" ).
If you have questions about these rules, PLEASE talk to me about them before
you get too far into the assignment.
Your Assignment
Your goal this week is to come up with a text-based version of Bulls and Cows
that follows the following parameters.
- The code-maker is the computer while the code-breaker is the human player.
- The code-maker comes up with a length 4 code.
- Rather than use the digits 0-9, I would like you to use the set of six characters { A, B, C, D, E, F }
- After each guess, the code-maker checks to see if the guess is the secret
code.
- It it is, then the game prints a congratulations method.
- If it is not, then the the game provides a Bulls/Cows evaluation as
outlined above.
- After each game is completed, the computer asks if the human would like to
play again. If the answer is yes, a new game starts. If the answer
is no, then a goodbye message is printed.
- At any time, the code-breaker can enter "quit" or "QUIT" This
should cause the code-maker to print a goodbye message which includes the
answer to the secret code.
- If at any time the code-breaker enters something OTHER than the quit
command or a valid code, the code-maker should remind the code-breaker about
valid options. For example, a guess of "ABCX" or "abcd" should generate
a message reminding the player that only ABCDEF are valid characters.
Make sure that your program provides this basic functionality.
Furthermore, you should design and implement your game in an object-oriented fashion.
It IS completely possible to create this game so that it exists in only class.
However, that is problematic for several reasons. First, the code simply
is tough to write this way. Second, as we discussed in
session 7 and
session 8, that probably makes the code
very difficult to modify. You should keep in mind that other variations of the game are possible
and, whenever possible, you should design your program so that changes to the
game would require few and localized changes. For example, while you are
not required to implement the code for this, keep in mind what it would require
to modify your game so that
- the code-sequence is longer or shorter
- the "element set changes" (for example, a kids version might only use {A,B,C,D}.
An expert version might use the whole alphabet. OR, we could switch from
"characters" to "numbers".
- the roles of code-maker and code-breaker could be changed
- the code-maker is the human and the code-breaker is the computer
- the human and the computer alternate roles
- the code-maker and the code-breaker are both humans
Again, you are not required to implement a program that provides these
features, but you should should design your program with future extensions in
mind.
My SUGGESTION (not a requirement) is that you follow the
Model-View-Controller pattern that we discussed in
session10.
Deliverables
By the due date and time, submit the files:
- a cover report. (electronically, call this "readme.txt) This
should document your design and describe each of the classes in your solution
in some meaningful manner. This report should also describe how to use
the program both from the player's point of view.
- BullsAndCows.java, the "driver" class (the class that
contains main() ).
- any other files you create, in the order listed on your cover sheet
via the electronic submission system. You will also need to prepare
your documentation packet following the
homework collection
policies.