-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer.othello.lua
More file actions
446 lines (383 loc) · 8.06 KB
/
computer.othello.lua
File metadata and controls
446 lines (383 loc) · 8.06 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
value = {...}
side = value[1]
-------------------------------------------------------------------------------
-- Function --
-------------------------------------------------------------------------------
--COMMON
function terminate()
os.queueEvent("terminate")
end
function termCls()
term.clear()
term.setCursorPos(1,1)
end
function monCls()
mon.clear()
mon.setCursorPos(1,1)
end
function cls()
monCls()
end
function acls()
monCls()
termCls()
end
function range(num, max, min)
if num>max then
return min
elseif num<min then
return max
else
return num
end
end
function oppos(bin)
if bin==1 then
return 0
elseif bin==0 then
return 1
else
return nil
end
end
-------------------------------------------------------------------------------
TURN_CURRENT = math.random(2)-1
TURN_OPPOS = oppos(TURN_CURRENT)
NONE = -1
WHITE = 0
BLACK = 1
PASSFLAG = 0
board = {}
direction = {}
mon = peripheral.wrap(side)
-------------------------------------------------------------------------------
--BOARD SETUP
function boardReset()
for x=1,8 do
board[x] = {}
for y=1,8 do
board[x][y] = NONE
end
end
--board[4][4] = WHITE
--board[5][4] = BLACK
--board[4][5] = BLACK
--board[5][5] = WHITE
board[4][8] = BLACK
board[4][7] = BLACK
board[4][6] = WHITE
board[4][5] = BLACK
end
function boardCount()
local amountWhite, amountBlack = 0, 0
for x=1,8 do
for y=1,8 do
if board[x][y] == WHITE then
amountWhite = amountWhite+1
elseif board[x][y] == BLACK then
amountBlack = amountBlack+1
end
end
end
return amountWhite, amountBlack
end
function setDirectionTable()
for i=1,8 do
direction[i]={}
for j=1,2 do
direction[i][j] = nil
end
end
--UP
direction[1][1] = 0
direction[1][2] = -1
--UP RIGHT
direction[2][1] = 1
direction[2][2] = -1
--RIGHT
direction[3][1] = 1
direction[3][2] = 0
--DOWN RIGHT
direction[4][1] = 1
direction[4][2] = 1
--DOWN
direction[5][1] = 0
direction[5][2] = 1
--DOWN LEFT
direction[6][1] = -1
direction[6][2] = 1
--LEFT
direction[7][1] = -1
direction[7][2] = 0
--UP LEFT
direction[8][1] = -1
direction[8][2] = -1
end
function canPlace(_x, _y, _turn)
local oppos = oppos(_turn)
setDirectionTable()
local fl=0
for i=1, 8 do
vx, vy = _x + direction[i][1], _y + direction[i][2]
if vx>0 and vx<9 and vy>0 and vy<9 then
if board[vx][vy] == oppos then
while 1 do
vx, vy = vx + direction[i][1], vy + direction[i][2]
if vx<1 or vx>8 or vy<1 or vy>8 then
break
end
if board[vx][vy]==_turn then
fl=1
elseif board[vx][vy]==NONE then
break
end
end
end
end
end
if _x>0 and _x<9 and _y>0 and _y<9 then
if board[_x][_y]~=NONE then
fl=0
end
else
fl=0
end
if fl==1 then
return true
else
return false
end
end
function boardPlace(_x, _y, _turn)
local oppos = oppos(_turn)
setDirectionTable()
if canPlace(_x,_y,_turn)==true then
for i=1, 8 do
vx, vy = _x + direction[i][1], _y + direction[i][2]
if vx>0 and vx<9 and vy>0 and vy<9 then
if board[vx][vy] == oppos then
while 1 do
vx, vy = vx + direction[i][1], vy + direction[i][2]
if vx<1 or vx>8 or vy<1 or vy>8 then
break
end
if board[vx][vy]==_turn then
vvx, vvy = _x + direction[i][1], _y + direction[i][2]
board[_x][_y] = _turn
while 1 do
if vvx<1 or vvx>8 or vvy<1 or vvy>8 then
break
end
if board[vvx][vvy]==oppos then
board[vvx][vvy]=_turn
else
break
end
vvx, vvy = vvx + direction[i][1], vvy + direction[i][2]
end
elseif board[vx][vy]==NONE then
break
end
end
end
end
end
end
end
function placableCnt(_turn)
local cnt=0
for x=1,8 do
for y=1,8 do
if canPlace(x,y,_turn)==true then
cnt = cnt + 1
end
end
end
return cnt
end
-------------------------------------------------------------------------------
--DISPLAY
function monChk()
local x, y = mon.getSize()
if x~=12 or y~=8 then
print("The monitor must be 2x2")
sleep(2)
terminate()
end
if mon.isColor()==false then
print("The monitor is not an advanced")
sleep(2)
terminate()
end
end
function boardDraw(turn)
--Board Number
for i=1,8 do
mon.setCursorPos(1,i)
mon.setBackgroundColor(colors.gray)
mon.setTextColor(colors.white)
mon.write(i.." ")
end
--Board Stone
for x=1,8 do
for y=1,8 do
mon.setCursorPos(x+1,y)
if board[x][y] == NONE and (x+y)%2 == 0 then
mon.setBackgroundColor(colors.green)
else
mon.setBackgroundColor(colors.lime)
end
if canPlace(x,y,TURN_CURRENT)==true then
mon.setBackgroundColor(colors.red)
end
if board[x][y] == WHITE then
mon.setBackgroundColor(colors.white)
elseif board[x][y] == BLACK then
mon.setBackgroundColor(colors.black)
end
mon.write(" ")
end
end
--Cursor
mon.setBackgroundColor(colors.gray)
mon.setTextColor(colors.white)
if turn==WHITE then
mon.setCursorPos(10,1)
mon.write(">")
elseif turn==BLACK then
mon.setCursorPos(10,2)
mon.write(">")
end
--Count
local amountWhite, amountBlack = boardCount()
local stwhite, stblack = tostring(amountWhite), tostring(amountBlack)
mon.setBackgroundColor(colors.lightGray)
mon.setTextColor(colors.white)
mon.setCursorPos(11,1)
if amountWhite<10 then
stwhite = string.sub(stwhite,1,1)
mon.write(" "..stwhite)
elseif amountWhite>9 then
stwhite = string.sub(stwhite,1,2)
mon.write(stwhite)
end
mon.setTextColor(colors.black)
mon.setCursorPos(11,2)
if amountBlack<10 then
stblack = string.sub(stblack,1,1)
mon.write(" "..stblack)
elseif amountBlack>9 then
stblack = string.sub(stblack,1,2)
mon.write(stblack)
end
mon.setBackgroundColor(colors.gray)
mon.setTextColor(colors.white)
mon.setCursorPos(10,8)
mon.write("RES")
if PASSFLAG > 0 then
mon.setCursorPos(11,3)
mon.write("P")
mon.setCursorPos(11,4)
mon.write("A")
mon.setCursorPos(11,5)
mon.write("S")
mon.setCursorPos(11,6)
mon.write("S")
else
mon.setCursorPos(11,3)
mon.write(" ")
mon.setCursorPos(11,4)
mon.write(" ")
mon.setCursorPos(11,5)
mon.write(" ")
mon.setCursorPos(11,6)
mon.write(" ")
end
mon.setCursorPos(1,1)
end
function getTouchPos()
local void, nm, x, y = os.pullEvent("monitor_touch")
if x>1 and x<10 and y>0 and y<9 then
--BOARD
return x-1, y
elseif x>9 and x<13 and y==8 then
--RES
return -1,-1
else
return 0, 0
end
end
function getColorName(_color)
if _color==WHITE then
return "WHITE"
elseif _color==BLACK then
return "BLACK"
elseif _color==NONE then
return "NONE"
else
return nil
end
end
-------------------------------------------------------------------------------
acls()
mon.setTextScale(1.5)
boardReset()
monChk()
boardDraw(TURN_CURRENT)
while 1 do
local tx, ty = getTouchPos()
print(tx..":"..ty.."("..placableCnt(TURN_CURRENT)..")"..PASSFLAG)
if tx==-1 or ty==-1 then
acls()
sleep(1)
boardReset()
TURN_CURRENT = math.random(2)-1
TURN_OPPOS = oppos(TURN_CURRENT)
boardDraw(TURN_CURRENT)
PASSFLAG = 0
else
if canPlace(tx,ty,TURN_CURRENT)==true then
boardPlace(tx,ty,TURN_CURRENT)
TURN_CURRENT = oppos(TURN_CURRENT)
TURN_OPPOS = oppos(TURN_CURRENT)
PASSFLAG = 0
boardDraw(TURN_CURRENT)
sleep(0.2)
end
end
if placableCnt(TURN_CURRENT)==0 and placableCnt(oppos(TURN_CURRENT))~=0 then
PASSFLAG = PASSFLAG + 1
TURN_CURRENT = oppos(TURN_CURRENT)
sleep(0.2)
boardDraw(TURN_CURRENT)
elseif placableCnt(TURN_CURRENT)==0 and placableCnt(oppos(TURN_CURRENT))==0 then
PASSFLAG = 2
sleep(0.2)
else
PASSFLAG = 0
end
if PASSFLAG >= 2 then
sleep(1)
mon.setBackgroundColor(colors.gray)
acls()
mon.setCursorPos(1,2)
local awhite, ablack = boardCount()
if awhite>ablack then
mon.setTextColor(colors.white)
mon.write(" WHITE WON")
elseif awhite<ablack then
mon.setTextColor(colors.black)
mon.write(" BLACK WON")
else
mon.write(" DRAW")
end
PASSFLAG = 0
sleep(2)
acls()
sleep(1)
boardReset()
TURN_CURRENT = math.random(2)-1
TURN_OPPOS = oppos(TURN_CURRENT)
boardDraw(TURN_CURRENT)
end
end