Avatar of Dominik93

Dominik93's solution

to Yacht in the Java Track

Published at Oct 15 2019 · 0 comments
Instructions
Test suite
Solution

Score a single throw of dice in Yacht

The dice game Yacht is from the same family as Poker Dice, Generala and particularly Yahtzee, of which it is a precursor. In the game, five dice are rolled and the result can be entered in any of twelve categories. The score of a throw of the dice depends on category chosen.

Scores in Yacht

Category Score Description Example
Ones 1 × number of ones Any combination 1 1 1 4 5 scores 3
Twos 2 × number of twos Any combination 2 2 3 4 5 scores 4
Threes 3 × number of threes Any combination 3 3 3 3 3 scores 15
Fours 4 × number of fours Any combination 1 2 3 3 5 scores 0
Fives 5 × number of fives Any combination 5 1 5 2 5 scores 15
Sixes 6 × number of sixes Any combination 2 3 4 5 6 scores 6
Full House Total of the dice Three of one number and two of another 3 3 3 5 5 scores 19
Four of a Kind Total of the four dice At least four dice showing the same face 4 4 4 4 6 scores 16
Little Straight 30 points 1-2-3-4-5 1 2 3 4 5 scores 30
Big Straight 30 points 2-3-4-5-6 2 3 4 5 6 scores 30
Choice Sum of the dice Any combination 2 3 3 4 6 scores 18
Yacht 50 points All five dice showing the same face 4 4 4 4 4 scores 50

If the dice do not satisfy the requirements of a category, the score is zero. If, for example, Four Of A Kind is entered in the Yacht category, zero points are scored. A Yacht scores zero if entered in the Full House category.

Task

Given a list of values for five dice and a category, your solution should return the score of the dice for that category. If the dice do not satisfy the requirements of the category your solution should return 0. You can assume that five values will always be presented, and the value of each will be between one and six inclusively. You should not assume that the dice are ordered.

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Source

James Kilfiger, using wikipedia https://en.wikipedia.org/wiki/Yacht_(dice_game)

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

YachtTest.java

