Quellcode durchsuchen

Day2 use enum for shapes

main
Jens Kadenbach vor 1 Jahr
Ursprung
Commit
5383662a81
2 geänderte Dateien mit 10 neuen und 5 gelöschten Zeilen
  1. 2
    4
      src/Day2/Shared.hs
  2. 8
    1
      test/Day2Spec.hs

+ 2
- 4
src/Day2/Shared.hs Datei anzeigen

@@ -12,7 +12,7 @@ module Day2.Shared (
12 12
 ) where
13 13
 
14 14
 data Shape = Rock | Paper | Scissors
15
-  deriving (Show, Eq)
15
+  deriving (Show, Eq, Enum)
16 16
 
17 17
 newtype Score = Score Int
18 18
   deriving (Show, Eq)
@@ -36,9 +36,7 @@ newtype Game = Game (Shape, Shape)
36 36
   deriving (Show, Eq)
37 37
 
38 38
 shapeScore :: Shape -> Score
39
-shapeScore Rock = Score 1
40
-shapeScore Paper = Score 2
41
-shapeScore Scissors = Score 3
39
+shapeScore = Score . (+1) . fromEnum
42 40
 
43 41
 winAgainst :: Shape -> Shape
44 42
 winAgainst Rock = Paper

+ 8
- 1
test/Day2Spec.hs Datei anzeigen

@@ -5,13 +5,20 @@ import Day2.Shared
5 5
 import Day2.Part1
6 6
 import Day2.Part2
7 7
 
8
-input :: String 
8
+input :: String
9 9
 input = "A Y\nB X\nC Z"
10 10
 
11 11
 spec :: Spec
12 12
 spec =
13 13
   describe "Day2" $ do
14 14
     describe "Day2 - Shared" $ do
15
+      it "should implement winAgainst and loseAgainst for shapes" $ do
16
+        winAgainst Rock `shouldBe` Paper
17
+        winAgainst Paper `shouldBe` Scissors
18
+        winAgainst Scissors `shouldBe` Rock
19
+        loseAgainst Rock `shouldBe` Scissors
20
+        loseAgainst Paper `shouldBe` Rock
21
+        loseAgainst Scissors `shouldBe` Paper
15 22
       it "should have scores for shapes" $ do
16 23
         shapeScore Rock `shouldBe` Score 1
17 24
         shapeScore Paper `shouldBe` Score 2

Laden…
Abbrechen
Speichern