-- | Types and functions for manipulating vectors.
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

{- |
    Represents a row or column on the game board. By convention, a vector has
    4 tiles.
-}
type Vector = [T.Tile]

{- |
    Determines if the vector can be shifted.

    >>> canShift [Nothing, Just 2, Nothing, Nothing]
    True
-}
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

{- |
    Returns an empty vector of the given size.

    >>> empty 4
    [Nothing,Nothing,Nothing,Nothing]
-}
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

{- |
    Returns the indexes that don't contain tiles.

    >>> emptyIndexes [Nothing, Just 2, Nothing, Nothing]
    [0,2,3]
-}
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 ..]

{- |
    Parses a string as a vector. This is the inverse of 'render'.

    >>> parse "- 2 - -"
    [Nothing,Just 2,Nothing,Nothing]
-}
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

{- |
    Renders a vector as a string. This is the inverse of 'parse'.

    >>> render [Nothing, Just 2, Nothing, Nothing]
    "- 2 - -"
-}
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

{- |
    Calculates the score of a vector.

    >>> score [Nothing, Just 2, Just 4, Just 8]
    20
-}
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

{- |
    Sets a tile at the given index in the vector.

    >>> set [Nothing, Nothing, Nothing, Nothing] (Just 2) 1
    [Nothing,Just 2,Nothing,Nothing]
-}
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'

{- |
    Shifts a vector toward the head. The output vector will be the same size as
    the input vector, padded with @Nothing@.

    >>> shift [Nothing, Just 2, Nothing, Nothing]
    [Just 2,Nothing,Nothing,Nothing]

    Like tiles will be combined.

    >>> shift [Just 2, Nothing, Just 2, Just 2]
    [Just 4,Just 2,Nothing,Nothing]

    Any number of tiles can be combined in one shift.

    >>> shift [Just 2, Just 2, Just 4, Just 4]
    [Just 4,Just 8,Nothing,Nothing]
-}
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