Advertisement
elvecent

Lab5

Nov 21st, 2016
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2. import qualified Data.Map.Strict as Map
  3.  
  4. main = do
  5.        print $ shipCovering $ ship [(0,0,SoundCell),(1,0,SoundCell)]
  6.        print $ goodPosition $ map ship [[(0,0,SoundCell)],[(0,2,SoundCell)],[(0,4,SoundCell)],[(0,6,SoundCell)],
  7.                                         [(2,0,SoundCell),(3,0,SoundCell)],[(2,2,SoundCell),(3,2,SoundCell)],[(2,4,SoundCell),(3,4,SoundCell)],
  8.                                         [(5,0,SoundCell),(6,0,SoundCell),(7,0,SoundCell)],[(5,2,SoundCell),(6,2,SoundCell),(7,2,SoundCell)],
  9.                                         [(9,0,SoundCell),(9,1,SoundCell),(9,2,SoundCell),(9,3,SoundCell)]]
  10.        print $ vars $ (Inv (c :&& b)) :&& (a :|| b)
  11.        print $ tautology (Inv ((a :&& b) :&& ((Inv a) :|| (Inv b))))
  12.        where a = Var "a"
  13.              b = Var "b"
  14.              c = Var "c"
  15.  
  16. data Cell = Cell Int Int deriving (Show,Eq)
  17. data CellCondition = DeadCell | HarmedCell | SoundCell deriving (Show,Eq)
  18. newtype Ship = Ship [(Cell,CellCondition)] deriving (Show,Eq)
  19. data TurnResult = Miss | Harmed | Dead | Already deriving (Show,Eq)
  20.  
  21. ship :: [(Int,Int,CellCondition)] -> Ship
  22. ship = Ship . map (\(a,b,c) -> (Cell a b, c))
  23.  
  24. unship :: Ship -> [(Cell,CellCondition)]
  25. unship (Ship xs) = xs
  26.  
  27. cell :: (Int,Int) -> Cell
  28. cell = uncurry Cell
  29.  
  30. uncell :: Cell -> (Int,Int)
  31. uncell (Cell a b) = (a,b)
  32.  
  33. hasCell :: Ship -> Cell -> Bool
  34. hasCell s c = c `elem` (getShipCells s)
  35.  
  36. goodPos :: (Int,Int) -> Bool
  37. goodPos (x,y) = x>=0 && x<=9 && y>=0 && y<=9
  38.  
  39. goodShipPos :: Ship -> Bool
  40. goodShipPos = (all (goodPos . uncell)) . getShipCells
  41.  
  42. getShipCells :: Ship -> [Cell]
  43. getShipCells (Ship xs) = map fst xs
  44.  
  45. covering :: Cell -> [Cell]
  46. covering (Cell a b) = map cell $ filter goodPos xs
  47.                       where xs = [(a-1,b-1),
  48.                                   (a-1,b),
  49.                                   (a-1,b+1),
  50.                                   (a,b-1),
  51.                                   (a,b),
  52.                                   (a,b+1),
  53.                                   (a+1,b-1),
  54.                                   (a+1,b),
  55.                                   (a+1,b+1)]
  56.                  
  57. shipCovering :: Ship -> [Cell]                  
  58. shipCovering = nub . (concatMap covering) . getShipCells
  59.  
  60. shipDegree :: Ship -> Int
  61. shipDegree = length . getShipCells
  62.  
  63. checkDegrees :: [Ship] -> Bool
  64. checkDegrees ss = all (\n -> 5-n == degreeCount n) [1..4]
  65.                   where degreeCount n = length $ filter (\s -> shipDegree s == n) ss
  66.  
  67. remove :: (Eq a) => a -> [a] -> [a]
  68. remove _ [] = []
  69. remove x (y:ys) = if x == y then ys else y : (remove x ys)
  70.  
  71. goodPair :: Ship -> Ship -> Bool
  72. goodPair a b = null $ intersect (shipCovering a) (getShipCells b)
  73.  
  74. goodPosition :: [Ship] -> Bool
  75. goodPosition ss = all goodShipPos ss && (checkDegrees ss) && (and $ [goodPair x y | x <- ss, y <- ss, x /= y])
  76.  
  77. doTurn :: [Ship] -> Cell -> (TurnResult,[Ship])
  78. doTurn ss c = case s of
  79.               Nothing -> (Miss,ss)
  80.               (Just oldShip) -> helper oldShip
  81.               where s = find (flip hasCell c) ss
  82.                     helper oldShip = (res,newShips)
  83.                                      where (Just (cc,cond)) = find (\(a,_) -> a == c) (unship oldShip)
  84.                                            newShips = replace oldShip newShip ss
  85.                                            newShip = Ship $ replace (cc,cond) (cc,newCond) (unship oldShip)
  86.                                            newCond = case cond of
  87.                                                      SoundCell -> HarmedCell
  88.                                                      HarmedCell -> DeadCell
  89.                                                      DeadCell -> DeadCell
  90.                                            res = case cond of
  91.                                                      SoundCell -> Harmed
  92.                                                      HarmedCell -> Dead
  93.                                                      DeadCell -> Already
  94.                                            replace y x xs = y : (remove x xs)
  95.                                                      
  96.  
  97. data Prop = Var String | (:&&) Prop Prop | (:||) Prop Prop | Inv Prop
  98.  
  99. vars :: Prop -> [String]
  100. vars (Var s) = [s]
  101. vars (p1 :&& p2) = nub $ vars p1 ++ vars p2
  102. vars (p1 :|| p2) = nub $ vars p1 ++ vars p2
  103. vars (Inv p) = vars p
  104.  
  105. truthValue :: Prop -> [(String,Bool)] -> Bool
  106. truthValue p a = evaluate p (Map.fromList a)
  107.  
  108. evaluate :: Prop -> (Map.Map String Bool) -> Bool
  109. evaluate (Var s) a = a Map.! s
  110. evaluate (p1 :&& p2) a = (evaluate p1 a) && (evaluate p2 a)
  111. evaluate (p1 :|| p2) a = (evaluate p1 a) || (evaluate p2 a)
  112. evaluate (Inv p) a = not $ evaluate p a
  113.  
  114. arrange2 :: a -> a -> Int -> [[a]]
  115. arrange2 x y n = foldl (\acc _ -> map (x :) acc ++ (map (y :) acc)) [[]] [1..n]
  116.  
  117. tautology :: Prop -> Bool
  118. tautology p = all (\a -> truthValue p a) assignments
  119.               where vs = vars p
  120.                     vsl = length vs
  121.                     assignments = map (\bs -> zip vs bs) $ arrange2 True False vsl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement