From 3467dd2f04750abe6d92b6200746e546419c079f Mon Sep 17 00:00:00 2001 From: Daniel Grigencha Date: Sun, 1 Dec 2024 19:37:40 +0100 Subject: [PATCH] added a new method getColorByIndex(int) and next() method to the enum 'Color' --- .../src/main/java/pp/mdga/game/Color.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Color.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Color.java index 218416a6..828450f4 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Color.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Color.java @@ -26,16 +26,33 @@ public enum Color { */ NONE; + /** + * This method will be used to return a Color enumeration depending on the given index parameter. + * + * @param index as the index of the color inside the values as an Integer. + * @return a Color enumeration. + */ + public static Color getColorByIndex(int index) { + if (index < 0 || index >= values().length) { + throw new IllegalArgumentException(""); + } + + return values()[index]; + } + /** * This method will be used to calculate the next color inside the sequence. * * @return color as a Color Enumeration. */ public Color next() { - if (this.next() == NONE) { - return AIRFORCE; + Color[] colors = values(); + int nextIndex = (this.ordinal() + 1) % colors.length; + + if (colors[nextIndex] == NONE) { + nextIndex = (nextIndex + 1) % colors.length; } - return values()[(ordinal() + 1) % values().length]; + return colors[nextIndex]; } }