Three of a kind Get three dice with the same number Points a

Three of a kind: Get three dice with the same number. Points are the sum all dice (not just the three of a kind).

Four of a kind: Get four dice with the same number. Points are the sum all dice (not just the four of a kind).

Full house: Get three of a kind and a pair, e.g. 1,1,3,3,3 or 3,3,3,6,6. Scores 25 points.

Small straight: Get four sequential dice, 1,2,3,4 or 2,3,4,5 or 3,4,5,6. Scores 30 points.

Large straight: Get five sequential dice, 1,2,3,4,5 or 2,3,4,5,6. Scores 40 points.

Chance: You can put anything into chance, it\'s basically like a garbage can when you don\'t have anything else you can use the dice for. The score is simply the sum of the dice.

YAHTZEE: Five of a kind. Scores 50 points. You can optionally get multiple Yahtzees, see below for details.

Make a java program that plays YAHTZEE.

Solution


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Yahtzee {
    int dice[] = new int[5];
  
    int getScore(){ //used to calculate and return score
        int count[] = {0,0,0,0,0,0};
        for (int i=0; i<5; i++)
            count[dice[i]]++;
        for (int i = 0; i<6; i++) //YAHTZEE
            if(count[i]==5)
                return 50;
        int maxCount=0, tempCount=0;
        for (int i = 0; i<6; i++)
            if(count[i]>0)
                tempCount++;
            else {
                if (tempCount>maxCount)
                    maxCount=tempCount;
                tempCount = 0;
            }
        if (maxCount==5 || tempCount==5) //Large straight
            return 40;
        if (maxCount==4 || tempCount==4) //Small straight
            return 0;
        boolean count2 = false;
        boolean count3 = false;
        boolean count4 = false;
        for (int i = 0; i<6; i++)
            if(count[i]==2)
                count2 = true;
            else if(count[i]==3)
                count3 = true;
            else if (count[i]==4)
                count4 = true;
        if (count2 && count3) //House full
            return 25;
      
        if (count4) // Four of a kind
            return sum();
      
        if (count3) //three of a kind
            return sum();
      
        return sum(); //chance
    }
  
    void rollDice(){ //Roll dice to get new set of numbers
        dice[0] = (int) (Math.round(Math.random()*5)+1);
        dice[1] = (int) (Math.round(Math.random()*5)+1);
        dice[2] = (int) (Math.round(Math.random()*5)+1);
        dice[3] = (int) (Math.round(Math.random()*5)+1);
        dice[4] = (int) (Math.round(Math.random()*5)+1);
        dice[5] = (int) (Math.round(Math.random()*5)+1);      
    }

    private int sum() { //get sum of all dice
        int sum = 0;
        for (int i = 0; i<5; i++)
            sum += dice[i];
        return sum;
    }
  
    public static void main(String args[])throws IOException {
        Yahtzee y = new Yahtzee();
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        int score;
        do{
            System.out.println(\"Rolling dice...\");
            y.rollDice();
            score = y.getScore();
            System.out.println(\"Score= \"+score);
            System.out.println(\"Do you want to roll dice and get score again?\");
        } while (!br.readLine().equalsIgnoreCase(\"no\"));
    }
}

Three of a kind: Get three dice with the same number. Points are the sum all dice (not just the three of a kind). Four of a kind: Get four dice with the same nu
Three of a kind: Get three dice with the same number. Points are the sum all dice (not just the three of a kind). Four of a kind: Get four dice with the same nu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site