Undo feature

This commit is contained in:
Pyrex 2024-02-09 22:35:55 -08:00
parent 3508072c0e
commit 917f50370b
8 changed files with 262 additions and 2 deletions

View File

@ -4,6 +4,7 @@ function board:init(ruleset)
self.ruleset=ruleset self.ruleset=ruleset
self.cursor=cursor:new(self) self.cursor=cursor:new(self)
self.animator=animator:new() self.animator=animator:new()
self.checkpoint=nil
self.slots={} self.slots={}
self.wells={} self.wells={}
@ -67,12 +68,20 @@ function board:deal(seed)
end end
end end
function board:undo()
if (self.checkpoint) self.checkpoint:apply(self) print("applied")
self.checkpoint=nil
end
function board:on_idle() function board:on_idle()
self:find_automove() self:find_automove()
end end
function board:pre_move(card)
self.checkpoint=checkpoint:new(self,card)
end
function board:on_move() function board:on_move()
-- TODO: Make checkpoint
self:find_automove() self:find_automove()
end end
@ -142,6 +151,13 @@ function board:draw()
end end
end) end)
if self.checkpoint then
local cpl=self.ruleset.layouts:checkpoint()
local x,y=cpl:place_card(0)
self.ruleset.deck:draw_card(x,y,self.checkpoint.card,{shadowed=true})
print("",x+1,y+9,7)
end
self.animator:draw() self.animator:draw()
if self.animator:idle() then if self.animator:idle() then

39
checkpoint.lua Normal file
View File

@ -0,0 +1,39 @@
checkpoint=klass()
function checkpoint:init(board,card)
self.card=card
self.slots={}
self.wells={}
for s=1,#board.slots do
local cnt={}
for c in all(board.slots[s].contents) do
add(cnt,c)
end
self.slots[s]=cnt
end
for w=1,#board.wells do
local cnt={}
for c in all(board.wells[w].contents) do
add(cnt,c)
end
self.wells[w]=cnt
end
end
function checkpoint:apply(board)
for s=1,#board.slots do
while #board.slots[s].contents>0 do
deli(board.slots[s].contents)
end
for i in all(self.slots[s]) do
add(board.slots[s].contents,i)
end
end
for w=1,#board.wells do
while #board.wells[w].contents>0 do
deli(board.wells[w].contents)
end
for i in all(self.wells[w]) do
add(board.wells[w].contents,i)
end
end
end

View File

@ -37,6 +37,7 @@ function cursor:toggle_grab()
if acc==acceptance_state.not_grabbed then if acc==acceptance_state.not_grabbed then
if (self.board.slots[self:hover_slot()]:peek()) self.grabbed=self:hover_slot() if (self.board.slots[self:hover_slot()]:peek()) self.grabbed=self:hover_slot()
elseif acc==acceptance_state.would_accept then elseif acc==acceptance_state.would_accept then
self.board:pre_move(src:peek())
local card=src:pop() local card=src:pop()
tar:add(card) tar:add(card)
self.grabbed=nil self.grabbed=nil

View File

@ -14,6 +14,7 @@ function main:update()
if (btnp(2)) self.board.cursor:move_y(-1) if (btnp(2)) self.board.cursor:move_y(-1)
if (btnp(3)) self.board.cursor:move_y(1) if (btnp(3)) self.board.cursor:move_y(1)
if (btnp(4)) self.board.cursor:toggle_grab() if (btnp(4)) self.board.cursor:toggle_grab()
if (btnp(5)) self.board:undo()
end end
end end

View File

@ -5,6 +5,7 @@ __lua__
#include animator.lua #include animator.lua
#include board.lua #include board.lua
#include board_animations.lua #include board_animations.lua
#include checkpoint.lua
#include dealer.lua #include dealer.lua
#include cursor.lua #include cursor.lua
#include layout.lua #include layout.lua

View File

@ -1,5 +1,5 @@
progression={ progression={
ruleset:new(11,5,10,25), ruleset:new(11,4,13,22,"ff"),
-- level 1 -- level 1
ruleset:new(5,1,9,0), -- by test: always winnable ruleset:new(5,1,9,0), -- by test: always winnable
-- level 2 -- level 2

View File

@ -176,6 +176,11 @@ function ruleset:generate_layouts()
assert(false,"unknown well") assert(false,"unknown well")
end end
function layouts:checkpoint()
local wx=(ruleset.n_slots\2)*10
return layout:new(x+wx,1,layout_mode.obscured)
end
function layouts:slot(i) function layouts:slot(i)
if i<=ruleset.n_slots then if i<=ruleset.n_slots then
local sx=(i-1)*10 local sx=(i-1)*10

View File

@ -852,3 +852,200 @@
5002 5002
5046 5046
5015 5015
5072
5007
5108
4988
5101
4906
5090
4987
5012
5166
5098
5136
5125
5064
5134
5143
5195
5190
5197
5193
5078
5089
5208
5133
4941
5217
5142
5198
5227
5225
5251
5246
5187
5219
5139
5249
5157
5273
5077
4942
5267
5268
5213
5239
5252
5291
5308
5253
5306
5242
5296
5288
5344
5313
5088
5224
5356
5207
5338
5186
5343
5381
5151
5401
5405
5270
5416
5376
5427
5415
5438
5279
5479
5349
5302
5430
5330
5471
5503
5446
5283
5404
5495
5519
5363
5111
5400
5512
5541
5317
5546
5480
5502
5504
5399
5524
5547
5535
5567
5419
5469
5580
5599
5537
5575
5550
5568
5605
5615
5555
5498
5574
5463
5493
5315
5564
5663
5633
5677
5640
5656
5604
5511
5686
5702
5660
5697
5662
5626
5684
5560
5709
5680
5713
5669
5561
5741
5699
5733
5745
5752
5607
5732
5716
5721
5706
5764
5765
5584
5671
5761
5781
5750
5809
5717
5812
5825
5808
5842
5844
5848
5753
5687
5814
5879
5893
5890
5794
5846
5931
5802
5930
5876
5914
5932
5747
5948
5963
5970
5912
5720
5974
5990
5722
5818
5852
5647
5976
6025
6012
6032
6038
6050
6047
5954
6056
5964