Skip to content

Instantly share code, notes, and snippets.

@o314
Created February 10, 2021 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save o314/ec952b7a193f79ad3410892779528049 to your computer and use it in GitHub Desktop.
Save o314/ec952b7a193f79ad3410892779528049 to your computer and use it in GitHub Desktop.
last_kata.jl
#= -----------------------------------------------------------------------------
GREED (DICE GAME) KATA
--------------------------------------------------------------------------------
Write a class Greed with a score() method that accepts an array of die values
(up to 6). Scoring rules are as follows:
A single one (100)
A single five (50)
Triple ones [1,1,1] (1000)
Triple twos [2,2,2] (200)
Triple threes [3,3,3] (300)
Triple fours [4,4,4] (400)
Triple fives [5,5,5] (500)
Triple sixes [6,6,6] (600)
Note that the scorer should work for any number of dice up to 6.
Four-of-a-kind (Multiply Triple Score by 2)
Five-of-a-kind (Multiply Triple Score by 4)
Six-of-a-kind (Multiply Triple Score by 8)
Three Pairs [2,2,3,3,4,4] (800)
Straight [1,2,3,4,5,6] (1200)
----------------------------------------------------------------------------- =#
struct Greed
rolls :: Vector{Int}
Greed(rolls::Vector{Int}) = begin
length(rolls)<=6 && all(in(1:6),rolls) || throw(ArgumentError("rolls = $rolls"))
new(rolls)
end
end
# v1
score_v1(a::Greed) =
a.rolls == [1] ? 100 :
a.rolls == [5] ? 50 :
a.rolls == [1,1,1] ? 1000 :
a.rolls == [2,2,2] ? 200 :
a.rolls == [3,3,3] ? 300 :
a.rolls == [4,4,4] ? 400 :
a.rolls == [5,5,5] ? 500 :
a.rolls == [6,6,6] ? 600 :
0
# v2
is_triple_kind(a::Greed) = length(a.rolls)==3 && all(==(a.rolls[1]), a.rolls[2:3])
triple_score(a::Greed) = let k=a.rolls[1]; k==1 ? 1000 : 100k end
score_v2(a::Greed) =
a.rolls == [1] ? 100 :
a.rolls == [5] ? 50 :
is_triple_kind(a) ? triple_score(a) :
0
# v3
is_nth_kind(a::Greed, n::Int) = length(a.rolls)==n && all(==(a.rolls[1]), a.rolls[2:(n-1)])
is_four_kind(a::Greed) = is_nth_kind(a, 4)
is_five_kind(a::Greed) = is_nth_kind(a, 5)
is_six_kind(a::Greed) = is_nth_kind(a, 6)
four_score(a::Greed) = 2*triple_score(a)
five_score(a::Greed) = 4*triple_score(a)
six_score(a::Greed) = 8*triple_score(a)
score_v3(a::Greed) =
a.rolls == [1] ? 100 :
a.rolls == [5] ? 50 :
is_triple_kind(a) ? triple_score(a) :
is_four_kind(a) ? four_score(a) :
is_five_kind(a) ? five_score(a) :
is_six_kind(a) ? six_score(a) :
0
# v4
is_three_pairs(a::Greed) = begin
length(a.rolls) == 6 || return false
b = sort(a.rolls)
b[1]==b[2] && b[2]!=b[3] &&
b[3]==b[4] && b[4]!=b[5] &&
b[5]==b[6] && b[6]!=b[1]
end
score_v4(a::Greed) =
a.rolls == [1] ? 100 :
a.rolls == [5] ? 50 :
is_triple_kind(a) ? triple_score(a) :
is_four_kind(a) ? four_score(a) :
is_five_kind(a) ? five_score(a) :
is_six_kind(a) ? six_score(a) :
is_three_pairs(a) ? 800 :
a.rolls == [1,2,3,4,5,6] ? 1200 :
0
# ------------------------------------------------------------------------------
using Test
@testset "greed" begin
@test Greed([1:4...]) isa Greed
@test_throws ArgumentError Greed([1:7...])
@test_throws ArgumentError Greed([3:8...])
@testset "score_v1" begin
@test [1] |> Greed |> score_v1 == 100
@test [5] |> Greed |> score_v1 == 50
@test [1,1,1] |> Greed |> score_v1 == 1000
@test [2,2,2] |> Greed |> score_v1 == 200
@test [3,3,3] |> Greed |> score_v1 == 300
@test [4,4,4] |> Greed |> score_v1 == 400
@test [5,5,5] |> Greed |> score_v1 == 500
@test [6,6,6] |> Greed |> score_v1 == 600
end
@testset "score_v2" begin
@test [1,1,1] |> Greed |> is_triple_kind
@test [2,2,2] |> Greed |> is_triple_kind
@test [3,3,3] |> Greed |> is_triple_kind
@test [4,4,4] |> Greed |> is_triple_kind
@test [5,5,5] |> Greed |> is_triple_kind
@test [6,6,6] |> Greed |> is_triple_kind
@test [1,1,1] |> Greed |> triple_score == 1000
@test [2,2,2] |> Greed |> triple_score == 200
@test [3,3,3] |> Greed |> triple_score == 300
@test [4,4,4] |> Greed |> triple_score == 400
@test [5,5,5] |> Greed |> triple_score == 500
@test [6,6,6] |> Greed |> triple_score == 600
@test [1] |> Greed |> score_v2 == 100
@test [5] |> Greed |> score_v2 == 50
@test [1,1,1] |> Greed |> score_v2 == 1000
@test [2,2,2] |> Greed |> score_v2 == 200
@test [3,3,3] |> Greed |> score_v2 == 300
@test [4,4,4] |> Greed |> score_v2 == 400
@test [5,5,5] |> Greed |> score_v2 == 500
@test [6,6,6] |> Greed |> score_v2 == 600
end
@testset "score_v3" begin
@test [1] |> Greed |> score_v3 == 100
@test [5] |> Greed |> score_v3 == 50
@test [1,1,1] |> Greed |> score_v3 == 1000
@test [2,2,2] |> Greed |> score_v3 == 200
@test [3,3,3] |> Greed |> score_v3 == 300
@test [4,4,4] |> Greed |> score_v3 == 400
@test [5,5,5] |> Greed |> score_v3 == 500
@test [6,6,6] |> Greed |> score_v3 == 600
@test [4,4,4,4] |> Greed |> score_v3 == 800
@test [5,5,5,5,5] |> Greed |> score_v3 == 2000
@test [6,6,6,6,6,6] |> Greed |> score_v3 == 4800
end
@testset "score_v4" begin
@test [1] |> Greed |> score_v4 == 100
@test [5] |> Greed |> score_v4 == 50
@test [1,1,1] |> Greed |> score_v4 == 1000
@test [2,2,2] |> Greed |> score_v4 == 200
@test [3,3,3] |> Greed |> score_v4 == 300
@test [4,4,4] |> Greed |> score_v4 == 400
@test [5,5,5] |> Greed |> score_v4 == 500
@test [6,6,6] |> Greed |> score_v4 == 600
@test [4,4,4,4] |> Greed |> score_v4 == 800
@test [5,5,5,5,5] |> Greed |> score_v4 == 2000
@test [6,6,6,6,6,6] |> Greed |> score_v4 == 4800
@test [2,2,3,3,4,4] |> Greed |> score_v4 == 800
@test [1,2,3,4,5,6] |> Greed |> score_v4 == 1200
end
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment