This repository was archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstack.lua
161 lines (140 loc) · 3.63 KB
/
stack.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
--
-- Create a new Card object
--
Card = {}
function Card:new (card_data)
local self = {
_id = nil
}
-- add the card data to self
for k, v in pairs(card_data) do self[k] = v end
-- generate a uniq id, makes Stack:pick easier
local id = function()
if self._id == nil then
local r1 = math.random(1, 9999)
local r2 = math.random(1, 9999)
local r3 = math.random(1, 9999)
local r4 = math.random(1, 9999)
self._id = r1 .. '-' .. r2 .. '-' .. r3 .. '-' .. r4
end
return self._id
end
-- fetch attributes of this card by key
local get = function(n)
return self[n]
end
return {
id = id,
get = get
}
end
--
-- Create a new Stack object
--
Stack = {}
function Stack:new (card_data)
-- list of all the cards in this stack
local self = {
cards = {}
}
-- shuffle the stack N times
local shuffle = function(n)
local rand = math.random
local j
for i = 0, n do
for i = #self.cards, 2, -1 do
j = rand(i)
self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
end
end
end
-- deal the top card from this stack
local deal = function()
return table.remove(self.cards, 1)
end
-- draw N cards from the top of this stack
local draw = function(n)
local drawn_cards = {}
if n == nil then
return self.deal()
end
for i = 1, n do
local card = table.remove(self.cards, 1)
table.insert(drawn_cards, card)
end
return drawn_cards
end
-- remove a card from this stack by id
local pick = function(id)
for i, card in ipairs(self.cards) do
if card.id() == id then
return table.remove(self.cards[i])
end
end
end
-- add a card to the end of this stack
local add = function(card)
table.insert(self.cards, #self.cards + 1, card)
end
-- combine this stack with another
local combine = function(stack)
for i, card in ipairs(stack) do
table.insert(self.cards, #self.cards + 1, card)
end
stack = nil
end
-- how many cards are in this stack
local count = function()
return #self.cards
end
-- replace a card with another
local replace = function(i, card)
self.cards[i] = card
end
-- sort
local sort = function(f)
if f ~= nil then
table.sort(self.cards, f)
else
table.sort(self.cards)
end
end
-- cards getter
local cards = function(i)
if i ~= nil then
return self.cards[i]
end
return self.cards
end
return {
cards = cards,
shuffle = shuffle,
deal = deal,
draw = draw,
pick = pick,
add = add,
combine = combine,
count = count,
replace = replace,
sort = sort,
}
end
--
-- Initialize stack contents from a file
--
function Stack:load (path)
local file_handle, reason = io.open(path, 'r')
if file_handle then
-- read all contents of file into a string
local contents = file_handle:read('*a')
local deck_data = json.decode(contents)
local deck = Stack:new()
for i, card_data in ipairs(deck_data) do
card_object = Card:new(card_data)
deck.add(card_object)
end
return deck
else
native.showAlert('Debug', 'Reading deck file failed: ' .. reason)
end
end