Class Code

java.lang.Object
mastermind.core.Code

public class Code extends Object
Represents a code sequence in the Mastermind game.
  • Constructor Details

    • Code

      public Code(List<Code.Color> code)
      Initializes a new Code object with the specified code sequence.
      Parameters:
      code - a list of Color objects representing the code sequence.
  • Method Details

    • getColor

      public Code.Color getColor(int index)
      Retrieves the Color at the specified position in the code sequence.
      Parameters:
      index - the zero-based index of the Color 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

      public List<Code.Color> getColors()
      Retrieves the list of Color objects contained in this code sequence.
      Returns:
      an List of Color objects representing the current code sequence.
    • getOccurrences

      public HashMap<Code.Color,Integer> getOccurrences()
      Computes the occurrence count of each Code.Color in the code sequence.

      This method iterates through the Code.Color enumeration and initializes a HashMap 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 the HashMap.

      Returns:
      a HashMap where the keys are Code.Color values and the values are integers representing the number of occurrences of each color in the code sequence.
    • toString

      public String toString()
      Overrides:
      toString in class Object