Created
June 16, 2013 05:50
-
-
Save martindemello/5790922 to your computer and use it in GitHub Desktop.
simple crossword grid in eml
This file contains 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
import Dict | |
side = 30 | |
n = 15 | |
x0 = side * n | |
-- coordinates | |
toScreen (x, y) = (side * x - x0, side * (n - y)) | |
fromScreen (x, y) = (div (x + x0) side, n - div y side) | |
-- square | |
data Square = Black | White Char | |
sqColor s = case s of | |
Black -> black | |
White _ -> white | |
sqLetter s = case s of | |
Black -> '#' | |
White c -> c | |
renderCell square = layers | |
[ sqr <| sqColor square | |
, container side side middle <| plainText [ sqLetter square ] | |
] | |
-- grid | |
getSq grid x y = | |
case (Dict.lookup (x, y) grid) of | |
Just s -> s | |
Nothing -> White ' ' | |
sqr c = collage side side [ | |
square side |> filled c | |
, square side |> outlined (solid black) | |
] | |
crsr = opacity 0.5 <| collage side side [ square side |> filled green ] | |
sq grid x y = | |
let (px, py) = toScreen (x, y) | |
cell = getSq grid x y | |
in renderCell cell |> toForm |> move (px, py) | |
renderCursor state = | |
let (px, py) = toScreen state.cursor | |
in crsr |> toForm |> move (px, py) | |
renderGrid state = | |
concatMap (\x -> map (sq state.grid x) [1 .. n]) [1 .. n] | |
renderAll state = concat [renderGrid state, [renderCursor state]] | |
start = [((1, 1), Black), ((1, 3), White 'A')] | |
main = | |
let grid = Dict.fromList start | |
state = { grid = grid, cursor = (1, 1) } | |
in | |
collage 1200 1200 <| renderAll state |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment