Skip to content

Commit e83adf3

Browse files
lemoncurryFridaTveit
authored andcommitted
Add exercise yacht (exercism#1588)
* Empty commit to indicate start of work on exercise yacht * Add exercise yacht Co-Authored-By: Jonas Weber <thriqon@users.noreply.github.com> * Change topics and unlocked_by of yacht * Change difficulty level from 5 to 3 of yacht * Add requested changes * Add requested changes * Update exercises/yacht/src/test/java/YachtTest.java
1 parent d5c9f6d commit e83adf3

10 files changed

Lines changed: 401 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@
205205
"strings"
206206
]
207207
},
208+
{
209+
"slug": "yacht",
210+
"uuid": "0cb45688-9598-49aa-accc-ed48c5d6962d",
211+
"core": false,
212+
"unlocked_by": "scrabble-score",
213+
"difficulty": 4,
214+
"topics": [
215+
"enumerations",
216+
"filtering",
217+
"games",
218+
"pattern_matching",
219+
"sorting"
220+
]
221+
},
208222
{
209223
"slug": "proverb",
210224
"uuid": "9906491b-a638-408d-86a4-4ad320a92658",

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,5 @@ include 'variable-length-quantity'
101101
include 'word-count'
102102
include 'word-search'
103103
include 'wordy'
104+
include 'yacht'
104105
include 'zipper'
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.util.Arrays;
2+
import java.util.Collection;
3+
import java.util.Map;
4+
import java.util.function.Function;
5+
import java.util.stream.Collectors;
6+
7+
class Yacht {
8+
private int[] dice;
9+
private YachtCategory yachtCategory;
10+
11+
Yacht(int[] dice, YachtCategory yachtCategory) {
12+
this.dice = dice;
13+
this.yachtCategory = yachtCategory;
14+
}
15+
16+
int score() {
17+
switch(this.yachtCategory) {
18+
case ONES:
19+
return Arrays.stream(dice)
20+
.filter(n -> n == 1)
21+
.sum();
22+
case TWOS:
23+
return Arrays.stream(dice)
24+
.filter(n -> n == 2)
25+
.sum();
26+
case THREES:
27+
return Arrays.stream(dice)
28+
.filter(n -> n == 3)
29+
.sum();
30+
case FOURS:
31+
return Arrays.stream(dice)
32+
.filter(n -> n == 4)
33+
.sum();
34+
case FIVES:
35+
return Arrays.stream(dice)
36+
.filter(n -> n == 5)
37+
.sum();
38+
case SIXES:
39+
return Arrays.stream(dice)
40+
.filter(n -> n == 6)
41+
.sum();
42+
case FULL_HOUSE:
43+
Collection<Long> occurrences = Arrays.stream(dice)
44+
.boxed()
45+
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
46+
.values();
47+
return occurrences.contains(2L) && occurrences.contains(3L) ? Arrays.stream(dice).sum() : 0;
48+
case FOUR_OF_A_KIND:
49+
return Arrays.stream(dice)
50+
.boxed()
51+
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
52+
.entrySet()
53+
.stream()
54+
.filter(x -> x.getValue() >= 4)
55+
.map(Map.Entry::getKey)
56+
.findFirst()
57+
.orElse(0) * 4;
58+
case LITTLE_STRAIGHT:
59+
Arrays.sort(dice);
60+
return Arrays.equals(dice, new int[]{ 1, 2, 3, 4, 5 }) ? 30 : 0;
61+
case BIG_STRAIGHT:
62+
Arrays.sort(dice);
63+
return Arrays.equals(dice, new int[]{ 2, 3, 4, 5, 6 }) ? 30 : 0;
64+
case CHOICE:
65+
return Arrays.stream(dice).sum();
66+
case YACHT:
67+
return (Arrays.stream(dice).distinct().count() == 1) ? 50 : 0;
68+
69+
default: return 0;
70+
}
71+
}
72+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
enum YachtCategory {
2+
3+
YACHT,
4+
ONES,
5+
TWOS,
6+
THREES,
7+
FOURS,
8+
FIVES,
9+
SIXES,
10+
FULL_HOUSE,
11+
FOUR_OF_A_KIND,
12+
LITTLE_STRAIGHT,
13+
BIG_STRAIGHT,
14+
CHOICE
15+
16+
}

‎exercises/yacht/.meta/version‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.1.0

‎exercises/yacht/README.md‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Yacht
2+
3+
# Score a single throw of dice in *Yacht*
4+
5+
The dice game [Yacht](https://en.wikipedia.org/wiki/Yacht_(dice_game)) is from
6+
the same family as Poker Dice, Generala and particularly Yahtzee, of which it
7+
is a precursor. In the game, five dice are rolled and the result can be entered
8+
in any of twelve categories. The score of a throw of the dice depends on the
9+
category chosen.
10+
11+
## Scores in Yacht
12+
13+
YachtCategory Score Example
14+
Ones 1 × number of ones 1 1 1 4 5 scores 3
15+
Twos 2 × number of twos 2 2 3 4 5 scores 4
16+
Threes 3 × number of threes 3 3 3 3 3 scores 15
17+
Fours 4 × number of fours 1 2 3 3 5 scores 0
18+
Fives 5 × number of fives 5 1 5 2 5 scores 15
19+
Sixes 6 × number of sixes 2 3 4 5 6 scores 6
20+
Full House Total of the dice 3 3 3 5 5 scores 19
21+
Four of a Kind Total of the four dice 4 4 4 4 6 scores 16
22+
Little Straight 30 points 1 2 3 4 5 scores 30
23+
Big Straight 30 points 2 3 4 5 6 scores 30
24+
Choice Sum of the dice 2 3 3 4 6 scores 18
25+
Yacht 50 points 4 4 4 4 4 scores 50
26+
27+
If the dice do not satisfy the requirements of a category, the score is zero.
28+
If, for example, *Four Of A Kind* is entered in the *Yacht* category, zero
29+
points are scored. A *Yacht* scores zero if entered in the *Full House* category.
30+
31+
## Task
32+
33+
Given a list of values for five dice and a category, your solution should return
34+
the score of the dice for that category. If the dice do not satisfy the requirements
35+
of the category your solution should return 0. You can assume that five values
36+
will always be presented, and the value of each will be between one and six
37+
inclusively. You should not assume that the dice are ordered.
38+
39+
# Running the tests
40+
41+
You can run all the tests for an exercise by entering
42+
43+
```sh
44+
$ gradle test
45+
```
46+
47+
in your terminal.
48+
49+
## Source
50+
51+
James Kilfiger, using wikipedia [https://en.wikipedia.org/wiki/Yacht_(dice_game)](https://en.wikipedia.org/wiki/Yacht_(dice_game))
52+
53+
## Submitting Incomplete Solutions
54+
55+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
56+

‎exercises/yacht/build.gradle‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Yacht {
2+
3+
Yacht(int[] dice, YachtCategory yachtCategory) {
4+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5+
}
6+
7+
int score() {
8+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9+
}
10+
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
enum YachtCategory {
2+
3+
YACHT,
4+
ONES,
5+
TWOS,
6+
THREES,
7+
FOURS,
8+
FIVES,
9+
SIXES,
10+
FULL_HOUSE,
11+
FOUR_OF_A_KIND,
12+
LITTLE_STRAIGHT,
13+
BIG_STRAIGHT,
14+
CHOICE
15+
16+
}

0 commit comments

Comments
 (0)