tictactoe

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Data.Array
import Data.Maybe

data Piece = Naught | Cross deriving (Show, Eq)
data Result = Win | Lose | Draw deriving (Show, Eq)
type Board = Array Integer (Maybe Piece)

emptyBoard :: Board
emptyBoard = listArray (0,8) $ replicate 9 Nothing

allPossibleMoves :: Board -> [Board]
allPossibleMoves b = foldl tryMove [] [0..8]
	where
		tryMove ms i = if b ! i == Nothing then
				ms ++ [(b // [(i, (Just Cross))])]
			else ms

invertBoard :: Board -> Board
invertBoard b = fmap switchPiece b
	where
		switchPiece (Just Naught) = (Just Cross)
		switchPiece (Just Cross) = (Just Naught)
		switchPiece x = x

invertResult :: (Maybe Result) -> (Maybe Result)
invertResult (Just Win) = (Just Lose)
invertResult (Just Lose) = (Just Win)
invertResult r = r

evaluateBoard :: Board -> (Maybe Result)
evaluateBoard b 
	| isWin b = (Just Win)
	| isWin $ invertBoard b = (Just Lose)
	| isFull b = (Just Draw)
	| otherwise = Nothing
	where
		isWin b = if 
				((b!0) == (Just Cross) && 
				(b!1) == (Just Cross) && 
				(b!2) == (Just Cross)) || -- Top Horizontal
				((b!3) == (Just Cross) && 
				(b!4) == (Just Cross) && 
				(b!5) == (Just Cross)) || -- Middle Horizontal
				((b!6) == (Just Cross) && 
				(b!7) == (Just Cross) && 
				(b!8) == (Just Cross)) || -- Bottom Horizontal
				((b!0) == (Just Cross) && 
				(b!3) == (Just Cross) && 
				(b!6) == (Just Cross)) || -- Left Vertical
				((b!1) == (Just Cross) && 
				(b!4) == (Just Cross) && 
				(b!7) == (Just Cross)) || -- Middle Vertical
				((b!2) == (Just Cross) && 
				(b!5) == (Just Cross) && 
				(b!8) == (Just Cross)) || -- Right Vertical
				((b!0) == (Just Cross) && 
				(b!4) == (Just Cross) && 
				(b!8) == (Just Cross)) || -- Diagonal Top Left, Bottom Right
				((b!2) == (Just Cross) && 
				(b!4) == (Just Cross) && 
				(b!6) == (Just Cross)) -- Diagonal Top Right, Bottom Left
			then True 
			else False
		isFull b = not $ Nothing `elem` (elems b)

-- Assumes Cross to move first
walkPaths :: Bool -> Board -> [Result]
walkPaths inv b = if result /= Nothing then [fromJust result]
		else concat $ map (walkPaths (not inv)) (allPossibleMoves $ invertBoard b)
	where 
		result = if inv then invertResult evald else evald
		evald = evaluateBoard b 

bestMove :: Board -> Board
bestMove b = b

drawBoard :: Board -> String
drawBoard b = " a b c\n" ++ 
		(drawLine 0) ++ " -----\n" ++
		(drawLine 1) ++ " -----\n" ++
		(drawLine 2)
	where
		drawLine y = (show $ y + 1) ++ 
			(drawPiece $ b ! (y*3+0)) ++ "|" ++
			(drawPiece $ b ! (y*3+1)) ++ "|" ++
			(drawPiece $ b ! (y*3+2)) ++ "\n"
		drawPiece (Just Naught) = "O"
		drawPiece (Just Cross) = "X"
		drawPiece _ = " "

main = putStrLn $ "Wins: " ++ 
	(show $ length $ filter (==Win) (walkPaths False emptyBoard))
15:39: Warning: Redundant bracket
Found:
[(b // [(i, (Just Cross))])]
Why not:
[b // [(i, (Just Cross))]]
15:47: Warning: Redundant bracket
Found:
(i, (Just Cross))
Why not:
(i, Just Cross)
21:45: Warning: Redundant bracket
Found:
(Just Cross)
Why not:
Just Cross
22:44: Warning: Redundant bracket
Found:
(Just Naught)
Why not:
Just Naught
25:17: Warning: Redundant bracket
Found:
(Maybe Result) -> (Maybe Result)
Why not:
Maybe Result -> (Maybe Result)
25:17: Warning: Redundant bracket
Found:
(Maybe Result) -> (Maybe Result)
Why not:
(Maybe Result) -> Maybe Result
26:27: Warning: Redundant bracket
Found:
(Just Lose)
Why not:
Just Lose
27:28: Warning: Redundant bracket
Found:
(Just Win)
Why not:
Just Win
30:18: Warning: Redundant bracket
Found:
Board -> (Maybe Result)
Why not:
Board -> Maybe Result
32:21: Warning: Redundant bracket
Found:
(Just Win)
Why not:
Just Win
33:35: Warning: Redundant bracket
Found:
(Just Lose)
Why not:
Just Lose
34:22: Warning: Redundant bracket
Found:
(Just Draw)
Why not:
Just Draw
85:25: Warning: Redundant $
Found:
(drawPiece $ b ! (y * 3 + 1)) ++
"|" ++ (drawPiece $ b ! (y * 3 + 2)) ++ "\n"
Why not:
drawPiece (b ! (y * 3 + 1)) ++
"|" ++ (drawPiece $ b ! (y * 3 + 2)) ++ "\n"
86:25: Warning: Redundant $
Found:
(drawPiece $ b ! (y * 3 + 2)) ++ "\n"
Why not:
drawPiece (b ! (y * 3 + 2)) ++ "\n"
37:27: Error: Redundant if
Found:
if
((b ! 0) == (Just Cross) &&
(b ! 1) == (Just Cross) && (b ! 2) == (Just Cross))
||
((b ! 3) == (Just Cross) &&
(b ! 4) == (Just Cross) && (b ! 5) == (Just Cross))
||
((b ! 6) == (Just Cross) &&
(b ! 7) == (Just Cross) && (b ! 8) == (Just Cross))
||
((b ! 0) == (Just Cross) &&
(b ! 3) == (Just Cross) && (b ! 6) == (Just Cross))
||
((b ! 1) == (Just Cross) &&
(b ! 4) == (Just Cross) && (b ! 7) == (Just Cross))
||
((b ! 2) == (Just Cross) &&
(b ! 5) == (Just Cross) && (b ! 8) == (Just Cross))
||
((b ! 0) == (Just Cross) &&
(b ! 4) == (Just Cross) && (b ! 8) == (Just Cross))
||
((b ! 2) == (Just Cross) &&
(b ! 4) == (Just Cross) && (b ! 6) == (Just Cross))
then True else False
Why not:
((b ! 0) == (Just Cross) &&
(b ! 1) == (Just Cross) && (b ! 2) == (Just Cross))
||
((b ! 3) == (Just Cross) &&
(b ! 4) == (Just Cross) && (b ! 5) == (Just Cross))
||
((b ! 6) == (Just Cross) &&
(b ! 7) == (Just Cross) && (b ! 8) == (Just Cross))
||
((b ! 0) == (Just Cross) &&
(b ! 3) == (Just Cross) && (b ! 6) == (Just Cross))
||
((b ! 1) == (Just Cross) &&
(b ! 4) == (Just Cross) && (b ! 7) == (Just Cross))
||
((b ! 2) == (Just Cross) &&
(b ! 5) == (Just Cross) && (b ! 8) == (Just Cross))
||
((b ! 0) == (Just Cross) &&
(b ! 4) == (Just Cross) && (b ! 8) == (Just Cross))
||
((b ! 2) == (Just Cross) &&
(b ! 4) == (Just Cross) && (b ! 6) == (Just Cross))
38:34: Warning: Redundant bracket
Found:
(b ! 0) == (Just Cross)
Why not:
(b ! 0) == Just Cross
39:33: Warning: Redundant bracket
Found:
(b ! 1) == (Just Cross)
Why not:
(b ! 1) == Just Cross
40:33: Warning: Redundant bracket
Found:
(b ! 2) == (Just Cross)
Why not:
(b ! 2) == Just Cross
41:34: Warning: Redundant bracket
Found:
(b ! 3) == (Just Cross)
Why not:
(b ! 3) == Just Cross
42:33: Warning: Redundant bracket
Found:
(b ! 4) == (Just Cross)
Why not:
(b ! 4) == Just Cross
43:33: Warning: Redundant bracket
Found:
(b ! 5) == (Just Cross)
Why not:
(b ! 5) == Just Cross
44:34: Warning: Redundant bracket
Found:
(b ! 6) == (Just Cross)
Why not:
(b ! 6) == Just Cross
45:33: Warning: Redundant bracket
Found:
(b ! 7) == (Just Cross)
Why not:
(b ! 7) == Just Cross
46:33: Warning: Redundant bracket
Found:
(b ! 8) == (Just Cross)
Why not:
(b ! 8) == Just Cross
47:34: Warning: Redundant bracket
Found:
(b ! 0) == (Just Cross)
Why not:
(b ! 0) == Just Cross
48:33: Warning: Redundant bracket
Found:
(b ! 3) == (Just Cross)
Why not:
(b ! 3) == Just Cross
49:33: Warning: Redundant bracket
Found:
(b ! 6) == (Just Cross)
Why not:
(b ! 6) == Just Cross
50:34: Warning: Redundant bracket
Found:
(b ! 1) == (Just Cross)
Why not:
(b ! 1) == Just Cross
51:33: Warning: Redundant bracket
Found:
(b ! 4) == (Just Cross)
Why not:
(b ! 4) == Just Cross
52:33: Warning: Redundant bracket
Found:
(b ! 7) == (Just Cross)
Why not:
(b ! 7) == Just Cross
53:34: Warning: Redundant bracket
Found:
(b ! 2) == (Just Cross)
Why not:
(b ! 2) == Just Cross
54:33: Warning: Redundant bracket
Found:
(b ! 5) == (Just Cross)
Why not:
(b ! 5) == Just Cross
55:33: Warning: Redundant bracket
Found:
(b ! 8) == (Just Cross)
Why not:
(b ! 8) == Just Cross
56:34: Warning: Redundant bracket
Found:
(b ! 0) == (Just Cross)
Why not:
(b ! 0) == Just Cross
57:33: Warning: Redundant bracket
Found:
(b ! 4) == (Just Cross)
Why not:
(b ! 4) == Just Cross
58:33: Warning: Redundant bracket
Found:
(b ! 8) == (Just Cross)
Why not:
(b ! 8) == Just Cross
59:34: Warning: Redundant bracket
Found:
(b ! 2) == (Just Cross)
Why not:
(b ! 2) == Just Cross
60:33: Warning: Redundant bracket
Found:
(b ! 4) == (Just Cross)
Why not:
(b ! 4) == Just Cross
61:33: Warning: Redundant bracket
Found:
(b ! 6) == (Just Cross)
Why not:
(b ! 6) == Just Cross
64:28: Error: Use notElem
Found:
not $ Nothing `elem` (elems b)
Why not:
notElem Nothing (elems b)
64:34: Warning: Redundant bracket
Found:
Nothing `elem` (elems b)
Why not:
Nothing `elem` elems b
69:22: Error: Use concatMap
Found:
concat $
map (walkPaths (not inv)) (allPossibleMoves $ invertBoard b)
Why not:
concatMap (walkPaths (not inv)) (allPossibleMoves $ invertBoard b)
79:17: Warning: Redundant bracket
Found:
(drawLine 0) ++
" -----\n" ++ (drawLine 1) ++ " -----\n" ++ (drawLine 2)
Why not:
drawLine 0 ++
" -----\n" ++ (drawLine 1) ++ " -----\n" ++ (drawLine 2)
80:17: Warning: Redundant bracket
Found:
(drawLine 1) ++ " -----\n" ++ (drawLine 2)
Why not:
drawLine 1 ++ " -----\n" ++ (drawLine 2)
80:33: Warning: Redundant bracket
Found:
" -----\n" ++ (drawLine 2)
Why not:
" -----\n" ++ drawLine 2
83:30: Warning: Redundant $
Found:
(show $ y + 1) ++
(drawPiece $ b ! (y * 3 + 0)) ++
"|" ++
(drawPiece $ b ! (y * 3 + 1)) ++
"|" ++ (drawPiece $ b ! (y * 3 + 2)) ++ "\n"
Why not:
show (y + 1) ++
(drawPiece $ b ! (y * 3 + 0)) ++
"|" ++
(drawPiece $ b ! (y * 3 + 1)) ++
"|" ++ (drawPiece $ b ! (y * 3 + 2)) ++ "\n"
84:25: Warning: Redundant $
Found:
(drawPiece $ b ! (y * 3 + 0)) ++
"|" ++
(drawPiece $ b ! (y * 3 + 1)) ++
"|" ++ (drawPiece $ b ! (y * 3 + 2)) ++ "\n"
Why not:
drawPiece (b ! (y * 3 + 0)) ++
"|" ++
(drawPiece $ b ! (y * 3 + 1)) ++
"|" ++ (drawPiece $ b ! (y * 3 + 2)) ++ "\n"
91:19: Warning: Redundant $
Found:
"Wins: " ++
(show $ length $ filter (== Win) (walkPaths False emptyBoard))
Why not:
"Wins: " ++
show (length $ filter (== Win) (walkPaths False emptyBoard))