146,212. Die-game Yahtzee

This task is about the scoring in the first phase of the die-game Yahtzee, where five dice are used. The score is determined by the values on the upward die faces after a roll. The player gets to choose a value, and all dice that show the chosen value are considered active. The score is simply the sum of values on active dice.

Say, for instance, that a player ends up with the die faces showing 2, 2, 3, 5 and 4. Choosing the value two makes the dice showing 2 active and yields a score of 2 + 2 = 4, while choosing 5 makes the one die showing 5 active, yielding a score of 5.

Your method will take as input an int[] toss, where each element represents the upward face of a die, and return the maximum possible score with these values.

import java.util.Arrays;
public class Yahtzee {
    private void maxPoints(int[] toss, int res) {
        //Arrays.sort(toss);
        int sum=0;
        int t[]= new int[7];
        t[0] =0;
        for (int i=0;i<toss.length;i++){
            t[toss[i]] = t[toss[i]]+toss[i];
        }
        Arrays.sort(t);
        sum = t[t.length-1];
        System.out.println(sum);
    }
    
    public static void main(String[] args) {
        Yahtzee y = new Yahtzee();
        y.maxPoints(new int[]    {2, 2, 3, 5, 4}     ,    5    );
        y.maxPoints(new int[]    {6, 4, 1, 1, 3}     ,    6    );
        y.maxPoints(new int[]    {5, 3, 5, 3, 3}     ,    10    );
        y.maxPoints(new int[]    {2, 3, 2, 2, 3}     ,    6    );
        y.maxPoints(new int[]    {1, 1, 1, 1, 1}     ,    5    );
        y.maxPoints(new int[]    {3, 4, 4, 3, 6}     ,    8    );
        y.maxPoints(new int[]    {6, 4, 3, 6, 1}     ,    12    );
        y.maxPoints(new int[]    {1, 5, 1, 3, 2}     ,    5    );
        y.maxPoints(new int[]    {4, 6, 4, 6, 1}     ,    12    );
        y.maxPoints(new int[]    {6, 1, 1, 6, 3}     ,    12    );
}
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: