Guest User

Untitled

a guest
Mar 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import Data.List (nub)
  2.  
  3. data Move
  4. = Maxi Int
  5. | Mini Int
  6.  
  7. instance Show Move where
  8. show (Mini n) = "Mini " ++ (show n)
  9. show (Maxi n) = "Maxi " ++ (show n)
  10.  
  11. instance Eq Move where
  12. (==) (Mini n1) (Mini n2) = (n1 == n2)
  13. (==) (Maxi n1) (Maxi n2) = (n1 == n2)
  14. (==) _a _b = False
  15. (/=) (Maxi n1) (Mini n2) = (n1 /= n2)
  16. (/=) (Mini n1) (Maxi n2) = (n1 /= n2)
  17. (/=) _a _b = False
  18.  
  19. nextConfigs :: [Move] -> [[Move]]
  20. nextConfigs [] = fmap (\y -> [(Maxi y)]) ([1 .. 9] :: [Int])
  21. nextConfigs moves =
  22. case (last moves) of
  23. (Maxi n) -> [moves ++ [(Mini n)] | n <- [1 .. 9], (Mini n) `notElem` moves]
  24. (Mini n) -> [moves ++ [(Maxi n)] | n <- [1 .. 9], (Maxi n) `notElem` moves]
  25.  
  26. winsMaxi :: [Move] -> Bool
  27. winsMaxi moves =
  28. (all id [((Maxi x) `elem` moves) | x <- [1 .. 3]]) ||
  29. (all id [((Maxi x) `elem` moves) | x <- [4 .. 6]]) ||
  30. (all id [((Maxi x) `elem` moves) | x <- [7 .. 9]]) ||
  31. (all id [((Maxi x) `elem` moves) | x <- [1, 4, 7]]) ||
  32. (all id [((Maxi x) `elem` moves) | x <- [2, 5, 8]]) ||
  33. (all id [((Maxi x) `elem` moves) | x <- [3, 6, 9]])
  34.  
  35. winsMini :: [Move] -> Bool
  36. winsMini moves =
  37. (all id [((Mini x) `elem` moves) | x <- [1 .. 3]]) ||
  38. (all id [((Mini x) `elem` moves) | x <- [4 .. 6]]) ||
  39. (all id [((Mini x) `elem` moves) | x <- [7 .. 9]]) ||
  40. (all id [((Mini x) `elem` moves) | x <- [1, 4, 7]]) ||
  41. (all id [((Mini x) `elem` moves) | x <- [2, 5, 8]]) ||
  42. (all id [((Mini x) `elem` moves) | x <- [3, 6, 9]])
  43.  
  44. isTerminal :: [Move] -> Bool
  45. isTerminal moves = winsMaxi moves || winsMini moves || isDraw moves
  46.  
  47. isDraw :: [Move] -> Bool
  48. isDraw moves = length moves == 9 && not (winsMaxi moves) && not (winsMini moves)
  49.  
  50. main :: IO ()
  51. main = do
  52. print $ show $ nextConfigs [(Maxi 1), (Mini 5)]
Add Comment
Please, Sign In to add comment