Monaris 0.1.1.1 → 0.1.2
raw patch · 2 files changed
+16/−16 lines, 2 files
Files
- Monaris.cabal +2/−2
- Monaris.hs +14/−14
Monaris.cabal view
@@ -1,7 +1,7 @@ name: Monaris -version: 0.1.1.1 +version: 0.1.2 synopsis: A simple tetris clone -description: A tetris clone written Haskell. +description: A tetris clone written in Haskell. homepage: https://github.com/fumieval/Monaris/ license: BSD3 license-file: LICENSE
Monaris.hs view
@@ -38,11 +38,11 @@ type Field = Array Coord (Maybe Color) -data Rotation = CCW | CW deriving (Show, Eq, Ord) +data Spin = CCW | CW deriving (Show, Eq, Ord) -rotate :: Rotation -> Coord -> Polyomino -> Polyomino -rotate CCW center = map $ (#/2) . (#+#center) . (\(x, y) -> (-y, x)) . (#-#center) . (2*#) -rotate CW center = map $ (#/2) . (#+#center) . (\(x, y) -> (y, -x)) . (#-#center) . (2*#) +spin :: Spin -> Coord -> Polyomino -> Polyomino +spin CCW center = map $ (#/2) . (#+#center) . (\(x, y) -> (-y, x)) . (#-#center) . (2*#) +spin CW center = map $ (#/2) . (#+#center) . (\(x, y) -> (y, -x)) . (#-#center) . (2*#) centers :: Polyomino -> [Coord] centers cs = cs' ++ [i | i@(c, r) <- map ((1,1)#+#) cs' @@ -68,12 +68,13 @@ getPolyomino :: Game (Polyomino, Color) getPolyomino = (polyominos!!) <$> randomness (0, length polyominos - 1) -rotationStrategy :: Polyomino -> Field -> [Polyomino] -> Polyomino -rotationStrategy original field = maximumBy (compare `on` ev) where +spinStrategy :: Polyomino -> Field -> [Polyomino] -> Polyomino +spinStrategy original field = maximumBy (compare `on` ev) where g xs = fromIntegral (sum (map snd xs)) / fromIntegral (length xs) - ev x = sum [fromEnum (g original <= g x) + sum [1 | c <- neighbors, isJust (field ! c)] ^ 2 + ev x = sum [fromEnum (g original <= g x) + + sum [1 | c <- neighbors, not (inRange (bounds field) c) || isJust (field ! c)] ^ 2 | r <- nub $ map snd x] - where neighbors = nub $ filter (inRange (bounds field)) $ (#+#) <$> x <*> [(0, 1), (0, -1), (1, 0), (1, 1)] + where neighbors = nub $ (#+#) <$> x <*> [(0, 1), (0, -1), (1, 0), (1, 1)] place :: (?picBlocks :: M.Map (Color, Int) Picture, ?blockSize :: Float, ?picBlockBackground :: Picture) => Polyomino -> Color -> Field -> Int -> Game (Maybe Field) @@ -108,8 +109,8 @@ (False, True) -> move (1, 0) _ -> return False b <- case (not z && z', not x && x') of - (True, False) -> rot CCW - (False, True) -> rot CW + (True, False) -> sp CCW + (False, True) -> sp CW _ -> return False return $ a || b @@ -139,10 +140,10 @@ then put omino >> return True else return False - rot dir = do omino <- get - case filter (isJust . putF) $ map (flip (rotate dir) omino) $ centers omino of + sp dir = do omino <- get + case filter (isJust . putF) $ map (flip (spin dir) omino) $ centers omino of [] -> return False - xs -> put (rotationStrategy omino field xs) >> return True + xs -> put (spinStrategy omino field xs) >> return True destination omino | isNothing $ putF omino' = omino @@ -277,4 +278,3 @@ f <- embedIO $ doesFileExist highscorePath score <- if f then embedIO $ read <$> readFile highscorePath else return 0 loop score -