module Hs2048.Vector
( Vector
, canShift
, empty
, emptyIndexes
, parse
, render
, score
, set
, shift
) where
import Data.List (group)
import Data.Maybe (isJust, isNothing)
import Data.Monoid ((<>))
import qualified Hs2048.Tile as T
type Vector = [T.Tile]
canShift :: Vector -> Bool
canShift :: [Tile] -> Bool
canShift [Tile]
v = [Tile] -> [Tile]
shift [Tile]
v [Tile] -> [Tile] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Tile]
v
empty :: Int -> Vector
empty :: Int -> [Tile]
empty = (Int -> Tile -> [Tile]) -> Tile -> Int -> [Tile]
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> Tile -> [Tile]
forall a. Int -> a -> [a]
replicate Tile
T.empty
emptyIndexes :: Vector -> [Int]
emptyIndexes :: [Tile] -> [Int]
emptyIndexes = ((Int, Tile) -> Int) -> [(Int, Tile)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int, Tile) -> Int
forall a b. (a, b) -> a
fst ([(Int, Tile)] -> [Int])
-> ([Tile] -> [(Int, Tile)]) -> [Tile] -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Tile) -> Bool) -> [(Int, Tile)] -> [(Int, Tile)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Tile -> Bool
forall a. Maybe a -> Bool
isNothing (Tile -> Bool) -> ((Int, Tile) -> Tile) -> (Int, Tile) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, Tile) -> Tile
forall a b. (a, b) -> b
snd) ([(Int, Tile)] -> [(Int, Tile)])
-> ([Tile] -> [(Int, Tile)]) -> [Tile] -> [(Int, Tile)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> [Tile] -> [(Int, Tile)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..]
parse :: String -> Vector
parse :: String -> [Tile]
parse = (String -> Tile) -> [String] -> [Tile]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Tile
T.parse ([String] -> [Tile]) -> (String -> [String]) -> String -> [Tile]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words
render :: Vector -> String
render :: [Tile] -> String
render = [String] -> String
unwords ([String] -> String) -> ([Tile] -> [String]) -> [Tile] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tile -> String) -> [Tile] -> [String]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Tile -> String
T.render
score :: Vector -> Int
score :: [Tile] -> Int
score = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int] -> Int) -> ([Tile] -> [Int]) -> [Tile] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tile -> Int) -> [Tile] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Tile -> Int
T.score
set :: Vector -> T.Tile -> Int -> Vector
set :: [Tile] -> Tile -> Int -> [Tile]
set [Tile]
v Tile
t Int
i = (Int -> Tile -> Tile) -> [Int] -> [Tile] -> [Tile]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Int -> Tile -> Tile
go [Int
0 ..] [Tile]
v
where
go :: Int -> Tile -> Tile
go Int
i' Tile
t' = if Int
i' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i then Tile
t else Tile
t'
shift :: Vector -> Vector
shift :: [Tile] -> [Tile]
shift [Tile]
v = Int -> [Tile] -> [Tile]
forall a. Int -> [a] -> [a]
take Int
n ([Tile]
v' [Tile] -> [Tile] -> [Tile]
forall a. Semigroup a => a -> a -> a
<> Int -> [Tile]
empty Int
n)
where
n :: Int
n = [Tile] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Tile]
v
v' :: [Tile]
v' = [Tile] -> [[Tile]]
forall a. Eq a => [a] -> [[a]]
group ((Tile -> Bool) -> [Tile] -> [Tile]
forall a. (a -> Bool) -> [a] -> [a]
filter Tile -> Bool
forall a. Maybe a -> Bool
isJust [Tile]
v) [[Tile]] -> ([Tile] -> [Tile]) -> [Tile]
forall a b. [a] -> (a -> [b]) -> [b]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [Tile] -> [Tile]
forall {a}. Num a => [Maybe a] -> [Maybe a]
go
go :: [Maybe a] -> [Maybe a]
go (Just a
a : Just a
b : [Maybe a]
ts) = a -> Maybe a
forall a. a -> Maybe a
Just (a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a
b) Maybe a -> [Maybe a] -> [Maybe a]
forall a. a -> [a] -> [a]
: [Maybe a] -> [Maybe a]
go [Maybe a]
ts
go [Maybe a]
ts = [Maybe a]
ts