In [1]:
:ext NoMonomorphismRestriction
import Diagrams.Prelude
import Data.Colour.SRGB
import Data.List.Split (chunksOf)
In [4]:
-- Generate a board of size `size`
board :: Int -> ((Int, Int) -> Diagram b R2) -> Diagram b R2
board size sq = vcat rows
  where rows = map hcat $ chunksOf size diagrams
        diagrams = map (sq . tuple) indices
        indices = sequence [[0..size-1], [0..size-1]]
        tuple [a, b] = (a, b)

-- Change visibility
visible 0 diagram = diagram # opacity 0
visible _ diagram = diagram

-- Board square
tile board (row, col) = visible value diagram
  where diagram = roundedRect size size 0.05 # fc color
                                             # pad padding
        padding = 1.15
        size = 1 / padding
        color = if value == 0
                then white
                else sRGB c c (c/3)
        c = (20.0 - value) / 20.0
        value = fromIntegral $ board !! row !! col        
       
-- Labels
labels board (row, col) = visible value $ number `atop` sq
  where number = text (show $ 2 ^ value) # fontSize 0.5
        value = board !! row !! col
        sq = square 1 # opacity 0
        
values = [[1, 2, 0], [0, 5, 6], [7, 0, 9]]

diagram $ board 3 (labels values) 
   `atop` board 3 (const $ square 1)
   `atop` board 3 (tile values)
In [ ]: