Package mastermind.core
Class Code
java.lang.Object
mastermind.core.Code
Represents a code sequence in the Mastermind game.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumRepresents a color in the Mastermind game. -
Constructor Summary
ConstructorsConstructorDescriptionCode(List<Code.Color> code) Initializes a newCodeobject with the specified code sequence. -
Method Summary
Modifier and TypeMethodDescriptiongetColor(int index) Retrieves theColorat the specified position in the code sequence.Retrieves the list ofColorobjects contained in this code sequence.Computes the occurrence count of eachCode.Colorin the code sequence.toString()
-
Constructor Details
-
Code
Initializes a newCodeobject with the specified code sequence.- Parameters:
code- a list ofColorobjects representing the code sequence.
-
-
Method Details
-
getColor
Retrieves theColorat the specified position in the code sequence.- Parameters:
index- the zero-based index of theColorto retrieve.- Returns:
- the
Colorat the specified index. - Throws:
IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size of the code).
-
getColors
Retrieves the list ofColorobjects contained in this code sequence.- Returns:
- an
ListofColorobjects representing the current code sequence.
-
getOccurrences
Computes the occurrence count of eachCode.Colorin the code sequence.This method iterates through the
Code.Colorenumeration and initializes aHashMapwith 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
HashMapwhere the keys areCode.Colorvalues and the values are integers representing the number of occurrences of each color in the code sequence.
-
toString
-