-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathchess.lua
More file actions
67 lines (55 loc) · 1.66 KB
/
Copy pathchess.lua
File metadata and controls
67 lines (55 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
local _Chess = require("chess/src/chess")
local Chess = {}
Chess.__index = Chess
setmetatable(Chess, {
__index = function(_, key)
return _Chess[key]
end,
})
function Chess:new()
local instance = _Chess()
instance.human_player = {
[instance.WHITE] = true,
[instance.BLACK] = true,
}
instance.redo_stack = {}
instance.set_human = function(color, isHuman)
assert(color == instance.WHITE or color == instance.BLACK,
"Invalid color: " .. tostring(color))
instance.human_player[color] = isHuman
end
instance.is_human = function(color)
assert(color == instance.WHITE or color == instance.BLACK,
"Invalid color: " .. tostring(color))
return instance.human_player[color]
end
-- override undo: call base, push onto redo_stack
local _undo = instance.undo
instance.undo = function()
local move = _undo(instance) -- call the base‐class undo
if move then
table.insert(instance.redo_stack, move)
end
return move
end
-- redo: pop from redo_stack and re-apply
instance.redo = function()
local _move = table.remove(instance.redo_stack)
if _move then
instance.move(_move)
end
return _move
end
instance.redo_history = function()
return instance.redo_stack
end
-- override reset: clear redo stack, then call base
_reset = instance.reset
function Chess:reset()
instance.redo_stack = {}
_reset(self)
end
setmetatable(instance, Chess)
return instance
end
return Chess