Last active
September 25, 2020 11:15
-
-
Save pazworld/63727d5b267d172e989aa9d9b8c09544 to your computer and use it in GitHub Desktop.
BowlingGameKata in Erlang 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.beam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(bowling_game). | |
-export([score/1]). | |
score(Rolls) -> score(0, Rolls). | |
%% All rolls processed | |
score(Score, []) -> Score; | |
%% 10th frame | |
score(Score, [X, Y, Z]) -> Score + X + Y + Z; | |
%% Strike | |
score(Score, [10 | Rest]) -> | |
score(Score + 10 + bonus(Rest, 2), Rest); | |
%% Spare | |
score(Score, [X, Y | Rest]) when X + Y =:= 10 -> | |
score(Score + 10 + bonus(Rest, 1), Rest); | |
%% Not strike nor spare | |
score(Score, [X, Y | Rest]) when X + Y =/= 10 -> | |
score(Score + X + Y, Rest). | |
bonus(_, 0) -> 0; | |
bonus([], _) -> 0; | |
bonus([Roll | Rest], N) -> Roll + bonus(Rest, N - 1). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(bowling_game_test). | |
-include_lib("eunit/include/eunit.hrl"). | |
gutter_rolls() -> lists:duplicate(20, 0). | |
all_one_rolls() -> lists:duplicate(20, 1). | |
one_spare_rolls() -> [5, 5, 3] ++ lists:duplicate(17, 0). | |
one_spare_rolls2() -> [0, 0, 3, 7, 7] ++ lists:duplicate(15, 0). | |
one_strike_rolls() -> [10, 3, 4] ++ lists:duplicate(16, 0). | |
perfect_game_rolls() -> lists:duplicate(12, 10). | |
gutter_game_test() -> | |
?assertEqual(0, bowling_game:score(gutter_rolls())). | |
all_ones_test() -> | |
?assertEqual(20, bowling_game:score(all_one_rolls())). | |
one_spare_test() -> | |
?assertEqual(16, bowling_game:score(one_spare_rolls())). | |
one_spare_another_test() -> | |
?assertEqual(24, bowling_game:score(one_spare_rolls2())). | |
one_strike_test() -> | |
?assertEqual(24, bowling_game:score(one_strike_rolls())). | |
perfect_game_test() -> | |
?assertEqual(300, bowling_game:score(perfect_game_rolls())). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment