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:
parent
7ddeff547b
commit
f41836c359
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1 @@
|
|||
Square_side = 100
|
|
@ -0,0 +1 @@
|
|||
Font_size = 0
|
|
@ -0,0 +1 @@
|
|||
Padding = 0
|
|
@ -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
|
|
@ -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
|
|
@ -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},
|
||||
},
|
||||
}
|
|
@ -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},
|
||||
},
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,8 @@
|
|||
Problem = {
|
||||
row_totals={0, 0},
|
||||
col_totals={0, 0},
|
||||
data={
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
},
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue