Kniffel/fassade/GameCycle.java

29 lines
1.5 KiB
Java
Raw Normal View History

package fassade;
public class GameCycle {
// public class DiceRoll {
// public static boolean hasSmallStraight(int[] dice) {
// Arrays.sort(dice);
// for (int i = 0; i < dice.length - 3; i++) {
// if (dice[i] + 1 == dice[i + 1] && dice[i] + 2 == dice[i + 2] && dice[i] + 3 == dice[i + 3]) {
// return true;
// }
// }
// return false;
// }
//
// public static boolean hasLargeStraight(int[] dice) {
// Arrays.sort(dice);
// for (int i = 0; i < dice.length - 4; i++) {
// if (dice[i] + 1 == dice[i + 1] && dice[i] + 2 == dice[i + 2] && dice[i] + 3 == dice[i + 3] && dice[i] + 4 == dice[i + 4]) {
// return true;
// }
// }
// return false;
// }
//
// public static void main(String[] args) {
// int[] dice = {3, 1, 4, 2, 5};}
// } // example array System.out.println("Has small straight: " + hasSmallStraight(dice)); System.out.println("Has large straight: " + hasLargeStraight(dice)); }}```Here's how the code works:1. First, we sort the array of dice rolls in ascending order using `Arrays.sort()`.2. In the `hasSmallStraight()` method, we iterate through the sorted array and check if four consecutive numbers appear. We do this by checking if the current element plus 1, 2, and 3 are equal to the next three elements in the array. If we find a match, we return `true`.3. In the `hasLargeStraight()` method, we do a similar check,
}