瀏覽代碼

Day2 use enum for shapes

main
Jens Kadenbach 2 年之前
父節點
當前提交
5383662a81
共有 2 個檔案被更改,包括 10 行新增5 行删除
  1. 2
    4
      src/Day2/Shared.hs
  2. 8
    1
      test/Day2Spec.hs

+ 2
- 4
src/Day2/Shared.hs 查看文件

12
 ) where
12
 ) where
13
 
13
 
14
 data Shape = Rock | Paper | Scissors
14
 data Shape = Rock | Paper | Scissors
15
-  deriving (Show, Eq)
15
+  deriving (Show, Eq, Enum)
16
 
16
 
17
 newtype Score = Score Int
17
 newtype Score = Score Int
18
   deriving (Show, Eq)
18
   deriving (Show, Eq)
36
   deriving (Show, Eq)
36
   deriving (Show, Eq)
37
 
37
 
38
 shapeScore :: Shape -> Score
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
 winAgainst :: Shape -> Shape
41
 winAgainst :: Shape -> Shape
44
 winAgainst Rock = Paper
42
 winAgainst Rock = Paper

+ 8
- 1
test/Day2Spec.hs 查看文件

5
 import Day2.Part1
5
 import Day2.Part1
6
 import Day2.Part2
6
 import Day2.Part2
7
 
7
 
8
-input :: String 
8
+input :: String
9
 input = "A Y\nB X\nC Z"
9
 input = "A Y\nB X\nC Z"
10
 
10
 
11
 spec :: Spec
11
 spec :: Spec
12
 spec =
12
 spec =
13
   describe "Day2" $ do
13
   describe "Day2" $ do
14
     describe "Day2 - Shared" $ do
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
       it "should have scores for shapes" $ do
22
       it "should have scores for shapes" $ do
16
         shapeScore Rock `shouldBe` Score 1
23
         shapeScore Rock `shouldBe` Score 1
17
         shapeScore Paper `shouldBe` Score 2
24
         shapeScore Paper `shouldBe` Score 2

Loading…
取消
儲存