Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Kopapir where
  2. --http://lambda.inf.elte.hu/fp/RPSGame.xml
  3. --Luksa Norbert
  4.  
  5. type Sign = Int
  6.  
  7. --error, show
  8. validSigns :: (Sign -> Sign -> a) -> Sign -> Sign -> a
  9. validSigns f a b
  10.   | a >= 0 && a < 3 && b >= 0 && b < 3 = f a b
  11. validSigns _ a b = error $ "validSigns: invalid values: (" ++ show a ++ "," ++ show b ++ ")"
  12.  
  13. --mintaillesztés, inline jelölés
  14. beats :: Sign -> Sign -> Bool
  15. 0 `beats` 2 = True
  16. 1 `beats` 0 = True
  17. 2 `beats` 1 = True
  18. a `beats` b = validSigns f a b
  19.    where f _ _ = False
  20.  
  21. isDraw :: Sign -> Sign -> Bool
  22. isDraw a b = validSigns f a b
  23.    where f x y = x == y
  24.  
  25. --őrfeltétel
  26. result :: Sign -> Sign -> Int
  27. result a b
  28.   | isDraw a b  = 0
  29.   | a `beats` b = 1
  30.   | otherwise   = -1
  31.  
  32. type Play = [Sign]
  33. type Rounds = (Play, Play)
  34.  
  35. --lambda, map, zip
  36. tournament :: Rounds -> Int
  37. tournament (xs, ys)
  38.  | l < 0     = 2
  39.  | l > 0     = 1
  40.  | otherwise = 0
  41.    where
  42.      l = sum $ map (\(x,y) -> result x y) $ zip xs ys
  43.    
  44. --listagenerátor
  45. partitionRounds :: Rounds -> ([Sign], [Sign], [Sign])
  46. partitionRounds (xs,ys) = ([ b | (a, b) <- r, a == 1], [ b | (a, b) <- r, a == 0], [ b | (a, b) <- r, a == -1])
  47.     where
  48.       r = map (\(x,y) -> (result x y, x)) $ zip xs ys
  49.  
  50. next :: Sign -> Sign
  51. next 0 = 1
  52. next 1 = 2
  53. next 2 = 0
  54.  
  55. --rekurzió
  56. frequency :: Eq a => a -> [a] -> Int
  57. frequency _ []   = 0
  58. frequency a (x:xs)
  59.   | x == a    = 1 + frequency a xs
  60.   | otherwise = frequency a xs
  61.  
  62. --nub
  63. mostFrequent :: Ord a => [a] {-nem üres-} -> a
  64. mostFrequent x = snd $ maximum $ map (\a -> (frequency a x, a)) $ nub x
  65.  
  66. type Strategy = Rounds -> Sign -- (Play, Play) -> Sign
  67.  
  68. strategy1 :: Strategy
  69. strategy1 ([],[])    = 0
  70. strategy1 (x:_, y:_)
  71.   | result x y == 1 = y
  72.   | otherwise       = next y
  73.  
  74.  
  75. strategy2 :: Strategy
  76. strategy2 ([],[]) = 2
  77. strategy2 (_, ys) = next $ mostFrequent ys
  78.  
  79. --tuple-ben mintaillesztés
  80. strategy3 :: Strategy
  81. strategy3 ([],[]) = 1
  82. strategy3 rounds
  83.  | length (won r) == length (lost r) = next $ mostFrequent (tie r)
  84.  | length (won r) >  length (lost r) = mostFrequent (won r)
  85.  | otherwise                         = next $ mostFrequent (lost r)  
  86.    where
  87.       r = partitionRounds rounds
  88.       won  (a,_,_) = a
  89.       tie  (_,b,_) = b
  90.       lost (_,_,c) = c
  91.  
  92. applyStrategies :: Strategy -> Strategy -> Rounds -> Rounds
  93. applyStrategies s1 s2 r@(xs,ys) = (s1 r : xs, s2 (ys, xs) : ys)
  94.  
  95. play :: Int -> Strategy -> Strategy -> Rounds
  96. play n s1 s2
  97.   | n <= 0    = ([], [])
  98.   | otherwise = applyStrategies s1 s2 (play (n-1) s1 s2)
  99.  
  100. winningStrategy :: Strategy -> [Strategy] -> [Int]
  101. winningStrategy = winningStrategyN 1
  102.   where
  103.    winningStrategyN :: Int ->  Strategy -> [Strategy] -> [Int]
  104.    winningStrategyN _ _ [] = []
  105.    winningStrategyN n s (x:xs)
  106.      | (tournament $ play 10 s x) == 2 = n : winningStrategyN (n+1) s xs
  107.      | otherwise                       = winningStrategyN (n+1) s xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement