Last active
July 15, 2017 12:10
-
-
Save AndrasKovacs/9597994 to your computer and use it in GitHub Desktop.
2048 game clone. Has identical mechanincs to "http://gabrielecirulli.github.io/2048/" as far as I can tell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
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. | |
-} | |
{- | |
NOTE: | |
If you want proper keyboard input and terminal display, play this patched version instead: | |
https://gist.github.com/gallais/db0b1a83f09c4512e47dd83fbf050c77 | |
-} | |
{-# LANGUAGE LambdaCase #-} | |
import Control.Applicative | |
import Control.Lens | |
import Control.Monad.State | |
import Data.List | |
import System.Random | |
import Text.Printf | |
import Data.Function | |
type Board = [[Int]] | |
movKeys = map (:[]) "hjkl" | |
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) | |
move :: String -> Board -> State Int Board | |
move "h" = mapM shift | |
move "l" = mapM ((reverse <$>) . shift . reverse) | |
move "j" = (transpose <$>) . move "l" . transpose | |
move "k" = (transpose <$>) . move "h" . transpose | |
move _ = error "wrong move key" | |
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 <- 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 <- getLine | |
maybe loop pure $ lookup key validMovs) | |
main = do | |
board <- foldr (=<<) (pure emptyBoard) | |
(replicate (initCells - 1) addCell) | |
putStrLn "*********************************************" | |
putStrLn "Keys:" | |
putStrLn " h : left" | |
putStrLn " j : down" | |
putStrLn " k : up" | |
putStrLn " l : right" | |
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