Le défi est proposé sur la plateforme Dev.to sur le lien suivant Greed.
""
Greed is a dice game played with five six-sided dice. Using an array containing five six-sided dice values, write a function that will score a throw according to the following rules:
Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 point
A single die can only be counted once in each roll. For example, a "5" can only count as part of a triplet (contributing to the 500 points) or alone (as 50 points), but not both in the same roll.
Example Scoring
5 1 3 4 1 => 50 + 2 * 100 = 250
1 1 1 3 1 => 1000 + 100 = 1100
2 4 4 5 4 => 400 + 50 = 450
""
Ma proposition (simplifiée) est la suivante :
Une version plus complète peut être trouvée ici.
""
Greed is a dice game played with five six-sided dice. Using an array containing five six-sided dice values, write a function that will score a throw according to the following rules:
Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 point
A single die can only be counted once in each roll. For example, a "5" can only count as part of a triplet (contributing to the 500 points) or alone (as 50 points), but not both in the same roll.
Example Scoring
5 1 3 4 1 => 50 + 2 * 100 = 250
1 1 1 3 1 => 1000 + 100 = 1100
2 4 4 5 4 => 400 + 50 = 450
""
Ma proposition (simplifiée) est la suivante :
public class Greed { public static int[] scores = new int[]{1000, 200, 300, 400, 500, 600}; public static int checkOccurrences(int[] dicesValues, int valueToCheck) { int numberOfOccurrences = 0; for (int i = 0; i < dicesValues.length; i++) { numberOfOccurrences += (dicesValues[i] == valueToCheck) ? 1 : 0; } return numberOfOccurrences; } public static void eraseThreeOccurences(int[] dicesValues, int valueToCheck) { int index = 0; int erased = 0; while (index < dicesValues.length && erased < 3) { if (dicesValues[index] == valueToCheck) { dicesValues[index] = 0; erased++; } index++; } } public static int playExample(int[] dicesValues) { int score = 0; for (int i = 0; i < scores.length; i++) { if (checkOccurrences(dicesValues, (i + 1)) >= 3) { score += scores[i]; eraseThreeOccurences(dicesValues, (i + 1)); } } score += 100 * checkOccurrences(dicesValues, 1); score += 50 * checkOccurrences(dicesValues, 5); return score; } public static void main(String[] args) { int[] example1 = new int[]{5, 1, 3, 4, 1}; int[] example2 = new int[]{1, 1, 1, 3, 1}; int[] example3 = new int[]{2, 4, 4, 5, 4}; System.out.println("Example 1 " + playExample(example1)); System.out.println("Example 2 " + playExample(example2)); System.out.println("Example 3 " + playExample(example3)); } }
Une version plus complète peut être trouvée ici.