import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class YachtTest {

    @Test
    public void yacht() {
        Yacht yacht = new Yacht(new int[]{ 5, 5, 5, 5, 5 }, YachtCategory.YACHT);
        assertEquals(50, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void notYacht() {
        Yacht yacht = new Yacht(new int[]{ 1, 3, 3, 2, 5 }, YachtCategory.YACHT);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void ones() {
        Yacht yacht =  new Yacht(new int[]{ 1, 1, 1, 3, 5 }, YachtCategory.ONES);
        assertEquals(3, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void onesOutOfOrder() {
        Yacht yacht = new Yacht(new int[]{ 3, 1, 1, 5, 1 }, YachtCategory.ONES);
        assertEquals(3, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void noOnes() {
        Yacht yacht = new Yacht(new int[]{ 4, 3, 6, 5, 5 }, YachtCategory.ONES);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void twos() {
        Yacht yacht = new Yacht(new int[]{ 2, 3, 4, 5, 6 }, YachtCategory.TWOS);
        assertEquals(2, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void fours() {
        Yacht yacht = new Yacht(new int[]{ 1, 4, 1, 4, 1 }, YachtCategory.FOURS);
        assertEquals(8, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void yachtCountedAsThrees() {
        Yacht yacht = new Yacht(new int[]{ 3, 3, 3, 3, 3 }, YachtCategory.THREES);
        assertEquals(15, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void yachtOfThreesCountedAsFives() {
        Yacht yacht = new Yacht(new int[]{ 3, 3, 3, 3, 3 }, YachtCategory.FIVES);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void sixes() {
        Yacht yacht = new Yacht(new int[]{ 2, 3, 4, 5, 6 }, YachtCategory.SIXES);
        assertEquals(6, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void fullHouseTwoSmallThreeBig() {
        Yacht yacht = new Yacht(new int[]{ 2, 2, 4, 4, 4 }, YachtCategory.FULL_HOUSE);
        assertEquals(16, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void fullHouseThreeSmallTwoBig() {
        Yacht yacht = new Yacht(new int[]{ 5, 3, 3, 5, 3 }, YachtCategory.FULL_HOUSE);
        assertEquals(19, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void twoPairIsNotAFullHouse() {
        Yacht yacht = new Yacht(new int[]{ 2, 2, 4, 4, 5 }, YachtCategory.FULL_HOUSE);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void fourOfAKindIsNotAFullHouse() {
        Yacht yacht = new Yacht(new int[]{ 1, 4, 4, 4, 4 }, YachtCategory.FULL_HOUSE);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void yachtIsNotAFullHouse() {
        Yacht yacht = new Yacht(new int[]{ 2, 2, 2, 2, 2 }, YachtCategory.FULL_HOUSE);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void fourOfAKind() {
        Yacht yacht = new Yacht(new int[]{ 6, 6, 4, 6, 6 }, YachtCategory.FOUR_OF_A_KIND);
        assertEquals(24, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void yachtCanBeScoredAsFourOfAKind() {
        Yacht yacht = new Yacht(new int[]{ 3, 3, 3, 3, 3 }, YachtCategory.FOUR_OF_A_KIND);
        assertEquals(12, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void fullHouseIsNotFourOfAKind() {
        Yacht yacht = new Yacht(new int[]{ 3, 3, 3, 5, 5 }, YachtCategory.FOUR_OF_A_KIND);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void littleStraight() {
        Yacht yacht = new Yacht(new int[]{ 3, 5, 4, 1, 2 }, YachtCategory.LITTLE_STRAIGHT);
        assertEquals(30, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void littleStraightAsBigStraight() {
        Yacht yacht = new Yacht(new int[]{ 1, 2, 3, 4, 5 }, YachtCategory.BIG_STRAIGHT);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void fourInOrderButNotALittleStraight() {
        Yacht yacht = new Yacht(new int[]{ 1, 1, 2, 3, 4 }, YachtCategory.LITTLE_STRAIGHT);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void noPairsButNotALittleStraight() {
        Yacht yacht = new Yacht(new int[]{ 1, 2, 3, 4, 6 }, YachtCategory.LITTLE_STRAIGHT);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void minimumIs1MaximumIs5ButNotALittleStraight() {
        Yacht yacht = new Yacht(new int[]{ 1, 1, 3, 4, 5 }, YachtCategory.LITTLE_STRAIGHT);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void bigStraight() {
        Yacht yacht = new Yacht(new int[]{ 4, 6, 2, 5, 3 }, YachtCategory.BIG_STRAIGHT);
        assertEquals(30, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void bigStraightAsLittleStraight() {
        Yacht yacht = new Yacht(new int[]{ 6, 5, 4, 3, 2 }, YachtCategory.LITTLE_STRAIGHT);
        assertEquals(0, yacht.score());
    }
    
    @Ignore("Remove to run test")
    @Test
    public void noPairsButNotABigStraight() {
        Yacht yacht = new Yacht(new int[]{ 6, 5, 4, 3, 1 }, YachtCategory.BIG_STRAIGHT);
        assertEquals(0, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void choice() {
        Yacht yacht = new Yacht(new int[]{ 3, 3, 5, 6, 6 }, YachtCategory.CHOICE);
        assertEquals(23, yacht.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void yachtAsChoice() {
        Yacht yacht = new Yacht(new int[]{ 2, 2, 2, 2, 2 }, YachtCategory.CHOICE);
        assertEquals(10, yacht.score());
    }

}

src/main/java/Yacht.java

import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.IntPredicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

class Yacht {

    private Map<YachtCategory, Supplier<Integer>> scoreCalculator
            = new EnumMap<YachtCategory, Supplier<Integer>>(YachtCategory.class) {
        {
            put(YachtCategory.LITTLE_STRAIGHT, () -> new LittleStraightCalculator().calculate(dice));
            put(YachtCategory.BIG_STRAIGHT, () -> new BigStraightCalculator().calculate(dice));
            put(YachtCategory.FULL_HOUSE, () -> new FullHouseCalculator().calculate(dice));
            put(YachtCategory.ONES, () -> new NumberCalculator(1).calculate(dice));
            put(YachtCategory.TWOS, () -> new NumberCalculator(2).calculate(dice));
            put(YachtCategory.THREES, () -> new NumberCalculator(3).calculate(dice));
            put(YachtCategory.FOURS, () -> new NumberCalculator(4).calculate(dice));
            put(YachtCategory.FIVES, () -> new NumberCalculator(5).calculate(dice));
            put(YachtCategory.SIXES, () -> new NumberCalculator(6).calculate(dice));
            put(YachtCategory.YACHT, () -> new YachtCalculator().calculate(dice));
            put(YachtCategory.CHOICE, () -> new SumCalculator().calculate(dice));
            put(YachtCategory.FOUR_OF_A_KIND, () -> new FourOfAKindCalculator().calculate(dice));
        }
    };

    private int[] dice;

    private YachtCategory yachtCategory;

    Yacht(int[] dice, YachtCategory yachtCategory) {
        this.dice = dice;
        this.yachtCategory = yachtCategory;
    }

    int score() {
        return scoreCalculator.get(yachtCategory).get();
    }

}

class SumCalculator {

    public Integer calculate(int[] dice) {
        return Arrays.stream(dice).sum();
    }
}

class LittleStraightCalculator {

    private List<Integer> littleStraight = Arrays.asList(1, 2, 3, 4, 5);

    private StraightChecker straightChecker = new StraightChecker();

    public Integer calculate(int[] dice) {
        if (straightChecker.isStraight(littleStraight, dice)) {
            return 30;
        }
        return 0;
    }

}


class BigStraightCalculator {

    private List<Integer> bigStraight = Arrays.asList(2, 3, 4, 5, 6);

    private StraightChecker straightChecker = new StraightChecker();

    public Integer calculate(int[] dice) {
        if (straightChecker.isStraight(bigStraight, dice)) {
            return 30;
        }
        return 0;
    }
}

class StraightChecker {

    public boolean isStraight(List<Integer> straight, int[] dice) {
        List<Integer> dices = Arrays.stream(dice).boxed().collect(Collectors.toList());
        return dices.containsAll(straight);
    }
}

class FullHouseCalculator {

    public Integer calculate(int[] dice) {
        Map<Integer, List<Integer>> diceGroupBy = Arrays.stream(dice).boxed().collect(Collectors.groupingBy(Function.identity()));
        int three = sumDiceWithOccurence(diceGroupBy, 3);
        int two = sumDiceWithOccurence(diceGroupBy, 2);
        return isFullHouse(three, two) ? three + two : 0;
    }

    private boolean isFullHouse(int three, int two) {
        return three != 0 && two != 0;
    }

    private int sumDiceWithOccurence(Map<Integer, List<Integer>> diceGroupBy, int i) {
        for (Map.Entry<Integer, List<Integer>> integerListEntry : diceGroupBy.entrySet()) {
            if (integerListEntry.getValue().size() == i) {
                return integerListEntry.getValue().stream().mapToInt(Integer::intValue).sum();
            }
        }
        return 0;
    }
}

class NumberCalculator {

    private int number;

    public NumberCalculator(int number) {
        this.number = number;
    }

    public Integer calculate(int[] dice) {
        return Arrays.stream(dice).filter(isNumber(number)).sum();
    }

    private IntPredicate isNumber(int number) {
        return value -> value == number;
    }

}

class FourOfAKindCalculator {

    public Integer calculate(int[] dice) {
        List<Integer> diceList = Arrays.stream(dice).boxed().collect(Collectors.toList());
        return diceList.stream()
                .filter(value -> Collections.frequency(diceList, value) >= 4)
                .findFirst()
                .map(integer -> integer * 4)
                .orElse(0);
    }
}

class YachtCalculator {

    public Integer calculate(int[] dice) {
        List<Integer> diceList = Arrays.stream(dice).boxed().collect(Collectors.toList());
        return diceList.stream()
                .filter(value -> Collections.frequency(diceList, value) == 5)
                .findFirst()
                .map(integer -> 50)
                .orElse(0);
    }

}

src/main/java/YachtCategory.java

enum YachtCategory {

    YACHT,
    ONES,
    TWOS,
    THREES,
    FOURS,
    FIVES,
    SIXES,
    FULL_HOUSE,
    FOUR_OF_A_KIND,
    LITTLE_STRAIGHT,
    BIG_STRAIGHT,
    CHOICE

}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?