Package mastermind.core
Class Code
java.lang.Object
mastermind.core.Code
Represents a code sequence in the Mastermind game.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Represents a color in the Mastermind game. -
Constructor Summary
ConstructorDescriptionCode
(List<Code.Color> code) Initializes a newCode
object with the specified code sequence. -
Method Summary
Modifier and TypeMethodDescriptiongetColor
(int index) Retrieves theColor
at the specified position in the code sequence.Retrieves the list ofColor
objects contained in this code sequence.Computes the occurrence count of eachCode.Color
in the code sequence.toString()
-
Constructor Details
-
Code
Initializes a newCode
object with the specified code sequence.- Parameters:
code
- a list ofColor
objects representing the code sequence.
-
-
Method Details
-
getColor
Retrieves theColor
at the specified position in the code sequence.- Parameters:
index
- the zero-based index of theColor
to retrieve.- Returns:
- the
Color
at the specified index. - Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size of the code).
-
getColors
Retrieves the list ofColor
objects contained in this code sequence.- Returns:
- an
List
ofColor
objects representing the current code sequence.
-
getOccurrences
Computes the occurrence count of eachCode.Color
in the code sequence.This method iterates through the
Code.Color
enumeration and initializes aHashMap
with each color as a key and its initial count set to zero. It then iterates through the code sequence, updating the count for each color encountered in theHashMap
.- Returns:
- a
HashMap
where the keys areCode.Color
values and the values are integers representing the number of occurrences of each color in the code sequence.
-
toString
-