-- | Main entry point for the console interface to the game.
module Hs2048.Main
    ( direction
    , getChars
    , getMove
    , play
    ) where

import qualified Hs2048.Board     as B
import qualified Hs2048.Direction as D
import qualified Hs2048.Game      as G
import           Hs2048.Renderer  (renderGame)
import           System.IO        (BufferMode (NoBuffering), hSetBuffering,
                                   hSetEcho, stdin)
import qualified System.Random    as R

{- |
    Converts a string into a direction.

    >>> direction "\ESC[D"
    Just West
    >>> direction "<"
    Nothing
-}
direction :: String -> Maybe D.Direction
direction :: String -> Maybe Direction
direction String
"\ESC[D" = Direction -> Maybe Direction
forall a. a -> Maybe a
Just Direction
D.West
direction String
"\ESC[B" = Direction -> Maybe Direction
forall a. a -> Maybe a
Just Direction
D.South
direction String
"\ESC[C" = Direction -> Maybe Direction
forall a. a -> Maybe a
Just Direction
D.East
direction String
"\ESC[A" = Direction -> Maybe Direction
forall a. a -> Maybe a
Just Direction
D.North
direction String
_ = Maybe Direction
forall a. Maybe a
Nothing

{- |
    Gets up to three characters from standard input. If the input corresponds
    to an arrow key, it will be returned. Otherwise 'Nothing' will be returned.
    Will only consume enough input to determine if the input is an arrow key or
    not.
-}
getChars :: IO (Maybe String)
getChars :: IO (Maybe String)
getChars = do
    Char
a <- IO Char
getChar
    if Char
a Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\ESC' then Maybe String -> IO (Maybe String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe String
forall a. Maybe a
Nothing else do
        Char
b <- IO Char
getChar
        if Char
b Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'[' then Maybe String -> IO (Maybe String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe String
forall a. Maybe a
Nothing else do
            Char
c <- IO Char
getChar
            Maybe String -> IO (Maybe String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe String -> IO (Maybe String))
-> Maybe String -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$ if Char
c Char -> String -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
"ABCD"
                then String -> Maybe String
forall a. a -> Maybe a
Just [Char
a, Char
b, Char
c]
                else Maybe String
forall a. Maybe a
Nothing

{- |
    Reads from standard input and converts it into a direction. See 'getChars'.
-}
getMove :: IO (Maybe D.Direction)
getMove :: IO (Maybe Direction)
getMove = (Maybe String -> Maybe Direction)
-> IO (Maybe String) -> IO (Maybe Direction)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Direction
-> (String -> Maybe Direction) -> Maybe String -> Maybe Direction
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Maybe Direction
forall a. Maybe a
Nothing String -> Maybe Direction
direction) IO (Maybe String)
getChars

{- |
    Plays the game.
-}
play :: R.RandomGen r => (B.Board, r) -> IO ()
play :: forall r. RandomGen r => (Board, r) -> IO ()
play (Board
b, r
r) = do
    Handle -> BufferMode -> IO ()
hSetBuffering Handle
stdin BufferMode
NoBuffering
    Handle -> Bool -> IO ()
hSetEcho Handle
stdin Bool
False

    String -> IO ()
putStr (Board -> String
renderGame Board
b)

    if Board -> Bool
G.hasWon Board
b then String -> IO ()
putStrLn String
"You won!" else do
        if Board -> Bool
G.isOver Board
b then String -> IO ()
putStrLn String
"You lost." else do
            Maybe Direction
m <- IO (Maybe Direction)
getMove
            case Maybe Direction
m of
                Maybe Direction
Nothing -> String -> IO ()
putStrLn String
"Unknown move." IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Board, r) -> IO ()
forall r. RandomGen r => (Board, r) -> IO ()
play (Board
b, r
r)
                Just Direction
d -> if Board -> Direction -> Bool
B.canMove Board
b Direction
d
                    then String -> IO ()
putStrLn (Direction -> String
D.render Direction
d) IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Board, r) -> IO ()
forall r. RandomGen r => (Board, r) -> IO ()
play (Board -> r -> (Board, r)
forall r. RandomGen r => Board -> r -> (Board, r)
G.addRandomTile (Board -> Direction -> Board
B.move Board
b Direction
d) r
r)
                    else String -> IO ()
putStrLn String
"Invalid move." IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Board, r) -> IO ()
forall r. RandomGen r => (Board, r) -> IO ()
play (Board
b, r
r)