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
type Board = [V.Vector]
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
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
empty :: Int
-> Int
-> 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
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
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
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
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 :: 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
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
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
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
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
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