-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdeck.lua
40 lines (35 loc) · 911 Bytes
/
deck.lua
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
Deck = class()
function Deck:ctor()
self.card_pile = {}
self.discard_pile = {}
end
local function shuffle(card_pile)
local len = #card_pile
while len > 0 do
local i = math.random(len)
local id = card_pile[i]
card_pile[i] = card_pile[len]
card_pile[len] = id
len = len - 1
end
end
function Deck:init()
for i = 1, resmng.card_finish_id - resmng.card_start_id + 1, 1 do
helper.insert(self.card_pile, i)
end
shuffle(self.card_pile)
end
function Deck:draw(n)
if #self.card_pile < n then
shuffle(self.discard_pile)
helper.insert(self.card_pile, self.discard_pile)
helper.clear(self.discard_pile)
end
local cards = {}
for _ = 1, n, 1 do
helper.insert(cards, self.card_pile[1])
table.remove(self.card_pile, 1)
end
return n == 1 and cards[1] or cards
end
return Deck