1
+ module LangtonsAnt
2
+ open NUnit.Framework
3
+ open Xunit
4
+ open Xunit.Extensions
5
+ open FsUnit.Xunit
6
+ open NUnit
7
+
8
+
9
+ //At a white square, turn 90° right, flip the color of the square, move forward one unit
10
+ //At a black square, turn 90° left, flip the color of the square, move forward one unit
11
+
12
+ type Color =
13
+ | White
14
+ | Black
15
+
16
+ type Direction =
17
+ | North
18
+ | West
19
+ | East
20
+ | South
21
+
22
+ let Flip color =
23
+ match color with
24
+ | White -> Black
25
+ | Black -> White
26
+
27
+ let TurnLeft direction =
28
+ match direction with
29
+ | North -> West
30
+ | East -> North
31
+ | South -> East
32
+ | West -> South
33
+
34
+ let TurnRight direction =
35
+ direction |> TurnLeft |> TurnLeft |> TurnLeft
36
+
37
+ let MoveForwardDelta direction =
38
+ match direction with
39
+ | North -> ( 0 , 1 )
40
+ | East -> ( 1 , 0 )
41
+ | South -> ( 0 , - 1 )
42
+ | West -> (- 1 , 0 )
43
+
44
+ let MoveForward direction ( x , y ) =
45
+ let ( dx , dy ) = MoveForwardDelta direction
46
+ ( x + dx, y + dy)
47
+
48
+ let Turn color =
49
+ match color with
50
+ | White -> TurnRight
51
+ | Black -> TurnLeft
52
+
53
+ let Step ( color , direction , coordinate ) =
54
+ let newColor = Flip color
55
+ let newDirection = Turn color direction
56
+ let newCoordinate = MoveForward newDirection coordinate
57
+ ( newColor, newDirection, newCoordinate)
58
+
59
+ [<Fact>]
60
+ let ``Flips color`` () =
61
+ Flip White |> should equal Black
62
+ Flip Black |> should equal White
63
+
64
+ [<Fact>]
65
+ let ``Turns left`` () =
66
+ TurnLeft North |> should equal West
67
+ TurnLeft East |> should equal North
68
+ TurnLeft South |> should equal East
69
+ TurnLeft West |> should equal South
70
+
71
+ [<Fact>]
72
+ let ``Turns right`` () =
73
+ TurnRight North |> should equal East
74
+ TurnRight East |> should equal South
75
+ TurnRight South |> should equal West
76
+ TurnRight West |> should equal North
77
+
78
+ [<Fact>]
79
+ let ``moves forward`` () =
80
+ MoveForward East ( 1 , 1 ) |> should equal ( 2 , 1 )
81
+ MoveForward South ( 1 , 1 ) |> should equal ( 1 , 0 )
82
+ MoveForward West ( 1 , 1 ) |> should equal ( 0 , 1 )
83
+ MoveForward North ( 2 , 2 ) |> should equal ( 2 , 3 )
84
+
85
+ [<Fact>]
86
+ let ``Turns on color`` () =
87
+ Turn White North |> should equal East
88
+ Turn White East |> should equal South
89
+ Turn White South |> should equal West
90
+ Turn White West |> should equal North
91
+ Turn Black North |> should equal West
92
+ Turn Black East |> should equal North
93
+ Turn Black South |> should equal East
94
+ Turn Black West |> should equal South
95
+
96
+ [<Fact>]
97
+ let ``At a white square , turn 90 ° right , flip the color of the square , move forward one unit`` () =
98
+ Step( White, North, ( 1 , 1 )) |> should equal ( Black, East, ( 2 , 1 ))
99
+ Step( White, West, ( 1 , 1 )) |> should equal ( Black, North, ( 1 , 2 ))
100
+
101
+ [<Fact>]
102
+ let ``At a black square , turn 90 ° left , flip the color of the square , move forward one unit`` () =
103
+ Step( Black, North, ( 1 , 1 )) |> should equal ( White, West, ( 0 , 1 ))
0 commit comments