Avatar of Princic-1837592

Princic-1837592's solution

to Yacht in the Java Track

Published at Oct 14 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());
    }

}
import java.util.Arrays;

class Yacht
{
	private int score;
	public Yacht(int[] dice,YachtCategory yachtCategory)
	{
		score=computeScore(dice,yachtCategory);
	}
	public int score()
	{
		return score;
	}
	private int computeScore(int[] dice,YachtCategory yachtCategory)
	{
		int result=0,i;
		Arrays.sort(dice);
		switch(yachtCategory)
		{
		case YACHT:
			return dice[0]!=dice[4]?0:50;
		case ONES:
			return count(1,dice);
		case TWOS:
			return count(2,dice);
		case THREES:
			return count(3,dice);
		case FOURS:
			return count(4,dice);
		case FIVES:
			return count(5,dice);
		case SIXES:
			return count(6,dice);
		case FULL_HOUSE:
			if(dice[0]==dice[1]&&dice[3]==dice[4]&&(dice[2]==dice[0]||dice[2]==dice[3])&&dice[0]!=dice[4])
			{
				for(i=0;i<5;i++)
					result+=dice[i];
			}
			return result;
		case FOUR_OF_A_KIND:
			int start=0,stop=4;
			if(dice[0]!=dice[1])
			{
				start=1;
				stop=5;
			}
			result=dice[start++];
			for(i=start;i<stop;i++)
			{
				if(dice[i]!=dice[i-1]) return 0;
				result+=dice[i];
			}
			return result;
		case LITTLE_STRAIGHT:
			for(i=1;i<5;i++)
				if(dice[i]!=dice[i-1]+1) return 0;
			return dice[0]==1?30:0;
		case BIG_STRAIGHT:
			for(i=1;i<5;i++)
				if(dice[i]!=dice[i-1]+1) return 0;
			return dice[0]==2?30:0;
		case CHOICE:
			for(i=0;i<5;i++)
				result+=dice[i];
			return result;
		}
		return result;
	}
	private int count(int n,int[] dice)
	{
		int result=0;
		for(int i=0;i<5;i++)
			if(dice[i]==n) result+=n;
		return result;
	}
}

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?