Skip to content

Commit da0bd85

Browse files
author
Emily Bache
committed
Merge pull request emilybache#1 from tarvaina/master
JavaScript implementation
2 parents ace5386 + e6cd6e8 commit da0bd85

File tree

4 files changed

+375
-0
lines changed

4 files changed

+375
-0
lines changed

javascript/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

javascript/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+

javascript/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "yatzy-refactoring-kata",
3+
"version": "0.0.1",
4+
"description": "Code for a refactoring exercise about the game of Yatzy",
5+
"dependencies": {},
6+
"devDependencies": {
7+
"mocha": "~1.17"
8+
}
9+
}

javascript/test/yatzy-test.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
var assert = require("assert");
2+
var Yatzy = require("../lib/yatzy");
3+
4+
5+
describe('Chance', function() {
6+
it('scores sum of all dice', function(){
7+
assert.equal(15, Yatzy.chance(2, 3, 4, 5, 1));
8+
assert.equal(16, Yatzy.chance(3, 3, 4, 5, 1));
9+
});
10+
});
11+
12+
describe("Yatzy", function() {
13+
it("scores 50", function() {
14+
assert.equal(50, Yatzy.yatzy(4,4,4,4,4));
15+
assert.equal(50, Yatzy.yatzy(6,6,6,6,6));
16+
assert.equal(0, Yatzy.yatzy(6,6,6,6,3));
17+
});
18+
});
19+
20+
describe("Ones", function() {
21+
it("score the sum of 1s", function() {
22+
assert.equal(1, Yatzy.ones(1,2,3,4,5));
23+
assert.equal(2, Yatzy.ones(1,2,1,4,5));
24+
assert.equal(0, Yatzy.ones(6,2,2,4,5));
25+
assert.equal(4, Yatzy.ones(1,2,1,1,1));
26+
});
27+
});
28+
29+
describe("Twos", function() {
30+
it("score the sum of 2s", function() {
31+
assert.equal(4, Yatzy.twos(1,2,3,2,6));
32+
assert.equal(10, Yatzy.twos(2,2,2,2,2));
33+
});
34+
});
35+
36+
describe("Threes", function() {
37+
it("score the sum of 3s", function() {
38+
assert.equal(6, Yatzy.threes(1,2,3,2,3));
39+
assert.equal(12, Yatzy.threes(2,3,3,3,3));
40+
});
41+
});
42+
43+
describe("Fours", function() {
44+
it("score the sum of 4s", function() {
45+
assert.equal(12, new Yatzy(4,4,4,5,5).fours());
46+
assert.equal(8, new Yatzy(4,4,5,5,5).fours());
47+
assert.equal(4, new Yatzy(4,5,5,5,5).fours());
48+
});
49+
});
50+
51+
describe("Fives", function() {
52+
it("score the sum of fives", function() {
53+
assert.equal(10, new Yatzy(4,4,4,5,5).fives());
54+
assert.equal(15, new Yatzy(4,4,5,5,5).fives());
55+
assert.equal(20, new Yatzy(4,5,5,5,5).fives());
56+
});
57+
});
58+
59+
describe("Sixes", function() {
60+
it("score the sum of sixes", function() {
61+
assert.equal(0, new Yatzy(4,4,4,5,5).sixes());
62+
assert.equal(6, new Yatzy(4,4,6,5,5).sixes());
63+
assert.equal(18, new Yatzy(6,5,6,6,5).sixes());
64+
});
65+
});
66+
67+
describe("One pair", function() {
68+
it("scores the sum of the highest pair", function() {
69+
assert.equal(6, Yatzy.score_pair(3,4,3,5,6));
70+
assert.equal(10, Yatzy.score_pair(5,3,3,3,5));
71+
assert.equal(12, Yatzy.score_pair(5,3,6,6,5));
72+
});
73+
});
74+
75+
describe("Two pair", function() {
76+
it("scores the sum of the two pairs", function() {
77+
assert.equal(16, Yatzy.two_pair(3,3,5,4,5));
78+
assert.equal(16, Yatzy.two_pair(3,3,5,5,5));
79+
});
80+
});
81+
82+
describe("Three of a kind", function() {
83+
it("scores the sum of the three of the kind", function() {
84+
assert.equal(9, Yatzy.three_of_a_kind(3,3,3,4,5));
85+
assert.equal(15, Yatzy.three_of_a_kind(5,3,5,4,5));
86+
assert.equal(9, Yatzy.three_of_a_kind(3,3,3,3,5));
87+
});
88+
});
89+
90+
describe("Four of a kind", function() {
91+
it("scores the sum of the four of the kind", function() {
92+
assert.equal(12, Yatzy.four_of_a_kind(3,3,3,3,5));
93+
assert.equal(20, Yatzy.four_of_a_kind(5,5,5,4,5));
94+
assert.equal(9, Yatzy.three_of_a_kind(3,3,3,3,3));
95+
});
96+
});
97+
98+
describe("Small straight", function() {
99+
it("scores 15", function() {
100+
assert.equal(15, Yatzy.smallStraight(1,2,3,4,5));
101+
assert.equal(15, Yatzy.smallStraight(2,3,4,5,1));
102+
assert.equal(0, Yatzy.smallStraight(1,2,2,4,5));
103+
});
104+
});
105+
106+
describe("Large straight", function() {
107+
it("scores 20", function() {
108+
assert.equal(20, Yatzy.largeStraight(6,2,3,4,5));
109+
assert.equal(20, Yatzy.largeStraight(2,3,4,5,6));
110+
assert.equal(0, Yatzy.largeStraight(1,2,2,4,5));
111+
});
112+
});
113+
114+
describe("Full house", function() {
115+
it("scores the sum of the full house", function() {
116+
assert.equal(18, Yatzy.fullHouse(6,2,2,2,6));
117+
assert.equal(0, Yatzy.fullHouse(2,3,4,5,6));
118+
});
119+
});

0 commit comments

Comments
 (0)