Skip to content

Commit c6772c9

Browse files
authored
Merge pull request #347 from britto/bowling-update-implementation
[Bowling] Update implementation and tests
2 parents a9c05cd + 30b6c2d commit c6772c9

File tree

2 files changed

+95
-22
lines changed

2 files changed

+95
-22
lines changed

exercises/bowling/bowling_test.exs

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule BowlingTest do
1212
Enum.reduce(rolls, game, fn(roll, game) -> Bowling.roll(game, roll) end)
1313
end
1414

15-
test "can score all 0s" do
15+
test "should be able to score a game with all zeros" do
1616
game = Bowling.start
1717
rolls = [0, 0,
1818
0, 0,
@@ -29,7 +29,7 @@ defmodule BowlingTest do
2929
end
3030

3131
@tag :pending
32-
test "can score a game with no strikes or spares" do
32+
test "should be able to score a game with no strikes or spares" do
3333
game = Bowling.start
3434
rolls = [3, 6,
3535
3, 6,
@@ -46,7 +46,7 @@ defmodule BowlingTest do
4646
end
4747

4848
@tag :pending
49-
test "spare followed by all 0s is worth 10 points" do
49+
test "a spare followed by zeros is worth ten points" do
5050
game = Bowling.start
5151
rolls = [6, 4,
5252
0, 0,
@@ -63,7 +63,7 @@ defmodule BowlingTest do
6363
end
6464

6565
@tag :pending
66-
test "points scored in the roll after the spare are counted twice" do
66+
test "points scored in the roll after a spare are counted twice" do
6767
game = Bowling.start
6868
rolls = [6, 4,
6969
3, 0,
@@ -115,7 +115,7 @@ defmodule BowlingTest do
115115
end
116116

117117
@tag :pending
118-
test "a strike earns ten points in frame with a single roll" do
118+
test "a strike earns ten points in a frame with a single roll" do
119119
game = Bowling.start
120120
rolls = [10,
121121
0, 0,
@@ -258,26 +258,26 @@ defmodule BowlingTest do
258258
end
259259

260260
@tag :pending
261-
test "rolls can not score negative points" do
261+
test "rolls cannot score negative points" do
262262
game = Bowling.start
263-
assert Bowling.roll(game, -1) == {:error, "Pins must have a value from 0 to 10"}
263+
assert Bowling.roll(game, -1) == {:error, "Negative roll is invalid"}
264264
end
265265

266266
@tag :pending
267-
test "a roll can not score more than 10 points" do
267+
test "a roll cannot score more than 10 points" do
268268
game = Bowling.start
269-
assert Bowling.roll(game, 11) == {:error, "Pins must have a value from 0 to 10"}
269+
assert Bowling.roll(game, 11) == {:error, "Pin count exceeds pins on the lane"}
270270
end
271271

272272
@tag :pending
273-
test "two rolls in a frame can not score more than 10 points" do
273+
test "two rolls in a frame cannot score more than 10 points" do
274274
game = Bowling.start
275275
game = Bowling.roll(game, 5)
276276
assert Bowling.roll(game, 6) == {:error, "Pin count exceeds pins on the lane"}
277277
end
278278

279279
@tag :pending
280-
test "two bonus rolls after a strike in the last frame can not score more than 10 points" do
280+
test "bonus roll after a strike in the last frame cannot score more than 10 points" do
281281
game = Bowling.start
282282
rolls = [0, 0,
283283
0, 0,
@@ -288,26 +288,100 @@ defmodule BowlingTest do
288288
0, 0,
289289
0, 0,
290290
0, 0,
291+
10]
292+
game = roll_reduce(game, rolls)
293+
assert Bowling.roll(game, 11) == {:error, "Pin count exceeds pins on the lane"}
294+
end
295+
296+
@tag :pending
297+
test "two bonus rolls after a strike in the last frame cannot score more than 10 points" do
298+
game = Bowling.start
299+
rolls = [0, 0,
300+
0, 0,
301+
0, 0,
302+
0, 0,
303+
0, 0,
304+
0, 0,
305+
0, 0,
306+
0, 0,
307+
0, 0,
308+
10,
291309
5]
292310
game = roll_reduce(game, rolls)
293311
assert Bowling.roll(game, 6) == {:error, "Pin count exceeds pins on the lane"}
294312
end
295313

296314
@tag :pending
297-
test "an unstarted game can not be scored" do
315+
test "two bonus rolls after a strike in the last frame can score more than 10 points if one is a strike" do
316+
game = Bowling.start
317+
rolls = [0, 0,
318+
0, 0,
319+
0, 0,
320+
0, 0,
321+
0, 0,
322+
0, 0,
323+
0, 0,
324+
0, 0,
325+
0, 0,
326+
10,
327+
10,
328+
6]
329+
game = roll_reduce(game, rolls)
330+
assert Bowling.score(game) == 26
331+
end
332+
333+
@tag :pending
334+
test "the second bonus rolls after a strike in the last frame cannot be a strike if the first one is not a strike" do
335+
game = Bowling.start
336+
rolls = [0, 0,
337+
0, 0,
338+
0, 0,
339+
0, 0,
340+
0, 0,
341+
0, 0,
342+
0, 0,
343+
0, 0,
344+
0, 0,
345+
10,
346+
6]
347+
game = roll_reduce(game, rolls)
348+
assert Bowling.roll(game, 10) == {:error, "Pin count exceeds pins on the lane"}
349+
end
350+
351+
@tag :pending
352+
test "second bonus roll after a strike in the last frame cannot score more than 10 points" do
353+
game = Bowling.start
354+
rolls = [0, 0,
355+
0, 0,
356+
0, 0,
357+
0, 0,
358+
0, 0,
359+
0, 0,
360+
0, 0,
361+
0, 0,
362+
0, 0,
363+
10,
364+
10]
365+
game = roll_reduce(game, rolls)
366+
assert Bowling.roll(game, 11) == {:error, "Pin count exceeds pins on the lane"}
367+
end
368+
369+
@tag :pending
370+
test "an unstarted game cannot be scored" do
298371
game = Bowling.start
299372
assert Bowling.score(game) == {:error, "Score cannot be taken until the end of the game"}
300373
end
301374

302375
@tag :pending
303-
test "score cannot be taken until the end of the game" do
376+
test "an incomplete game cannot be scored" do
304377
game = Bowling.start
305-
game = Bowling.roll(game, 0)
378+
rolls = [0, 0]
379+
game = roll_reduce(game, rolls)
306380
assert Bowling.score(game) == {:error, "Score cannot be taken until the end of the game"}
307381
end
308382

309383
@tag :pending
310-
test "a game with more than ten frames can not be scored" do
384+
test "cannot roll if game already has ten frames" do
311385
game = Bowling.start
312386
rolls = [0, 0,
313387
0, 0,
@@ -318,10 +392,9 @@ defmodule BowlingTest do
318392
0, 0,
319393
0, 0,
320394
0, 0,
321-
0, 0,
322-
0]
395+
0, 0]
323396
game = roll_reduce(game, rolls)
324-
assert Bowling.score(game) == {:error, "Invalid game: too many frames"}
397+
assert Bowling.roll(game, 0) == {:error, "Cannot roll after game is over"}
325398
end
326399

327400
@tag :pending

exercises/bowling/example.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ defmodule Bowling do
1010
end
1111

1212
def roll(_, score) when score < 0 do
13-
{:error, "Pins must have a value from 0 to 10"}
13+
{:error, "Negative roll is invalid"}
1414
end
1515

1616
def roll(_, score) when score > 10 do
17-
{:error, "Pins must have a value from 0 to 10"}
17+
{:error, "Pin count exceeds pins on the lane"}
1818
end
1919

2020
def roll(game, score) do
2121
updates = update_score(game.roll_in_frame, game, score)
2222
cond do
23+
too_many_frames?(updates) ->
24+
{:error, "Cannot roll after game is over"}
2325
valid_updates?(updates) ->
2426
updates
2527
true ->
@@ -58,8 +60,6 @@ defmodule Bowling do
5860
{:error, "Score cannot be taken until the end of the game"}
5961
bonus_roll_remaining?(game) ->
6062
{:error, "Score cannot be taken until the end of the game"}
61-
too_many_frames?(game) ->
62-
{:error, "Invalid game: too many frames"}
6363
true ->
6464
parse_scores(game.scores)
6565
end

0 commit comments

Comments
 (0)