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)/2
love.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 have
local c = Char.col_totals
local d = Char.data
Solution = {
col_totals = {c[1], c[2]},
data = {
{d[1][1], d[1][2] },
{ },
}
}
-- fill out the remaining data
Solution.data[2][1] = c[1] - d[1][1]
Solution.data[2][2] = c[2] - d[1][2]
d = Solution.data
Solution.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' then
generate_sum_grid()
solve()
elseif chord == '`' then
if Draw == 'problem' then
Draw = 'char'
elseif Draw == 'char' then
Draw = 'solution'
elseif Draw == 'solution' then
Draw = 'problem'
end
end
end
generate_sum_grid = function()
-- a sum grid is fully characterized by 4 numbers
Char = {
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.8
Padding = (Square_side-Font_size)/2
love.graphics.setFont(love.graphics.newFont(Font_size))
end
draw_sum_grid = function(g)
-- lines
love.graphics.setLineWidth(3)
local x1,y1 = 250,200
local x2,y2 = 450,400
love.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)/2
love.graphics.line(x1,ym, x2,ym)
love.graphics.line(xm,y1, xm,y2)
-- data
draw_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 do
for y=1,2 do
draw_cell(g.data[y][x], x1+(x-1)*Square_side, y1+(y-1)*Square_side)
end
end
end
on.draw = function()
if Draw == 'problem' then
draw_sum_grid(Problem)
elseif Draw == 'char' then
draw_sum_grid(Char)
else
draw_sum_grid(Solution)
end
end