Advertisement
PonaFly

SEA BATTLE (BUGURT)

Nov 28th, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data Turn =     Missed | Damaged | Destroyed | Oldmove deriving (Show,Eq)
  2.  
  3. -----а
  4.  
  5.  
  6. getShipCells :: Ship -> [Cell]
  7. getShipCells (Ship lst) = map fst  lst
  8.  
  9.  
  10. -------б
  11.  
  12.  
  13.  
  14. goodPair :: Ship -> Ship -> Bool
  15. goodPair (Ship a) (Ship b) =  all (\x -> (all (\q -> (distance  (fst x) (fst q))> sqrt 2) b) ) a
  16.  
  17.  
  18. distance :: Cell -> Cell -> Double
  19. distance (Cell x1 y1) (Cell x2 y2) = sqrt (fromIntegral (x^2 + y^2)) where
  20.  y = y2-y1
  21.  x = fromEnum x2 - fromEnum x1
  22.  
  23.  
  24. --------в
  25.  
  26.  
  27. goodPosition :: [Ship] -> Bool
  28. goodPosition lst = (help lst 0 0 0 0) && all (\x ->  all (\q -> (goodPair x q )) (delete x lst)) lst
  29.  
  30. help :: [Ship] -> Int ->Int -> Int -> Int -> Bool
  31. help  lst n1 n2 n3 n4 | lst==[] = (n1==4 && n2==3 && n3==2 && n4==1 )
  32.                       | otherwise = case length(getShipCells (head lst)     ) of
  33.                        1 ->  help (tail lst) (n1+1) n2 n3 n4
  34.                        2 ->  help (tail lst) n1(n2+1) n3 n4
  35.                        3 ->  help (tail lst) n1 n2 (n3+1) n4
  36.                        4 ->  help (tail lst) n1 n2 n3 (n4+1)
  37.                        
  38.  
  39. -------- г
  40.  
  41. doTurn :: [Ship] -> Cell ->  (Turn,[Ship])                     
  42. doTurn lst cell = foldl (\res1 x -> if (fst res1)== Missed then
  43.  (let a = (help1 x cell) in ((fst a),((snd a ):(snd res1))) )
  44.   else ((fst res1),x :(snd res1) ))
  45.  (Missed,[]) lst
  46.  
  47.  
  48. help1 :: Ship -> Cell -> (Turn,Ship)
  49. help1 (Ship lst) cell = (a,Ship b) where
  50.     (a,b) = foldl (\(a,b) x -> if a /= Missed then (a,x:b) else
  51.         if cell==(fst x) then
  52.         if died (Ship (delete x lst))  then
  53.         (Destroyed,((fst x),0):b) else
  54.         if (snd x)==0 then (Oldmove,((fst x),0):b) else
  55.         (Damaged,((fst x),0):b) else (Missed,(((fst x),(snd x)):b))) (Missed,[]) lst
  56.  
  57.  died :: Ship -> Bool
  58. died (Ship lst)= foldl (\res a -> if (snd a)==0 then True else False ) True lst
  59.  
  60.  
  61. ---- д
  62. doTurns :: [Ship] -> [Cell] -> (Int,[Ship])
  63. doTurns lstS lstC = foldl (\(n,lst) c -> let i =  (doTurn lst c) in
  64.  if (fst i)== Missed then (n+1,lst) else (n,(snd i))) (1,lstS) lstC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement