Day2 use enum for shapes

This commit is contained in:
Jens Kadenbach
2022-12-02 16:45:03 +01:00
parent 71972e30ec
commit 5383662a81
2 changed files with 10 additions and 5 deletions

View File

@@ -12,7 +12,7 @@ module Day2.Shared (
) where ) where
data Shape = Rock | Paper | Scissors data Shape = Rock | Paper | Scissors
deriving (Show, Eq) deriving (Show, Eq, Enum)
newtype Score = Score Int newtype Score = Score Int
deriving (Show, Eq) deriving (Show, Eq)
@@ -36,9 +36,7 @@ newtype Game = Game (Shape, Shape)
deriving (Show, Eq) deriving (Show, Eq)
shapeScore :: Shape -> Score shapeScore :: Shape -> Score
shapeScore Rock = Score 1 shapeScore = Score . (+1) . fromEnum
shapeScore Paper = Score 2
shapeScore Scissors = Score 3
winAgainst :: Shape -> Shape winAgainst :: Shape -> Shape
winAgainst Rock = Paper winAgainst Rock = Paper

View File

@@ -12,6 +12,13 @@ spec :: Spec
spec = spec =
describe "Day2" $ do describe "Day2" $ do
describe "Day2 - Shared" $ do describe "Day2 - Shared" $ do
it "should implement winAgainst and loseAgainst for shapes" $ do
winAgainst Rock `shouldBe` Paper
winAgainst Paper `shouldBe` Scissors
winAgainst Scissors `shouldBe` Rock
loseAgainst Rock `shouldBe` Scissors
loseAgainst Paper `shouldBe` Rock
loseAgainst Scissors `shouldBe` Paper
it "should have scores for shapes" $ do it "should have scores for shapes" $ do
shapeScore Rock `shouldBe` Score 1 shapeScore Rock `shouldBe` Score 1
shapeScore Paper `shouldBe` Score 2 shapeScore Paper `shouldBe` Score 2