Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main :: IO()
- main = do
- putStrLn "Enter board configuration"
- -- board <- getLine
- let board = "1 0 2 1 0 1 1 2 2"
- let gameBoard = parseGameBoard board
- printBoard $ sliceBoard gameBoard
- print $ translateResult $ verifyGame gameBoard
- parseInt :: String -> Int
- parseInt s = read s :: Int
- parseGameBoard :: String -> [Int]
- parseGameBoard inputString = map parseInt $ words inputString
- translateResult :: Int -> String
- translateResult result
- | result == 0 = "game is not over"
- | result == 1 = "game is a tie"
- | result == 2 = "x won"
- | result == 3 = "O won"
- verifyGame :: [Int] -> Int
- verifyGame gameBoard = maximum $ map verifyLine $ map (translatePositions gameBoard) possibleLines
- verifyLine :: (Int, Int, Int) -> Int
- verifyLine (a, b, c)
- | a == 0 || b == 0 || c == 0 = 0
- | a == b && b == c = (a + 1)
- | otherwise = 1
- possibleLines :: [(Int, Int, Int)]
- possibleLines = [(0, 1, 2), (3, 4, 5), (6, 7, 8),(0, 3, 6), (1, 4, 7), (2, 5, 8), (1, 4, 8), (2, 4, 6)]
- listGet :: Ord a => [a] -> Int -> a
- listGet (x:xs) i
- | i == 0 = x
- | otherwise = listGet xs (i-1)
- mapTuple :: (a -> b) -> (a, a, a) -> (b, b, b)
- mapTuple f (a1, a2, a3) = (f a1, f a2, f a3)
- translatePositions :: [Int] -> (Int, Int, Int) -> (Int, Int, Int)
- translatePositions board pos = mapTuple (listGet board) pos
- boardGet :: Ord a => [a] -> Int -> Int -> a
- boardGet board i j = listGet board ((i*3) + (j))
- sliceBoard :: [Int] -> [(Int, Int, Int)]
- sliceBoard board = map (translatePositions board) $ take 3 possibleLines
- getTuppleString :: (Int, Int, Int) -> String
- getTuppleString (a,b,c) = show a ++ " " ++ show b ++ " " ++ show c ++ "\n"
- printBoard :: [(Int, Int, Int)] -> IO()
- printBoard board = putStr $ concatMap getTuppleString board
Add Comment
Please, Sign In to add comment