Skip to content

Instantly share code, notes, and snippets.

@kspalaiologos
Last active August 31, 2023 22:05
Show Gist options
  • Save kspalaiologos/6b0eae6c02a8d13a15734be3bb5e86f6 to your computer and use it in GitHub Desktop.
Save kspalaiologos/6b0eae6c02a8d13a15734be3bb5e86f6 to your computer and use it in GitHub Desktop.
funniest 2048 on the planet
import Control.Monad
import System.Random
import Control.Monad.Trans.State
import Control.Lens hiding(under)
import Control.Conditional hiding(unless, when)
import Data.List
import Data.Functor
import Data.Random.Lift
import Control.Monad.IO.Class
import Control.Monad.Fix
import Data.List.Split
data Direction = L | U | R | D deriving(Eq, Show)
addTile=liftM2(>>=)(ap((>>=).state.randomR.(0,).subtract 1.length)((return.).(!!)).ap(map.flip divMod.length)(elemIndices 0.join))((`ap`snd).(.fst).((((state(randomR(1::Int, 10))>>=).(.flip(flip if' 4.(1==))2)).).).(((return.).).).(.(((.~).).lensCond)).(.).(.).(&))where lensCond x y = element x.element y
moveLeft=traverse%~(ap(++)(flip replicate 0.(-)4.length).map sum.(chunksOf 2<=<group).filter(0/=))
main=(fix(flip((.).(>>=).ap((>>).(>>putStrLn"Move?").mapM_(putStrLn.unwords.map show))(fix(liftM2(>>=)((`fmap`(getChar<&>flip lookup(zip"wasd"[U,L,D,R]))).ap maybe(((ap(flip if' moveLeft.(L ==))(ap(flip if'(under transpose moveLeft).(U ==))(flip(flip if'(under(map reverse)moveLeft).(R ==))(under transpose(under(map reverse)moveLeft))))<$>id)<*>).return)).flip flip return.(ap.).ap(flip.(if'.).(==))))).((ap(flip if'(putStrLn"You win!").any(elem 2048)).ap(flip if'(putStrLn "You lose!").all(notElem 0))).).join.(flip.).(.flip(evalState.addTile)).(.))=<<evalState(addTile=<<addTile(replicate 4(replicate 4 0))))=<<newStdGen where under=liftM2(.)(.)(flip(.))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment