-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminccweeper.lua
282 lines (247 loc) · 8.28 KB
/
minccweeper.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
--[[
+---=== minccweeper ===---+
| This is a 'recreation' |
| of minesweeper for the |
| Minecraft modification |
| computercraft. |
+-------------------------+
Made by Neui
]]
local currVer = 0 -- Version of this program
local w, h = term.getSize() -- Size of the screen
local running = true -- Program runs
local f = {} -- Functions
f.draw = {} -- Drawing functions
local v = {} -- Variables
v.state = 1 -- State 0 = startup, 1 = Main menu, 2 = Ingame
v.sel = { ["title"] = 1; ["customField"] = 0; }
v.titleSelection = {}
v.field = { ["width"] = 9; ["height"] = 9; ["mines"] = 10; }
v.time = 0
v.draw = {}
v.draw.fieldMoved = true
v.draw.redraw = true
local c = {} -- constants
c.name = "minCCweeper"
c.title = "~[ " .. c.name .. " ]~"
c.field = { ["maxwidth"] = 30; ["maxheight"] = 24; ["minmines"] = 10; ["maxmines"] = 668; }
c.field.draw = {}
c.field.draw.unknown = { ["char"] = '#'; ["fgmono"] = colors.white; ["bgadv"] = colors.gray; ["bgmono"] = colors.black; }
c.field.draw.nothing = { ["char"] = ' '; }
c.field.draw.mine = { ["char"] = 'O'; ["fgadv"] = colors.red; }
c.field.draw.mightMine = { ["char"] = 'P'; ["fgadv"] = colors.lightGray; }
c.field.draw.numbers = { ["fg"] = { colors.blue, colors.green, colors.red, colors.blue, colors.brown, colors.lightBlue, colors.gray, colors.lightGray }; }
c.field.presets = {} -- Also taken form windows minesweeper
c.field.presets.beginner = { ["name"] = "Beginner"; ["width"] = 9; ["height"] = 9; ["mines"] = 10; }
c.field.presets.advanced = { ["name"] = "Advanced"; ["width"] = 16; ["height"] = 16; ["mines"] = 40; }
c.field.presets.pro = { ["name"] = "Professional"; ["width"] = 30; ["height"] = 16; ["mines"] = 99; }
local playfield = {} -- Contains all of the bombs etc.
local playerfiled = {} -- What the players sees
local function csleep(sec)
local timer, p1 = os.startTimer(tonumber(sec or 0))
repeat
local _, p1 = os.pullEventRaw("timer")
until p1 == timer
end
-- Main Drawing Loop
function f.drawLoop()
while running do
if v.draw.redraw then
f.draw.setTBGColor(term, colors.white, colors.black)
term.clear()
end
if v.state == 1 or v.state == 2 then -- Main Selection
f.draw.title(term, w, h)
f.draw.titleSelection(term, w, h)
f.draw.fieldSizeAndWidth(term, w, h)
if v.state == 2 then
term.setCursorPos(w, h-1)
term.setCursorBlink(true)
else
end
elseif v.state == 3 then -- ingame
if v.draw.fieldMoved or v.draw.redraw then
f.draw.field(term, w, h, field)
end
f.draw.footer(term, w, h)
end
csleep(0)
end
end
function f.draw.field(term, w, h, field)
end
function f.draw.footer(term, w, h)
-- [FHxFW MI TIMEs]
f.draw.setBGColor(term, colors.black)
term.setCursorPos(1, h)
f.draw.setTextColor(term, colors.green)
term.write(tostring(v.field.width))
f.draw.setTextColor(term, colors.white)
term.write("x")
f.draw.setTextColor(term, colors.green)
term.write(tostring(v.field.height))
f.draw.setTextColor(term, colors.brown)
term.write(" " .. tostring(v.field.mines))
end
function f.draw.title(term, w, h)
term.setCursorPos((w/2)-(#c.title/2), h/#v.titleSelection)
term.write(c.title)
end
function f.draw.titleSelection(term, w, h)
for k, val in ipairs(v.titleSelection) do
term.setCursorPos((w/2)-((#val.name+2)/2), (h/2)-(#v.titleSelection/2)+k)
if k == v.sel.title then
f.draw.setTBGColor(term, colors.black, colors.lightGray, colors.white)
else
f.draw.setTBGColor(term, colors.white, colors.black)
end
term.write(" " .. val.name .. " ")
end
end
function f.draw.fieldSizeAndWidth(term, w, h)
term.setCursorPos(w/2-7, h-1)
f.draw.setTBGColor(term, colors.white, colors.black)
term.write(" x Mines")
term.setCursorPos(w/2-7, h-1)
f.draw.setTBGColor(term, colors.orange, colors.black, colors.white)
if v.field.width < 10 then
term.write("0")
end
term.write(tostring(v.field.width))
term.setCursorPos(w/2-4, h-1)
if v.field.height < 10 then
term.write("0")
end
term.write(tostring(v.field.height))
term.setCursorPos(w/2-1, h-1)
if v.field.mines < 100 then
term.write("0")
elseif v.field.mines < 10 then
term.write("00")
end
term.write(tostring(v.field.mines))
end
function f.draw.setTextColor(term, adv, mono)
local mono, adv = tonumber(mono) or tonumber(adv) or 1, tonumber(adv) or 1
if term.isColor and term.isColor() then
term.setTextColor(adv)
else
term.setTextColor(mono)
end
end
function f.draw.setBGColor(term, adv, mono)
local mono, adv = tonumber(mono) or tonumber(adv) or colors.black, tonumber(adv) or colors.black
if term.isColor and term.isColor() then
term.setBackgroundColor(adv)
else
term.setBackgroundColor(mono)
end
end
function f.draw.setTBGColor(term, advt, advbg, monot, monobg)
f.draw.setTextColor(term, advt, monot)
f.draw.setBGColor(term, advbg, monobg)
end
function f.getFromFieldPos(x, y, field)
return field[x*y]
end
function f.doSelection(sel, tsel, field)
if tsel[sel]["function"] == "exit" then
running = false
elseif tsel[sel]["function"] == "custom" then
else
v.state = 3
end
end
function f.inputLoop()
while running do
local ev, p1, p2, p3, p4, p5 = os.pullEventRaw()
if ev == "terminate" then
running = false
elseif ev == "term.resize" then
w, h = term.getSize()
elseif ev == "key" then
if v.state == 1 then -- Main menu
if p1 == 200 then -- Up Arrow
v.sel.title = ((v.sel.title - 2) % #v.titleSelection) + 1
elseif p1 == 208 then -- Down Arrow
v.sel.title = (v.sel.title % #v.titleSelection) + 1
elseif p1 == 28 then -- Enter
f.doSelection(v.sel.title, v.titleSelection, v.field)
end
if not v.titleSelection[v.sel.title]["function"] then
v.field.width = v.titleSelection[v.sel.title]["width"]
v.field.height = v.titleSelection[v.sel.title]["height"]
v.field.mines = v.titleSelection[v.sel.title]["mines"]
end
elseif v.mode == 2 then -- Custom selection
elseif v.mode == 3 then -- Ingame
end
elseif ev == "mouse_click" or ev == "mouse_drag" then
if v.state == 1 then -- Main menu
for k = 1, #v.titleSelection do
local hpos = math.floor((h/2)-(#v.titleSelection/2)+k)
if hpos == p3 then
if p1 == 1 and ev == "mouse_click" then
v.sel.title = k
os.queueEvent("key", 28) -- Enter, i am too lazy
else
if v.sel.title == k and ev == "mouse_click" then
os.queueEvent("key", 28) -- Enter
else
v.sel.title = k
end
end
break
end
end
elseif v.mode == 2 then -- Custom selection
elseif v.mode == 3 then -- Ingame
end
end
end
end
-- Main Program
local function main()
parallel.waitForAny(f.inputLoop, f.drawLoop)
end
-- Initalise
local function init()
local notice = false
f.draw.setTextColor(term, colors.white)
-- Setup the titlescreen selection
for k, val in pairs(c.field.presets) do
v.titleSelection[#v.titleSelection + 1] = val
end
v.titleSelection[#v.titleSelection + 1] = {["name"] = "Custom"; ["function"] = "custom";}
v.titleSelection[#v.titleSelection + 1] = {["name"] = "Exit"; ["function"] = "exit"; }
if notice and running then
write("\nPress a key to continue... (or wait 10s)")
local tim, ev, p1 = os.startTimer(10)
repeat
ev, p1 = os.pullEventRaw()
until (ev == "key") or (ev == "timer" and p1 == tim)
end
end
local est, emsg = pcall(init)
if not running or not est then -- error
if not est then
write(string.format("\nAn error happend during init:\n%s\n", tostring(emsg)))
else
write("\nProgram terminated.\n")
end
else
local _gsStart = os.clock()
local est, emsg = pcall(main)
term.setTextColor(1)
term.setBackgroundColor(32768)
term.clear()
term.setCursorPos(1,1)
if not est then
write(string.format("An error happend:\n%s\n", tostring(emsg)))
else
write("Program terminated.\n")
end
local _gs = math.floor(os.clock() - _gsStart + 0.5)
local _h, _m, _s = _gs / 3600, (_gs / 60) % 60, _gs % 60
write(string.format("Ran for %uh%um%us (%us)\n", _h, _m, _s, _gs))
end