-- | Functions for playing the game.
module Hs2048.Game
    ( addRandomTile
    , addRandomTiles
    , hasWon
    , isOver
    , new
    , randomEmptyIndex
    , randomEmptyPoint
    , randomTile
    ) where

import           Data.Maybe       (fromJust)
import qualified Hs2048.Board     as B
import qualified Hs2048.Direction as D
import qualified Hs2048.Point     as P
import qualified Hs2048.Settings  as S
import qualified Hs2048.Tile      as T
import qualified Hs2048.Vector    as V
import qualified System.Random    as R

{- |
    Adds a random tile to the board.

    >>> addRandomTile [[Nothing], [Nothing]] (R.mkStdGen 0)
    ([[Nothing],[Just 2]],1346387765 2103410263)
-}
addRandomTile :: R.RandomGen r => B.Board -> r -> (B.Board, r)
addRandomTile :: forall r. RandomGen r => Board -> r -> (Board, r)
addRandomTile Board
b r
r = case Maybe Point
p of
    Maybe Point
Nothing -> (Board
b, r
r)
    Maybe Point
_ -> (Board
b', r
r'')
  where
    b' :: Board
b' = Board -> Tile -> Point -> Board
B.set Board
b Tile
t (Maybe Point -> Point
forall a. HasCallStack => Maybe a -> a
fromJust Maybe Point
p)
    (Maybe Point
p, r
r') = Board -> r -> (Maybe Point, r)
forall r. RandomGen r => Board -> r -> (Maybe Point, r)
randomEmptyPoint Board
b r
r
    (Tile
t, r
r'') = r -> (Tile, r)
forall r. RandomGen r => r -> (Tile, r)
randomTile r
r'

{- |
    Adds some random tiles to the board.

    >>> addRandomTiles 2 [[Nothing], [Nothing]] (R.mkStdGen 0)
    ([[Just 2],[Just 2]],2127568003 1780294415)
-}
addRandomTiles :: R.RandomGen r => Int -> B.Board -> r -> (B.Board, r)
addRandomTiles :: forall r. RandomGen r => Int -> Board -> r -> (Board, r)
addRandomTiles Int
0 Board
b r
r = (Board
b, r
r)
addRandomTiles Int
n Board
b r
r = Int -> Board -> r -> (Board, r)
forall r. RandomGen r => Int -> Board -> r -> (Board, r)
addRandomTiles (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Board
b' r
r'
  where
    (Board
b', r
r') = Board -> r -> (Board, r)
forall r. RandomGen r => Board -> r -> (Board, r)
addRandomTile Board
b r
r

{- |
    Determines if the game has been won. See 'Hs2048.Settings.maxTile'.

    >>> hasWon [[Just 2048]]
    True
-}
hasWon :: B.Board -> Bool
hasWon :: Board -> Bool
hasWon = (Vector -> Bool) -> Board -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Tile -> Bool) -> Vector -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Bool -> (Int -> Bool) -> Tile -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
S.maxTile)))

{- |
    Determines if the game is over. The game is over if there are no available
    moves and no empty points.

    >>> isOver [[Just 2]]
    True
-}
isOver :: B.Board -> Bool
isOver :: Board -> Bool
isOver Board
b = Bool
cantMove Bool -> Bool -> Bool
&& Bool
haveNoEmptyPoints
  where
    cantMove :: Bool
cantMove = Bool -> Bool
not ((Direction -> Bool) -> [Direction] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Board -> Direction -> Bool
B.canMove Board
b) [Direction]
D.directions)
    haveNoEmptyPoints :: Bool
haveNoEmptyPoints = [Point] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Board -> [Point]
B.emptyPoints Board
b)

{- |
    Creates a new game by making an empty board and adding some random tiles to
    it. See 'Hs2048.Settings.width', 'Hs2048.Settings.height', and
    'Hs2048.Settings.tiles'.

    >>> new (R.mkStdGen 0)
    ([[Just 2,Nothing,Nothing,Nothing],[Nothing,Nothing,Nothing,Nothing],[Nothing,Nothing,Nothing,Nothing],[Nothing,Just 2,Nothing,Nothing]],2127568003 1780294415)
-}
new :: R.RandomGen r => r -> (B.Board, r)
new :: forall r. RandomGen r => r -> (Board, r)
new = Int -> Board -> r -> (Board, r)
forall r. RandomGen r => Int -> Board -> r -> (Board, r)
addRandomTiles Int
S.tiles (Int -> Int -> Board
B.empty Int
S.width Int
S.height)

{- |
    Selects an empty index at random from a vector.

    >>> randomEmptyIndex [Nothing, Nothing] (R.mkStdGen 0)
    (Just 1,40014 40692)
-}
randomEmptyIndex :: R.RandomGen r => V.Vector -> r -> (Maybe Int, r)
randomEmptyIndex :: forall r. RandomGen r => Vector -> r -> (Tile, r)
randomEmptyIndex Vector
v r
r = if [Int] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
is then (Tile
forall a. Maybe a
Nothing, r
r) else (Int -> Tile
forall a. a -> Maybe a
Just Int
i, r
r')
  where
    i :: Int
i = [Int]
is [Int] -> Int -> Int
forall a. HasCallStack => [a] -> Int -> a
!! Int
x
    (Int
x, r
r') = Point -> r -> (Int, r)
forall g. RandomGen g => Point -> g -> (Int, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
R.randomR (Int
0, [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
is Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) r
r
    is :: [Int]
is = Vector -> [Int]
V.emptyIndexes Vector
v

{- |
    Selects an empty point at random from a board.

    >>> randomEmptyPoint [[Nothing],[Nothing]] (R.mkStdGen 0)
    (Just (1,0),40014 40692)
-}
randomEmptyPoint :: R.RandomGen r => B.Board -> r -> (Maybe P.Point, r)
randomEmptyPoint :: forall r. RandomGen r => Board -> r -> (Maybe Point, r)
randomEmptyPoint Board
b r
r = if [Point] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Point]
ps then (Maybe Point
forall a. Maybe a
Nothing, r
r) else (Point -> Maybe Point
forall a. a -> Maybe a
Just Point
p, r
r')
  where
    p :: Point
p = [Point]
ps [Point] -> Int -> Point
forall a. HasCallStack => [a] -> Int -> a
!! Int
x
    (Int
x, r
r') = Point -> r -> (Int, r)
forall g. RandomGen g => Point -> g -> (Int, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
R.randomR (Int
0, [Point] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Point]
ps Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) r
r
    ps :: [Point]
ps = Board -> [Point]
B.emptyPoints Board
b

{- |
    Creates a random tile.

    >>> randomTile (R.mkStdGen 0)
    (Just 2,1601120196 1655838864)
-}
randomTile :: R.RandomGen r => r -> (T.Tile, r)
randomTile :: forall r. RandomGen r => r -> (Tile, r)
randomTile r
r = (Int -> Tile
forall a. a -> Maybe a
Just Int
n, r
r')
  where
    n :: Int
n = if (Float
x :: Float) Float -> Float -> Bool
forall a. Ord a => a -> a -> Bool
< Float
0.9 then Int
2 else Int
4
    (Float
x, r
r') = r -> (Float, r)
forall g. RandomGen g => g -> (Float, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
R.random r
r