Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Created January 28, 2019 16:08
Show Gist options
  • Save DarkSeraphim/41a93ba72085300b8680d41c2e88752e to your computer and use it in GitHub Desktop.
Save DarkSeraphim/41a93ba72085300b8680d41c2e88752e to your computer and use it in GitHub Desktop.
Partial implementation of snake logic in Haskell
move :: [(Int, Int)] -> Int -> Int -> [(Int, Int)]
move snake x y =
let (hx, hy) = head snake
nx = hx + x
ny = hy + y
n = (nx, ny)
in (n : (init snake), last snake)
isWall' :: Int -> Int -> (Int, Int) -> Bool
isWall w h (x, y)
| x == 0 || x == w = True
| y == 0 || y == h = True
| otherwise = False
drawCell' :: [(Int, Int)] -> (Int, Int) -> Int -> Int -> (Int, Int) -> ()
drawCell' snake food w h coord
| isWall w h coord = render coord '#'
| coord == (head snake) = render 'S'
| elem coord snake = render coord '|'
| coord == food = render coord '$'
| otherwise = render coord ' '
draw' :: [(Int, Int)] -> (Int, Int) -> Int -> Int -> ()
draw' snake food w h =
let x' = [0..w]
y' = [0..h]
a = [(x, y) | x <- x', y <- y']
in map_ drawCell' a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment