Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a simple text - based version of the classic board game Mastermind, where a single player is the code breaker, and the system is

Write a simple text-based version of the classic board game Mastermind, where a single player is the code
breaker, and the system is the code maker. The system selects a code of four coloured pegs and the player
tries to guess the secret code.
In each round, the player makes a guess, and the system tells the player how many pegs of the guess were
exact matches to the code (correct in both color and position, marked 'x'), and how many colours were
partial matches to the code (correct color placed in the wrong position, marked 'o'). The feedback is
displayed in a 2 x 2 grid format similar to the board game.
e.g., suppose the code is black red blue green
Guess #1:
blue red green yellow
x o
o -
The feedback shows that there is one exact match and 2 partial matches. Note that this configuration
does not indicate which pegs are exact matches.
The player makes guesses until either: a) the player breaks the code (player wins!) or b)11 guesses are
made but did not result in a full match (system wins).
1. Create the generic ArrayList class that implements the provided List interface (note that List
extends Iterable)
a. Overload the add method: include another add method that will have one parameter, an
element that adds to the end of the list.
b. Make your ArrayList dynamic: the array should grow to double its current capacity if it runs
out of space and shrink to half its current capacity when the number of elements in the arraylist
goes below N/4, where N is the current capacity of the array. Modify add() and remove()
methods and include a resize() method to support the dynamic structure. Set the default
capacity to 4.
c. Override the equals method of the Object class so that it checks if the ArrayList is
equivalent to the given instance. Consider an appropriate definition of equivalence.
2
2. Create a class named Peg with field colour. Include any other fields/methods to help with
gameplay. Override the equals method to return true if the colours match.
3. Write a Game class that acts as the code maker and handles the mechanics of the Mastermind game.
Include a minimal main method that instantiates the game and invokes the method to play the game.
4. In the main method, also illustrate how the capacity of your array would changes as objects are added
and removed. That is, include lines of code that to add/remove and show that it shrinks or enlarges as
elements are removed or added, respectively.
Your program should have the following:
a. An instance of ArrayList that holds a set of 4 pegs of which colours are randomly generated.
Each peg has a colour of 6 different possibilities (duplicates are allowed, blanks are not).
b. Another ArrayList that holds pegs that represent the players guess.
c. A game loop that prompts the user for their guess and determines if the 2 ArrayLists are equal:
i. if so, notify the player and end the game
ii. if not, provide the user feedback on their guess:
Determine whether if each peg of the guess is a match and mark it accordingly.
You will need to compare the guess against the code and determine the number
of exact and partial matches.
d. After their 11th guess, if it is not a full match, inform the player that the system won.
Note:
You may assume that the player knows the valid colours i.e., if it is an invalid colour, the player
loses and the game is over.
Enums are optional (e.g., Colour)
Suggestions:
Display the generated code (e.g., first line of the sample output below) for testing and remove
before submitting
For guess feedback: must be careful to avoid counting any of the pegs twice; make at least two
passes to compare the guess and the code. In the first pass, look for exact matches and in the
second pass, look for partial matches.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Rules In Database Systems Third International Workshop Rids 97 Sk Vde Sweden June 26 28 1997 Proceedings Lncs 1312

Authors: Andreas Geppert ,Mikael Berndtsson

1997th Edition

3540635165, 978-3540635161

More Books

Students also viewed these Databases questions