在java中与yahtzee的麻烦

时间:2014-11-18 01:13:30

标签: java methods

我很难创造这个游戏。如果有人可以请帮助我创建或给我一个想法如何检查fullhouse,smallstraight,big straight,4 of kind& 3种。

        Scanner keyboard = new Scanner(System.in);
        rollDice();
        for (int i = 0; i < NUM_ROLLS_; i++) {
            if (gotYatzee()) {
                break;
            }
            System.out.println(diceToString());

            System.out.println("Do you want to roll the dice again? Yes or No");
            int a = 0;
            String yes = "Yes";

            String userInput;
            userInput = keyboard.next();
            if (userInput.equals(yes)) {
                System.out.println("ALRIGHT");
                secondReroll(convert(keyboard.nextLine()));
            }

        }
        System.out.println("Which dice do you want to roll again: ");
        System.out.println(diceToString());

        if (gotYatzee()) {
            System.out.println("You got Yatzee & 50 points!");

        } else {
            System.out.println("SORRY");
        }

    }

    public static void rollDice() {
        for (int i = 0; i < NUM_OF_DICE; i++) {
            dice[i] = randomValue();
        }
    }

    public static int randomValue() {
        return (int) (Math.random() * 6 + 1);
    }

    public static String diceToString() {
        String dado = "Here are your dice: ";
        for (int element : dice) {
            dado = dado + element + " ";
        }
        return dado;
    }

    public static boolean gotYatzee() {
        for (int element : dice) {
            if (element != dice[0]) {
                return false;
            }
        }
        return true;
    }

    public static void secondReroll(int[] newValue) {
        for (int element : newValue) {
            dice[element - 1] = randomValue();
        }
    }

    public static int[] convert(String s) {
        StringTokenizer st = new StringTokenizer(s);
        int[] a = new int[st.countTokens()];
        int i = 0;

        while (st.hasMoreTokens()) {
            a[i++] = Integer.parseInt(st.nextToken());
        }
        return a;
    }

    public static boolean Chance() {
        for (int element : dice) {
            int i = 0;
            if (element != dice[i]) {
                i++;
                return false;
            }
        }
        return true;
    }

    public static boolean smallStraight() {
        for (int i = 1; i <= NUM_OF_DICE; i++) {
            boolean b = false;
            for (int j = 0; j < NUM_OF_DICE; j++) {
            b = b || (dice[j] == i);
            }
            if (!b) {
                return false;
            }
        }
        return true;
    }

}

public static boolean LargeStraight (){

}

public static boolean fullHouse (){

}

public static boolean threeKind (){

}

public static boolean fourKind (){

}

}

这些最后的方法是我的问题。

1 个答案:

答案 0 :(得分:1)

也许这会对你有所帮助。我创建了一个Map<String, Integer> result

Map<String, String> result = new HashMap<String, Integer>();
result.put("one", 0);
result.put("two", 0);
result.put("three", 0);
result.put("four", 0);
result.put("five", 0);
result.put("six", 0);

使用switch case将每个骰子存储在正确的Map条目中:

case "5"
result.put("five", result.get("five")+1);

并且您可以使用此Map来确定您的种类。例如。

threeKind(){
    return (result.containsValue(3) ||
    result.containsValue(4) || 
    result.containsValue(5) ||
    result.containsValue(6));
}

您也可以将此Map用于其他方法。例如。满屋是containsValue(3) && containsValue(2)

我希望你明白了。