Skip to content

Instantly share code, notes, and snippets.

@gallais
Created August 22, 2016 12:24
Show Gist options
  • Save gallais/db0b1a83f09c4512e47dd83fbf050c77 to your computer and use it in GitHub Desktop.
Save gallais/db0b1a83f09c4512e47dd83fbf050c77 to your computer and use it in GitHub Desktop.
Patched version of András Kovács' Mini2048 to use the arrow keys, turn off buffering and clear the screen regularly
-- Slight variant of https://gist.github.com/AndrasKovacs/9597994
{-
Copyright (C) 2014 András Kovács
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-}
{-# LANGUAGE LambdaCase #-}
import Control.Applicative
import Control.Lens
import Control.Monad.State
import Data.List
import System.Random
import Text.Printf
import Data.Function
import System.IO
import System.Console.ANSI
type Board = [[Int]]
movKeys = ["\ESC[D", "\ESC[A", "\ESC[C", "\ESC[B"]
tableSize = (4, 4)
goal = 2048
initCells = 3
emptyBoard = replicate (fst tableSize) $ replicate (snd tableSize) 0
newCells = 4: replicate 9 2
shift :: [Int] -> State Int [Int]
shift = fmap (take $ snd tableSize) . go . filter (/=0) where
go (a:b:xs) | a == b = id += a*2 >> go (a + b: xs)
go (a:b:xs) = (a:) <$> go (b:xs)
go xs = pure $ xs ++ repeat 0
zeros :: Traversal' Board Int
zeros = each . each . filtered (==0)
data Direction = West | North | East | South
deriving Eq
toDirection :: String -> Direction
toDirection str = case str of
"\ESC[D" -> West
"\ESC[A" -> North
"\ESC[C" -> East
"\ESC[B" -> South
_ -> error "wrong move key"
move :: Direction -> Board -> State Int Board
move d = case d of
West -> mapM shift
East -> mapM ((reverse <$>) . shift . reverse)
South -> (transpose <$>) . move East . transpose
North -> (transpose <$>) . move West . transpose
showBoard :: Board -> String
showBoard = unlines . (map $ (=<<) (\case 0 -> " |"; n -> printf "%4d|" n))
addCell :: Board -> IO Board
addCell table = do
pos <- randomRIO (0, lengthOf zeros table - 1)
cell <- (newCells!!) <$> randomRIO (0, length newCells - 1)
pure $ table & elementOf zeros pos .~ cell
game :: Board -> Int -> IO ()
game board score = do
board <- addCell board
let validMovs = [(key, res) |
key <- toDirection <$> movKeys,
let res = runState (move key board) score,
fst res /= board]
if (any . any) (==goal) board then do
putStr $ showBoard board
printf "You won! Score: %d\n" score
else if null validMovs && nullOf zeros board then do
putStr $ showBoard board
printf "Game over. Score is %d\n" score
else do
uncurry game =<< (fix $ \loop -> do
printf "Score: %d\n" score
putStr $ showBoard board
key <- toDirection <$> replicateM 3 getChar
initScreen
maybe loop pure $ lookup key validMovs)
initScreen :: IO ()
initScreen = do
clearScreen
setCursorPosition 0 0
main = do
hSetBuffering stdin NoBuffering
initScreen
board <- foldr (=<<) (pure emptyBoard)
(replicate (initCells - 1) addCell)
putStrLn "*********************************************"
putStrLn "Keys: use the arrow keys"
putStrLn ""
printf "Try to get a %d by joining identical tiles.\n" goal
putStrLn ""
game board 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment