-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.hs
More file actions
71 lines (57 loc) · 1.61 KB
/
TicTacToe.hs
File metadata and controls
71 lines (57 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module TicTacToe where
import BoGL_1
{-
REQUIRED FUNCTIONS & PREDICATES
initial :: State
gameOver :: State -> Bool
outcome :: State -> Status
OPTIONAL (functions are predefined but can be overwritten)
input :: State -> String -> Position
output :: Content -> String
isValid :: Position -> State -> Bool
turn :: Position -> State -> State
-}
-- The basic Tic-Tac-Toe game
--
ticTacToe :: Game
ticTacToe = game {
initial = (board (3,3) Empty,A),
gameOver = \(b,_) -> threeInARow b || isFull b,
outcome = \(b,p) -> if threeAInARow b then Win A else
if threeBInARow b then Win B else
if isFull b then Tie else Turn p
}
-- Allow input shortcuts
--
ttt :: Game
ttt = ticTacToe {
input = \_ s -> if s `elem` (map (:[]) "LMR<c>lmr") then name2pos s
else read s
}
name2pos :: String -> Position
name2pos "L" = (3,1)
name2pos "M" = (3,2)
name2pos "R" = (3,3)
name2pos "<" = (2,1)
name2pos "c" = (2,2)
name2pos ">" = (2,3)
name2pos "l" = (1,1)
name2pos "m" = (1,2)
name2pos "r" = (1,3)
-- Change output to X and O (keep input shortcuts)
--
xo = ttt { output = showXO }
where showXO Empty ="."
showXO (Occupied A) = "X"
showXO (Occupied B) = "O"
-- A simple variation with 4 columns (keep input shortcuts)
--
ttt4c = ttt { initial = (board (3,4) Empty,A) }
-- Game-Specific Properties
--
threeAInARow :: BoardProperty
threeAInARow = inARow 3 A
threeBInARow :: BoardProperty
threeBInARow = inARow 3 B
threeInARow :: BoardProperty
threeInARow = threeAInARow ||| threeBInARow