-- | Types and functions for manipulating boards.
module Hs2048.Board
    ( Board
    , canMove
    , canShift
    , empty
    , emptyPoints
    , move
    , parse
    , render
    , rotate
    , rotateFrom
    , rotateTo
    , score
    , set
    , shift
    ) where

import           Data.List        (transpose)
import qualified Hs2048.Direction as D
import qualified Hs2048.Point     as P
import qualified Hs2048.Tile      as T
import qualified Hs2048.Vector    as V

{- |
    Represents the game board. By convention, it is row-major.
-}
type Board = [V.Vector]

{- |
    Determines if the board can be moved in the given direction. See 'move'.

    >>> canMove [[Nothing, Just 2]] D.West
    True
-}
canMove :: Board -> D.Direction -> Bool
canMove :: Board -> Direction -> Bool
canMove Board
b Direction
d = Board -> Direction -> Board
move Board
b Direction
d Board -> Board -> Bool
forall a. Eq a => a -> a -> Bool
/= Board
b

{- |
    Determines if the board can be shifted. See 'shift'.

    >>> canShift [[Nothing, Just 2]]
    True
-}
canShift :: Board -> Bool
canShift :: Board -> Bool
canShift Board
b = Board -> Board
shift Board
b Board -> Board -> Bool
forall a. Eq a => a -> a -> Bool
/= Board
b

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

    >>> empty 2 1
    [[Nothing,Nothing]]
-}
empty :: Int -- ^ Width
    -> Int -- ^ Height
    -> Board
empty :: Int -> Int -> Board
empty = (Int -> [Tile] -> Board) -> [Tile] -> Int -> Board
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> [Tile] -> Board
forall a. Int -> a -> [a]
replicate ([Tile] -> Int -> Board) -> (Int -> [Tile]) -> Int -> Int -> Board
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Tile]
V.empty

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

    >>> emptyPoints [[Nothing, Just 2]]
    [(0,0)]
-}
emptyPoints :: Board -> [P.Point]
emptyPoints :: Board -> [Point]
emptyPoints Board
b = [Int] -> [[Int]] -> [(Int, [Int])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] (([Tile] -> [Int]) -> Board -> [[Int]]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Tile] -> [Int]
V.emptyIndexes Board
b) [(Int, [Int])] -> ((Int, [Int]) -> [Point]) -> [Point]
forall a b. [a] -> (a -> [b]) -> [b]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int, [Int]) -> [Point]
forall {f :: * -> *} {a} {a}. Functor f => (a, f a) -> f (a, a)
go
  where
    go :: (a, f a) -> f (a, a)
go (a
x, f a
ys) = (a -> (a, a)) -> f a -> f (a, a)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((,) a
x) f a
ys

{- |
    Moves the board in the given direction.

    >>> move [[Nothing, Just 2]] D.West
    [[Just 2,Nothing]]
-}
move :: Board -> D.Direction -> Board
move :: Board -> Direction -> Board
move Board
b Direction
d = Board -> Direction -> Board
rotateFrom (Board -> Board
shift (Board -> Direction -> Board
rotateTo Board
b Direction
d)) Direction
d

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

    >>> parse "- 2\n4 -\n"
    [[Nothing,Just 2],[Just 4,Nothing]]
-}
parse :: String -> Board
parse :: String -> Board
parse = (String -> [Tile]) -> [String] -> Board
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> [Tile]
V.parse ([String] -> Board) -> (String -> [String]) -> String -> Board
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines

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

    >>> render [[Nothing, Just 2], [Just 4, Nothing]]
    "- 2\n4 -\n"
-}
render :: Board -> String
render :: Board -> String
render = [String] -> String
unlines ([String] -> String) -> (Board -> [String]) -> Board -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Tile] -> String) -> Board -> [String]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Tile] -> String
V.render

{- |
    Rotate the board 90 degrees clockwise.

    >>> rotate [[Nothing, Just 2], [Just 4, Nothing]]
    [[Just 4,Nothing],[Nothing,Just 2]]
-}
rotate :: Board -> Board
rotate :: Board -> Board
rotate = ([Tile] -> [Tile]) -> Board -> Board
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Tile] -> [Tile]
forall a. [a] -> [a]
reverse (Board -> Board) -> (Board -> Board) -> Board -> Board
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Board -> Board
forall a. [[a]] -> [[a]]
transpose

{- |
    Rotates the board so that 'Hs2048.Direction.West' is at the left, assuming
    the given direction is currently at the left. This is the inverse of
    'rotateTo'.

    >>> rotateFrom [[Just 4, Nothing], [Nothing, Just 2]] D.South
    [[Nothing,Just 2],[Just 4,Nothing]]
-}
rotateFrom :: Board -> D.Direction -> Board
rotateFrom :: Board -> Direction -> Board
rotateFrom Board
b Direction
d = [Board] -> Board
forall a. HasCallStack => [a] -> a
head (Int -> [Board] -> [Board]
forall a. Int -> [a] -> [a]
drop Int
n [Board]
gs)
  where
    n :: Int
n = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Direction -> Int
forall a. Enum a => a -> Int
fromEnum (Direction
forall a. Bounded a => a
maxBound :: D.Direction) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Direction -> Int
forall a. Enum a => a -> Int
fromEnum Direction
d
    gs :: [Board]
gs = (Board -> Board) -> Board -> [Board]
forall a. (a -> a) -> a -> [a]
iterate Board -> Board
rotate Board
b

{- |
    Rotates the board so that the given direction is at the left. This is the
    inverse of 'rotateFrom'

    >>> rotateTo [[Nothing, Just 2], [Just 4, Nothing]] D.South
    [[Just 4,Nothing],[Nothing,Just 2]]
-}
rotateTo :: Board -> D.Direction -> Board
rotateTo :: Board -> Direction -> Board
rotateTo Board
b Direction
d = [Board] -> Board
forall a. HasCallStack => [a] -> a
head (Int -> [Board] -> [Board]
forall a. Int -> [a] -> [a]
drop Int
n [Board]
gs)
  where
    n :: Int
n = Direction -> Int
forall a. Enum a => a -> Int
fromEnum Direction
d
    gs :: [Board]
gs = (Board -> Board) -> Board -> [Board]
forall a. (a -> a) -> a -> [a]
iterate Board -> Board
rotate Board
b

{- |
    Calculates the score of a board.

    >>> score [[Nothing, Just 2], [Just 4, Just 8]]
    20
-}
score :: Board -> Int
score :: Board -> Int
score = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int] -> Int) -> (Board -> [Int]) -> Board -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Tile] -> Int) -> Board -> [Int]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Tile] -> Int
V.score

{- |
    Sets a tile at the given point in the board.

    >>> set [[Nothing, Just 2], [Just 4, Nothing]] (Just 8) (1, 1)
    [[Nothing,Just 2],[Just 4,Just 8]]
-}
set :: Board -> T.Tile -> P.Point -> Board
set :: Board -> Tile -> Point -> Board
set Board
b Tile
t Point
p = (Int -> [Tile] -> [Tile]) -> [Int] -> Board -> Board
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Int -> [Tile] -> [Tile]
go [Int
0 ..] Board
b
  where
    go :: Int -> [Tile] -> [Tile]
go Int
i [Tile]
v = if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Point -> Int
P.x Point
p then [Tile] -> Tile -> Int -> [Tile]
V.set [Tile]
v Tile
t (Point -> Int
P.y Point
p) else [Tile]
v

{- |
    Shifts a board toward the head. See 'Vector.shift'.

    >>> shift [[Nothing, Just 2], [Just 4, Nothing]]
    [[Just 2,Nothing],[Just 4,Nothing]]
-}
shift :: Board -> Board
shift :: Board -> Board
shift = ([Tile] -> [Tile]) -> Board -> Board
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Tile] -> [Tile]
V.shift