new fork: sum-grid problems

Currently generates valid problems (though sums can go over 10).
Will eventually also provide UI for manually solving and scoring them.
This commit is contained in:
Kartik K. Agaram 2023-11-16 00:13:07 -08:00
parent 7ddeff547b
commit f41836c359
15 changed files with 123 additions and 2 deletions

9
0010-on.draw Normal file
View File

@ -0,0 +1,9 @@
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

23
0011-draw_sum_grid Normal file
View File

@ -0,0 +1,23 @@
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

5
0012-on.initialize Normal file
View File

@ -0,0 +1,5 @@
on.initialize = function()
Font_size = Square_side*0.8
Padding = (Square_side-Font_size)/2
love.graphics.setFont(love.graphics.newFont(Font_size))
end

1
0013-Square_side Normal file
View File

@ -0,0 +1 @@
Square_side = 100

1
0014-Font_size Normal file
View File

@ -0,0 +1 @@
Font_size = 0

1
0015-Padding Normal file
View File

@ -0,0 +1 @@
Padding = 0

14
0016-generate_sum_grid Normal file
View File

@ -0,0 +1,14 @@
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

14
0017-on.keychord_press Normal file
View File

@ -0,0 +1,14 @@
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

9
0018-Char Normal file
View File

@ -0,0 +1,9 @@
-- 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},
},
}

9
0019-Solution Normal file
View File

@ -0,0 +1,9 @@
-- solution for the sum-grid problem defined by Char.
Solution = {
row_totals={0, 0},
col_totals={0, 0},
data={
{0, 0},
{0, 0},
},
}

21
0020-solve Normal file
View File

@ -0,0 +1,21 @@
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

1
0021-Draw Normal file
View File

@ -0,0 +1 @@
Draw = 'char'

8
0022-Problem Normal file
View File

@ -0,0 +1,8 @@
Problem = {
row_totals={0, 0},
col_totals={0, 0},
data={
{0, 0},
{0, 0},
},
}

6
0023-draw_cell Normal file
View File

@ -0,0 +1,6 @@
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

View File

@ -62,9 +62,8 @@ function App.initialize(arg)
-- TODO: app-specific stuff goes here
-- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
love.window.setTitle('TODO')
love.window.setTitle('Sum Grid')