Skip to content

Commit b6a929d

Browse files
authored
add Yatzy kata as both refactoring and green field (#58)
* add Yatzy kata as both refactoring and green field * Add default code for greenfield * uppercased the Yatzy function
1 parent bd98658 commit b6a929d

File tree

11 files changed

+999
-0
lines changed

11 files changed

+999
-0
lines changed

katas/yatzy-refactoring/.nvmrc

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

katas/yatzy-refactoring/README.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# JavaScript version of Yatzy Refactoring Kata
2+
3+
Make sure you have node.js and npm installed. Then in this directory:
4+
5+
# To install mocha in node_modules:
6+
npm install
7+
8+
# To run unit tests:
9+
node_modules/mocha/bin/mocha
10+
11+
# To run unit tests in watch mode:
12+
node_modules/mocha/bin/mocha -w
13+
14+
# # Yatzy Refactoring Kata
15+
16+
From https://github.com/emilybache/Yatzy-Refactoring-Kata
17+
18+
This Refactoring Kata was designed by Jon Jagger and is available in his Cyber-Dojo. See [his blog post](http://jonjagger.blogspot.co.uk/2012/05/yahtzee-cyber-dojo-refactoring-in-java.html)
19+
20+
I have changed it a little, so that the rules more closely match the original game.
21+
22+
The other language translations have been contributed by:
23+
24+
- Python: Emily Bache
25+
- Ruby: Kim Persson and Lennart Fridén
26+
- Javascript: Antti Tarvainen
27+
28+
## Kata: Yatzy rules
29+
30+
The game of Yatzy is a simple dice game. Each player
31+
rolls five six-sided dice. They can re-roll some or all
32+
of the dice up to three times (including the original roll).
33+
34+
For example, suppose a players rolls:
35+
36+
3,4,5,5,2
37+
38+
They hold (-,-,5,5,-) and re-roll (3,4,-,-,2):
39+
40+
5,1,5,5,3
41+
42+
They hold (5,-,5,5,-) and re-roll (-,1,-,-,3):
43+
44+
5,6,5,5,2
45+
46+
The player then places the roll in a category, such as ones,
47+
twos, fives, pair, two pairs etc (see below). If the roll is
48+
compatible with the category, the player gets a score for the
49+
roll according to the rules. If the roll is not compatible
50+
with the category, the player scores zero for the roll.
51+
52+
For example, suppose a player scores 5,6,5,5,2 in the fives
53+
category they would score 15 (three fives). The score for
54+
that go is then added to their total and the category cannot
55+
be used again in the remaining goes for that game.
56+
A full game consists of one go for each category. Thus, for
57+
their last go in a game, a player must choose their only
58+
remaining category.
59+
60+
Your task is to score a GIVEN roll in a GIVEN category.
61+
You do NOT have to program the random dice rolling.
62+
The game is NOT played by letting the computer choose the
63+
highest scoring category for a given roll.
64+
65+
66+
## Kata: Yatzy Categories and Scoring Rules
67+
68+
### Chance:
69+
The player scores the sum of all dice, no matter what they read.
70+
For example:
71+
72+
- 1,1,3,3,6 placed on "chance" scores 14 (1+1+3+3+6)
73+
- 4,5,5,6,1 placed on "chance" scores 21 (4+5+5+6+1)
74+
75+
### Yatzy:
76+
If all dice have the same number,
77+
the player scores 50 points.
78+
For example:
79+
80+
- 1,1,1,1,1 placed on "yatzy" scores 50
81+
- 1,1,1,2,1 placed on "yatzy" scores 0
82+
83+
### Ones, Twos, Threes, Fours, Fives, Sixes:
84+
The player scores the sum of the dice that reads one,
85+
two, three, four, five or six, respectively.
86+
For example:
87+
88+
- 1,1,2,4,4 placed on "fours" scores 8 (4+4)
89+
- 2,3,2,5,1 placed on "twos" scores 4 (2+2)
90+
- 3,3,3,4,5 placed on "ones" scores 0
91+
92+
### Pair:
93+
The player scores the sum of the two highest matching dice.
94+
For example, when placed on "pair":
95+
96+
- 3,3,3,4,4 scores 8 (4+4)
97+
- 1,1,6,2,6 scores 12 (6+6)
98+
- 3,3,3,4,1 scores 6 (3+3)
99+
- 3,3,3,3,1 scores 6 (3+3)
100+
101+
### Two pairs:
102+
If there are two pairs of dice with the same number, the
103+
player scores the sum of these dice.
104+
For example, when placed on "two pairs":
105+
106+
- 1,1,2,3,3 scores 8 (1+1+3+3)
107+
- 1,1,2,3,4 scores 0
108+
- 1,1,2,2,2 scores 6 (1+1+2+2)
109+
110+
### Three of a kind:
111+
If there are three dice with the same number, the player
112+
scores the sum of these dice.
113+
For example, when placed on "three of a kind":
114+
115+
- 3,3,3,4,5 scores 9 (3+3+3)
116+
- 3,3,4,5,6 scores 0
117+
- 3,3,3,3,1 scores 9 (3+3+3)
118+
119+
### Four of a kind:
120+
If there are four dice with the same number, the player
121+
scores the sum of these dice.
122+
For example, when placed on "four of a kind":
123+
124+
- 2,2,2,2,5 scores 8 (2+2+2+2)
125+
- 2,2,2,5,5 scores 0
126+
- 2,2,2,2,2 scores 8 (2+2+2+2)
127+
128+
### Small straight:
129+
When placed on "small straight", if the dice read
130+
131+
1,2,3,4,5,
132+
133+
the player scores 15 (the sum of all the dice).
134+
135+
### Large straight:
136+
When placed on "large straight", if the dice read
137+
138+
2,3,4,5,6,
139+
140+
the player scores 20 (the sum of all the dice).
141+
142+
### Full house:
143+
If the dice are two of a kind and three of a kind, the
144+
player scores the sum of all the dice.
145+
For example, when placed on "full house":
146+
147+
- 1,1,2,2,2 scores 8 (1+1+2+2+2)
148+
- 2,2,3,3,4 scores 0
149+
- 4,4,4,4,4 scores 0

katas/yatzy-refactoring/lib/yatzy.js

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
var Yatzy = function(d1, d2, d3, d4, _5) {
2+
this.dice = [];
3+
this.dice[0] = d1;
4+
this.dice[1] = d2;
5+
this.dice[2] = d3;
6+
this.dice[3] = d4;
7+
this.dice[4] = _5;
8+
9+
this.fours = function()
10+
{
11+
var sum;
12+
sum = 0;
13+
for (at = 0; at != 5; at++) {
14+
if (this.dice[at] == 4) {
15+
sum += 4;
16+
}
17+
}
18+
return sum;
19+
}
20+
21+
this.fives = function()
22+
{
23+
s = 0
24+
var i
25+
for (i = 0; i < this.dice.length; i++)
26+
if (this.dice[i] == 5)
27+
s = s + 5;
28+
return s;
29+
}
30+
31+
this.sixes = function()
32+
{
33+
sum = 0;
34+
for (var at = 0; at < this.dice.length; at++)
35+
if (this.dice[at] == 6)
36+
sum = sum + 6;
37+
return sum;
38+
}
39+
}
40+
41+
42+
43+
Yatzy.chance = function(d1, d2, d3, d4, d5) {
44+
var total = 0;
45+
total += d1;
46+
total += d2;
47+
total += d3;
48+
total += d4;
49+
total += d5;
50+
return total;
51+
}
52+
53+
Yatzy.yatzy = function() {
54+
var counts = [0, 0, 0, 0, 0, 0, 0, 0];
55+
for (var i = 0; i != arguments.length; ++i) {
56+
var die = arguments[i];
57+
counts[die-1]++; }
58+
for (i = 0; i != 6; i++)
59+
if (counts[i] == 5)
60+
return 50;
61+
return 0;
62+
}
63+
64+
Yatzy.ones = function(d1, d2, d3, d4, d5) {
65+
var sum = 0;
66+
if (d1 == 1) sum++;
67+
if (d2 == 1) sum++;
68+
if (d3 == 1) sum++;
69+
if (d4 == 1) sum++;
70+
if (d5 == 1)
71+
sum++;
72+
73+
return sum;
74+
}
75+
76+
Yatzy.twos = function(d1, d2, d3, d4, d5) {
77+
var sum = 0;
78+
if (d1 == 2) sum += 2;
79+
if (d2 == 2) sum += 2;
80+
if (d3 == 2) sum += 2;
81+
if (d4 == 2) sum += 2;
82+
if (d5 == 2) sum += 2;
83+
return sum;
84+
}
85+
86+
Yatzy.threes = function(d1, d2, d3, d4, d5) {
87+
var s;
88+
s = 0;
89+
if (d1 == 3) s += 3;
90+
if (d2 == 3) s += 3;
91+
if (d3 == 3) s += 3;
92+
if (d4 == 3) s += 3;
93+
if (d5 == 3) s += 3;
94+
return s;
95+
}
96+
97+
Yatzy.score_pair = function(d1, d2, d3, d4, d5)
98+
{
99+
var counts = [0, 0, 0, 0, 0, 0, 0, 0, 0];
100+
counts[d1-1]++;
101+
counts[d2-1]++;
102+
counts[d3-1]++;
103+
counts[d4-1]++;
104+
counts[d5-1]++;
105+
var at;
106+
for (at = 0; at != 6; at++)
107+
if (counts[6-at-1] >= 2)
108+
return (6-at)*2;
109+
return 0;
110+
}
111+
112+
Yatzy.two_pair = function(d1, d2, d3, d4, d5)
113+
{
114+
var counts = [0, 0, 0, 0, 0, 0, 0, 0, 0];
115+
counts[d1-1]++;
116+
counts[d2-1]++
117+
counts[d3-1]++
118+
counts[d4-1]++;
119+
counts[d5-1]++;
120+
var n = 0;
121+
var score = 0;
122+
for (i = 0; i < 6; i += 1)
123+
if (counts[6-i-1] >= 2) {
124+
n++;
125+
score += (6-i);
126+
}
127+
if (n == 2)
128+
return score * 2;
129+
else
130+
return 0;
131+
}
132+
133+
Yatzy.four_of_a_kind = function(_1, _2, d3, d4, d5)
134+
{
135+
var tallies;
136+
tallies = [0, 0, 0, 0, 0, 0, 0, 0]
137+
tallies[_1-1]++;
138+
tallies[_2-1]++;
139+
tallies[d3-1]++;
140+
tallies[d4-1]++;
141+
tallies[d5-1]++;
142+
for (i = 0; i < 6; i++)
143+
if (tallies[i] >= 4)
144+
return (i+1) * 4;
145+
return 0;
146+
}
147+
148+
Yatzy.three_of_a_kind = function(d1, d2, d3, d4, d5)
149+
{
150+
var t;
151+
t = [0, 0, 0, 0, 0, 0, 0, 0, 0]
152+
t[d1-1]++;
153+
t[d2-1]++;
154+
t[d3-1]++;
155+
t[d4-1]++;
156+
t[d5-1]++;
157+
for (i = 0; i < 6; i++)
158+
if (t[i] >= 3)
159+
return (i+1) * 3;
160+
return 0;
161+
}
162+
163+
Yatzy.smallStraight = function(d1, d2, d3, d4, d5)
164+
{
165+
var tallies;
166+
tallies = [0, 0, 0, 0, 0, 0, 0]
167+
tallies[d1-1] += 1;
168+
tallies[d2-1] += 1;
169+
tallies[d3-1] += 1;
170+
tallies[d4-1] += 1;
171+
tallies[d5-1] += 1;
172+
if (tallies[0] == 1 &&
173+
tallies[1] == 1 &&
174+
tallies[2] == 1 &&
175+
tallies[3] == 1 &&
176+
tallies[4] == 1)
177+
return 15;
178+
return 0;
179+
}
180+
181+
Yatzy.largeStraight = function(d1, d2, d3, d4, d5)
182+
{
183+
var tallies;
184+
tallies = [0, 0, 0, 0,0,0,0,0];
185+
tallies[d1-1] += 1;
186+
tallies[d2-1] += 1;
187+
tallies[d3-1] += 1;
188+
tallies[d4-1] += 1;
189+
tallies[d5-1] += 1;
190+
if (tallies[1] == 1 &&
191+
tallies[2] == 1 &&
192+
tallies[3] == 1 &&
193+
tallies[4] == 1
194+
&& tallies[5] == 1)
195+
return 20;
196+
return 0;
197+
}
198+
199+
Yatzy.fullHouse = function(d1, d2, d3, d4, d5)
200+
{
201+
var tallies;
202+
var _2 = false;
203+
var i;
204+
var _2_at = 0;
205+
var _3 = false;
206+
var _3_at = 0;
207+
208+
209+
210+
211+
tallies = [0, 0, 0, 0, 0, 0, 0, 0];
212+
tallies[d1-1] += 1;
213+
tallies[d2-1] += 1;
214+
tallies[d3-1] += 1;
215+
tallies[d4-1] += 1;
216+
tallies[d5-1] += 1;
217+
218+
for (i = 0; i != 6; i += 1)
219+
if (tallies[i] == 2) {
220+
_2 = true;
221+
_2_at = i+1;
222+
}
223+
224+
for (i = 0; i != 6; i += 1)
225+
if (tallies[i] == 3) {
226+
_3 = true;
227+
_3_at = i+1;
228+
}
229+
230+
if (_2 && _3)
231+
return _2_at * 2 + _3_at * 3;
232+
else
233+
return 0;
234+
}
235+
236+
module.exports = Yatzy;
237+
238+

0 commit comments

Comments
 (0)