Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data Turn = Missed | Damaged | Destroyed | Oldmove deriving (Show,Eq)
- -----а
- getShipCells :: Ship -> [Cell]
- getShipCells (Ship lst) = map fst lst
- -------б
- goodPair :: Ship -> Ship -> Bool
- goodPair (Ship a) (Ship b) = all (\x -> (all (\q -> (distance (fst x) (fst q))> sqrt 2) b) ) a
- distance :: Cell -> Cell -> Double
- distance (Cell x1 y1) (Cell x2 y2) = sqrt (fromIntegral (x^2 + y^2)) where
- y = y2-y1
- x = fromEnum x2 - fromEnum x1
- --------в
- goodPosition :: [Ship] -> Bool
- goodPosition lst = (help lst 0 0 0 0) && all (\x -> all (\q -> (goodPair x q )) (delete x lst)) lst
- help :: [Ship] -> Int ->Int -> Int -> Int -> Bool
- help lst n1 n2 n3 n4 | lst==[] = (n1==4 && n2==3 && n3==2 && n4==1 )
- | otherwise = case length(getShipCells (head lst) ) of
- 1 -> help (tail lst) (n1+1) n2 n3 n4
- 2 -> help (tail lst) n1(n2+1) n3 n4
- 3 -> help (tail lst) n1 n2 (n3+1) n4
- 4 -> help (tail lst) n1 n2 n3 (n4+1)
- -------- г
- doTurn :: [Ship] -> Cell -> (Turn,[Ship])
- doTurn lst cell = foldl (\res1 x -> if (fst res1)== Missed then
- (let a = (help1 x cell) in ((fst a),((snd a ):(snd res1))) )
- else ((fst res1),x :(snd res1) ))
- (Missed,[]) lst
- help1 :: Ship -> Cell -> (Turn,Ship)
- help1 (Ship lst) cell = (a,Ship b) where
- (a,b) = foldl (\(a,b) x -> if a /= Missed then (a,x:b) else
- if cell==(fst x) then
- if died (Ship (delete x lst)) then
- (Destroyed,((fst x),0):b) else
- if (snd x)==0 then (Oldmove,((fst x),0):b) else
- (Damaged,((fst x),0):b) else (Missed,(((fst x),(snd x)):b))) (Missed,[]) lst
- died :: Ship -> Bool
- died (Ship lst)= foldl (\res a -> if (snd a)==0 then True else False ) True lst
- ---- д
- doTurns :: [Ship] -> [Cell] -> (Int,[Ship])
- doTurns lstS lstC = foldl (\(n,lst) c -> let i = (doTurn lst c) in
- 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