Answered step by step
Verified Expert Solution
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 textbased 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 x grid format similar to the board game.
eg suppose the code is black red blue green
Guess #:
blue red green yellow
x o
o
The feedback shows that there is one exact match and 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 guesses are
made but did not result in a full match system wins
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 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
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.
Create a class named Peg with field colour. Include any other fieldsmethods to help with
gameplay. Override the equals method to return true if the colours match.
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.
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 addremove 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 pegs of which colours are randomly generated.
Each peg has a colour of 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 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 th 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 ie if it is an invalid colour, the player
loses and the game is over.
Enums are optional eg Colour
Suggestions:
Display the generated code eg 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started