Skip to content

Commit

Permalink
Define category datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Jan 2, 2024
1 parent 5010b45 commit da398d6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 44 deletions.
42 changes: 28 additions & 14 deletions exercises/practice/yacht/.meta/example.sml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
datatype category =
Ones
| Twos
| Threes
| Fours
| Fives
| Sixes
| FullHouse
| FourOfAKind
| LittleStraight
| BigStraight
| Choice
| Yacht

local
fun insert (value: int) (l: int list) =
case l of
Expand Down Expand Up @@ -76,22 +90,22 @@ local
if List.nth (dice, 0) = List.nth (dice, 4) then 50
else 0

fun score' (dice: int list, category: string): int =
fun score' (dice: int list, category): int =
case category of
"ones" => ones dice
| "twos" => twos dice
| "threes" => threes dice
| "fours" => fours dice
| "fives" => fives dice
| "sixes" => sixes dice
| "full house" => fullHouse dice
| "four of a kind" => fourOfAKind dice
| "little straight" => littleStraight dice
| "big straight" => bigStraight dice
| "choice" => choice dice
| "yacht" => yacht dice
Ones => ones dice
| Twos => twos dice
| Threes => threes dice
| Fours => fours dice
| Fives => fives dice
| Sixes => sixes dice
| FullHouse => fullHouse dice
| FourOfAKind => fourOfAKind dice
| LittleStraight => littleStraight dice
| BigStraight => bigStraight dice
| Choice => choice dice
| Yacht => yacht dice
| _ => raise Fail "invalid category"
in
fun score (dice: int list, category: string): int =
fun score (dice: int list, category): int =
score' (sort dice, category)
end
58 changes: 29 additions & 29 deletions exercises/practice/yacht/test.sml
Original file line number Diff line number Diff line change
Expand Up @@ -9,91 +9,91 @@ fun x |> f = f x
val testsuite =
describe "yacht" [
test "Yacht"
(fn _ => score ([5, 5, 5, 5, 5], "yacht") |> Expect.equalTo 50),
(fn _ => score ([5, 5, 5, 5, 5], Yacht) |> Expect.equalTo 50),

test "Not Yacht"
(fn _ => score ([1, 3, 3, 2, 5], "yacht") |> Expect.equalTo 0),
(fn _ => score ([1, 3, 3, 2, 5], Yacht) |> Expect.equalTo 0),

test "Ones"
(fn _ => score ([1, 1, 1, 3, 5], "ones") |> Expect.equalTo 3),
(fn _ => score ([1, 1, 1, 3, 5], Ones) |> Expect.equalTo 3),

test "Ones, out of order"
(fn _ => score ([3, 1, 1, 5, 1], "ones") |> Expect.equalTo 3),
(fn _ => score ([3, 1, 1, 5, 1], Ones) |> Expect.equalTo 3),

test "No ones"
(fn _ => score ([4, 3, 6, 5, 5], "ones") |> Expect.equalTo 0),
(fn _ => score ([4, 3, 6, 5, 5], Ones) |> Expect.equalTo 0),

test "Twos"
(fn _ => score ([2, 3, 4, 5, 6], "twos") |> Expect.equalTo 2),
(fn _ => score ([2, 3, 4, 5, 6], Twos) |> Expect.equalTo 2),

test "Fours"
(fn _ => score ([1, 4, 1, 4, 1], "fours") |> Expect.equalTo 8),
(fn _ => score ([1, 4, 1, 4, 1], Fours) |> Expect.equalTo 8),

test "Yacht counted as threes"
(fn _ => score ([3, 3, 3, 3, 3], "threes") |> Expect.equalTo 15),
(fn _ => score ([3, 3, 3, 3, 3], Threes) |> Expect.equalTo 15),

test "Yacht of 3s counted as fives"
(fn _ => score ([3, 3, 3, 3, 3], "fives") |> Expect.equalTo 0),
(fn _ => score ([3, 3, 3, 3, 3], Fives) |> Expect.equalTo 0),

test "Fives"
(fn _ => score ([1, 5, 3, 5, 3], "fives") |> Expect.equalTo 10),
(fn _ => score ([1, 5, 3, 5, 3], Fives) |> Expect.equalTo 10),

test "Sixes"
(fn _ => score ([2, 3, 4, 5, 6], "sixes") |> Expect.equalTo 6),
(fn _ => score ([2, 3, 4, 5, 6], Sixes) |> Expect.equalTo 6),

test "Full house two small, three big"
(fn _ => score ([2, 2, 4, 4, 4], "full house") |> Expect.equalTo 16),
(fn _ => score ([2, 2, 4, 4, 4], FullHouse) |> Expect.equalTo 16),

test "Full house three small, two big"
(fn _ => score ([5, 3, 3, 5, 3], "full house") |> Expect.equalTo 19),
(fn _ => score ([5, 3, 3, 5, 3], FullHouse) |> Expect.equalTo 19),

test "Two pair is not a full house"
(fn _ => score ([2, 2, 4, 4, 5], "full house") |> Expect.equalTo 0),
(fn _ => score ([2, 2, 4, 4, 5], FullHouse) |> Expect.equalTo 0),

test "Four of a kind is not a full house"
(fn _ => score ([1, 4, 4, 4, 4], "full house") |> Expect.equalTo 0),
(fn _ => score ([1, 4, 4, 4, 4], FullHouse) |> Expect.equalTo 0),

test "Yacht is not a full house"
(fn _ => score ([2, 2, 2, 2, 2], "full house") |> Expect.equalTo 0),
(fn _ => score ([2, 2, 2, 2, 2], FullHouse) |> Expect.equalTo 0),

test "Four of a Kind"
(fn _ => score ([6, 6, 4, 6, 6], "four of a kind") |> Expect.equalTo 24),
(fn _ => score ([6, 6, 4, 6, 6], FourOfAKind) |> Expect.equalTo 24),

test "Yacht can be scored as Four of a Kind"
(fn _ => score ([3, 3, 3, 3, 3], "four of a kind") |> Expect.equalTo 12),
(fn _ => score ([3, 3, 3, 3, 3], FourOfAKind) |> Expect.equalTo 12),

test "Full house is not Four of a Kind"
(fn _ => score ([3, 3, 3, 5, 5], "four of a kind") |> Expect.equalTo 0),
(fn _ => score ([3, 3, 3, 5, 5], FourOfAKind) |> Expect.equalTo 0),

test "Little Straight"
(fn _ => score ([3, 5, 4, 1, 2], "little straight") |> Expect.equalTo 30),
(fn _ => score ([3, 5, 4, 1, 2], LittleStraight) |> Expect.equalTo 30),

test "Little Straight as Big Straight"
(fn _ => score ([1, 2, 3, 4, 5], "big straight") |> Expect.equalTo 0),
(fn _ => score ([1, 2, 3, 4, 5], BigStraight) |> Expect.equalTo 0),

test "Four in order but not a little straight"
(fn _ => score ([1, 1, 2, 3, 4], "little straight") |> Expect.equalTo 0),
(fn _ => score ([1, 1, 2, 3, 4], LittleStraight) |> Expect.equalTo 0),

test "No pairs but not a little straight"
(fn _ => score ([1, 2, 3, 4, 6], "little straight") |> Expect.equalTo 0),
(fn _ => score ([1, 2, 3, 4, 6], LittleStraight) |> Expect.equalTo 0),

test "Minimum is 1, maximum is 5, but not a little straight"
(fn _ => score ([1, 1, 3, 4, 5], "little straight") |> Expect.equalTo 0),
(fn _ => score ([1, 1, 3, 4, 5], LittleStraight) |> Expect.equalTo 0),

test "Big Straight"
(fn _ => score ([4, 6, 2, 5, 3], "big straight") |> Expect.equalTo 30),
(fn _ => score ([4, 6, 2, 5, 3], BigStraight) |> Expect.equalTo 30),

test "Big Straight as little straight"
(fn _ => score ([6, 5, 4, 3, 2], "little straight") |> Expect.equalTo 0),
(fn _ => score ([6, 5, 4, 3, 2], LittleStraight) |> Expect.equalTo 0),

test "No pairs but not a big straight"
(fn _ => score ([6, 5, 4, 3, 1], "big straight") |> Expect.equalTo 0),
(fn _ => score ([6, 5, 4, 3, 1], BigStraight) |> Expect.equalTo 0),

test "Choice"
(fn _ => score ([3, 3, 5, 6, 6], "choice") |> Expect.equalTo 23),
(fn _ => score ([3, 3, 5, 6, 6], Choice) |> Expect.equalTo 23),

test "Yacht as choice"
(fn _ => score ([2, 2, 2, 2, 2], "choice") |> Expect.equalTo 10)
(fn _ => score ([2, 2, 2, 2, 2], Choice) |> Expect.equalTo 10)
]

val _ = Test.run testsuite
16 changes: 15 additions & 1 deletion exercises/practice/yacht/yacht.sml
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
fun score (dice: int list, category: string): int =
datatype category =
Ones
| Twos
| Threes
| Fours
| Fives
| Sixes
| FullHouse
| FourOfAKind
| LittleStraight
| BigStraight
| Choice
| Yacht

fun score (dice: int list, category): int =
raise Fail "'score' is not implemented"

0 comments on commit da398d6

Please sign in to comment.