Created
February 20, 2013 07:43
-
-
Save jhickner/4993720 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Array | |
| import Data.Maybe | |
| data Piece = X | O | Empty deriving (Eq) | |
| instance Show Piece where | |
| show X = "X" | |
| show O = "O" | |
| show Empty = " " | |
| type Board = Array Integer Piece | |
| emptyBoard = listArray (0,8) $ replicate 9 Empty | |
| move i p board = board // [(i, p)] | |
| checkWin board = filter isJust . map (checkRow board) $ | |
| [ (0, 1, 2), (3, 4, 5), (6, 7, 8) | |
| , (0, 3, 6), (1, 4, 7), (2, 5, 8) | |
| , (0, 4, 8), (2, 4, 7) | |
| ] | |
| checkRow board (a,b,c) = | |
| case (board ! a, board ! b, board ! c) of | |
| (X,X,X) -> Just X | |
| (O,O,O) -> Just O | |
| _ -> Nothing | |
| printBoard :: Board -> IO () | |
| printBoard b = let p = show . (b!) in | |
| putStr . unlines . map unwords $ | |
| [ [p 0, "|", p 1, "|", p 2] | |
| , ["---------"] | |
| , [p 3, "|", p 4, "|", p 5] | |
| , ["---------"] | |
| , [p 6, "|", p 7, "|", p 8] | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment