Currently generates valid problems (though sums can go over 10). Will eventually also provide UI for manually solving and scoring them.
2CQ7FSHGSODNHID5N6S3G26NS54HWMXPO4OEQKJXTHJSGK7DXPCQC EQSFHYF37CRGNDTH2BZD25K7YPJUTTY4FQVUBULE2PBZ45C4TQTQC M7UODV5HDRZKAS3BKGLNU5Z5HM4KZDM4QIB2S3E6S7NAWGJY3MJAC OTIBCAUJ3KDQJLVDN3A536DLZGNRYMGJLORZVR3WLCGXGO6UGO6AC YJ6ASFBGEATFRCO2OF2AA4EPOBLWXTIV6YZMHALW3QVR7XTOSJPAC 5OVKHVY6TJK53NCEUGSFBMHDENBJ25IEZNBWCI6QRCRLRKG5K7NAC R5QXEHUIZLELJGGCZAE7ATNS3CLRJ7JFRENMGH4XXH24C5WABZDQC draw_cell = function(n, x,y)local s = tostring(n)local w = App.width(s)local px = (Square_side-w)/2love.graphics.print(s, x+px, y+Padding)end
Problem = {row_totals={0, 0},col_totals={0, 0},data={{0, 0},{0, 0},},}
Draw = 'char'
solve = function()print('solve')-- copy the slots we havelocal c = Char.col_totalslocal d = Char.dataSolution = {col_totals = {c[1], c[2]},data = {{d[1][1], d[1][2] },{ },}}-- fill out the remaining dataSolution.data[2][1] = c[1] - d[1][1]Solution.data[2][2] = c[2] - d[1][2]d = Solution.dataSolution.row_totals = {d[1][1] + d[1][2],d[2][1] + d[2][2],}end
-- solution for the sum-grid problem defined by Char.Solution = {row_totals={0, 0},col_totals={0, 0},data={{0, 0},{0, 0},},}
-- a set of numbers that uniquely characterizes a sum-grid instance.Char = {row_totals={0, 0},col_totals={0, 0},data={{0, 0},{0, 0},},}
on.keychord_press = function(chord, key)if chord == 'C-n' thengenerate_sum_grid()solve()elseif chord == '`' thenif Draw == 'problem' thenDraw = 'char'elseif Draw == 'char' thenDraw = 'solution'elseif Draw == 'solution' thenDraw = 'problem'endendend
generate_sum_grid = function()-- a sum grid is fully characterized by 4 numbersChar = {data = {{ math.random(1,9), math.random(1,9) },{ '', ''},},row_totals = { '', '' },}Char.col_totals = {math.random(Char.data[1][1]+1,10),math.random(Char.data[1][2]+1,10)}end
Padding = 0
Font_size = 0
Square_side = 100
on.initialize = function()Font_size = Square_side*0.8Padding = (Square_side-Font_size)/2love.graphics.setFont(love.graphics.newFont(Font_size))end
draw_sum_grid = function(g)-- lineslove.graphics.setLineWidth(3)local x1,y1 = 250,200local x2,y2 = 450,400love.graphics.line(x1,y1, x2,y1)love.graphics.line(x1,y2, x2,y2)love.graphics.line(x1,y1, x1,y2)love.graphics.line(x2,y1, x2,y2)local xm,ym = (x1+x2)/2, (y1+y2)/2love.graphics.line(x1,ym, x2,ym)love.graphics.line(xm,y1, xm,y2)-- datadraw_cell(g.col_totals[1], x1,y1-Square_side)draw_cell(g.col_totals[2], xm,y1-Square_side)draw_cell(g.row_totals[1], x1-Square_side, y1)draw_cell(g.row_totals[2], x1-Square_side, ym)for x=1,2 dofor y=1,2 dodraw_cell(g.data[y][x], x1+(x-1)*Square_side, y1+(y-1)*Square_side)endendend
on.draw = function()if Draw == 'problem' thendraw_sum_grid(Problem)elseif Draw == 'char' thendraw_sum_grid(Char)elsedraw_sum_grid(Solution)endend