-
Notifications
You must be signed in to change notification settings - Fork 2
/
Reversi8x8.usp
364 lines (323 loc) · 9.63 KB
/
Reversi8x8.usp
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
/**
* Module: Reversi 8x8 Grid Game Processor
* Description: Simple Reversi/Othello implementation
* Programmer: TG
* Date: 2018-07-21
**/
#SYMBOL_NAME "Reversi 8x8 Processor"
#CATEGORY "46" "Games"
#DEFAULT_VOLATILE
#ENABLE_STACK_CHECKING
#ENABLE_TRACE
#DEFINE_CONSTANT TRUE 1
#DEFINE_CONSTANT FALSE 0
#DEFINE_CONSTANT CHECK_DIRECTION_NORTH 1
#DEFINE_CONSTANT CHECK_DIRECTION_SOUTH 2
#DEFINE_CONSTANT CHECK_DIRECTION_WEST 3
#DEFINE_CONSTANT CHECK_DIRECTION_EAST 4
#DEFINE_CONSTANT CHECK_DIRECTION_NORTH_WEST 5
#DEFINE_CONSTANT CHECK_DIRECTION_NORTH_EAST 6
#DEFINE_CONSTANT CHECK_DIRECTION_SOUTH_WEST 7
#DEFINE_CONSTANT CHECK_DIRECTION_SOUTH_EAST 8
DIGITAL_INPUT restart;
DIGITAL_INPUT grid[64];
DIGITAL_OUTPUT finished;
ANALOG_OUTPUT player, winner;
ANALOG_OUTPUT scores[2];
ANALOG_OUTPUT states[64];
INTEGER _scores[2];
INTEGER _states[64];
FUNCTION UpdateAndRecalculate()
{
INTEGER n;
// reset scores
SetArray(_scores, 0);
for (n = 1 to 64)
{
// add up scores for each of the states for the player index
// in the array
_scores[_states[n]] = _scores[_states[n]] + 1;
states[n] = _states[n]; // update state
}
// set scores to the ouptuts
scores[1] = _scores[1];
scores[2] = _scores[2];
}
FUNCTION SwitchPlayer()
{
player = ((player >> 1) ^ 1) + 1; // bitflip
}
INTEGER_FUNCTION GetIndexByRowAndColumn(INTEGER row, INTEGER column)
{
return ((8 * (row - 1)) + column);
}
INTEGER_FUNCTION GetStateByRowAndColumn(INTEGER row, INTEGER column)
{
return (_states[GetIndexByRowAndColumn(row, column)]);
}
FUNCTION SetStateByRowAndColumn(INTEGER row, INTEGER column, INTEGER slotType)
{
_states[GetIndexByRowAndColumn(row, column)] = slotType;
}
INTEGER_FUNCTION AttemptMove(INTEGER direction, INTEGER playerIndex, INTEGER row, INTEGER column)
{
INTEGER opponentSlot, slotType;
INTEGER r, c;
r = row;
c = column;
cswitch (direction)
{
case (CHECK_DIRECTION_NORTH):
{
while (r > 1)
{
r = r - 1;
slotType = GetStateByRowAndColumn(r, c);
if (!slotType) { break; }
if (slotType != playerIndex)
opponentSlot = TRUE;
else if (opponentSlot)
{
while (r < row)
{
SetStateByRowAndColumn(r, c, playerIndex);
r = r + 1;
}
return (TRUE);
}
}
break;
}
case (CHECK_DIRECTION_SOUTH):
{
while (r < 8)
{
r = r + 1;
slotType = GetStateByRowAndColumn(r, c);
if (!slotType) { break; }
if (slotType != playerIndex)
opponentSlot = TRUE;
else if (opponentSlot)
{
while (r > row)
{
SetStateByRowAndColumn(r, c, playerIndex);
r = r - 1;
}
return (TRUE);
}
}
break;
}
case (CHECK_DIRECTION_WEST):
{
while (c > 1)
{
c = c - 1;
slotType = GetStateByRowAndColumn(r, c);
if (!slotType) { break; }
if (slotType != playerIndex)
opponentSlot = TRUE;
else if (opponentSlot)
{
while (c < column)
{
SetStateByRowAndColumn(r, c, playerIndex);
c = c + 1;
}
return (TRUE);
}
}
break;
}
case (CHECK_DIRECTION_EAST):
{
while (c < 8)
{
c = c + 1;
slotType = GetStateByRowAndColumn(r, c);
if (!slotType) { break; }
if (slotType != playerIndex)
opponentSlot = TRUE;
else if (opponentSlot)
{
while (c > column)
{
SetStateByRowAndColumn(r, c, playerIndex);
c = c - 1;
}
return (TRUE);
}
}
break;
}
case (CHECK_DIRECTION_NORTH_WEST):
{
while (r > 1 && c > 1)
{
r = r - 1;
c = c - 1;
slotType = GetStateByRowAndColumn(r, c);
if (!slotType) { break; }
if (slotType != playerIndex)
opponentSlot = TRUE;
else if (opponentSlot)
{
while (r < row)
{
SetStateByRowAndColumn(r, c, playerIndex);
r = r + 1;
c = c + 1;
}
return (TRUE);
}
}
break;
}
case (CHECK_DIRECTION_NORTH_EAST):
{
while (r < 8 && c < 8)
{
r = r + 1;
c = c + 1;
slotType = GetStateByRowAndColumn(r, c);
if (!slotType) { break; }
if (slotType != playerIndex)
opponentSlot = TRUE;
else if (opponentSlot)
{
while (r > row)
{
SetStateByRowAndColumn(r, c, playerIndex);
r = r - 1;
c = c - 1;
}
return (TRUE);
}
}
break;
}
case (CHECK_DIRECTION_SOUTH_WEST):
{
while (r < 8 && c > 1)
{
r = r + 1;
c = c - 1;
slotType = GetStateByRowAndColumn(r, c);
if (!slotType) { break; }
if (slotType != playerIndex)
opponentSlot = TRUE;
else if (opponentSlot)
{
while (r > row)
{
SetStateByRowAndColumn(r, c, playerIndex);
r = r - 1;
c = c + 1;
}
return (TRUE);
}
}
break;
}
case (CHECK_DIRECTION_SOUTH_EAST):
{
while (r > 1 && c < 8)
{
r = r - 1;
c = c + 1;
slotType = GetStateByRowAndColumn(r, c);
if (!slotType) { break; }
if (slotType != playerIndex)
opponentSlot = TRUE;
else if (opponentSlot)
{
while (r < row)
{
SetStateByRowAndColumn(r, c, playerIndex);
r = r + 1;
c = c - 1;
}
return (TRUE);
}
}
break;
}
}
return (FALSE);
}
FUNCTION CheckForFinish(INTEGER playerIndex)
{
// check if the either opponent has been cleared from the board
// or if the score for # of markers between each player equals the
// size of the board (indicating that the board is filled in)
if ((_scores[1] = 0)
|| (_scores[2] = 0)
|| (_scores[1] + _scores[2] = 64))
{
if (_scores[1] > _scores[2])
winner = 1;
else
winner = 2;
// pulse finished output
Pulse(1, finished);
}
}
FUNCTION Initialize()
{
player = 1; // reset player
// reset
SetArray(_scores, 0);
SetArray(_states, 0);
// set initial markers for game start
_states[GetIndexByRowAndColumn(4, 4)] = 1;
_states[GetIndexByRowAndColumn(4, 5)] = 2;
_states[GetIndexByRowAndColumn(5, 4)] = 2;
_states[GetIndexByRowAndColumn(5, 5)] = 1;
UpdateAndRecalculate();
}
PUSH restart
{
Initialize();
}
PUSH grid
{
INTEGER n, changed;
INTEGER row, column;
n = GetLastModifiedArrayIndex();
if (_states[n] = 0)
{
row = (n + 7) / 8;
column = n - ((row - 1) * 8);
changed = FALSE;
for (n = 1 to 8)
{
// check each of the 8 directions to see if the current
// grid cell indicates a valid move and set a flag if
// the internal board state was changed as a result
if (AttemptMove(n, player, row, column))
{
changed = TRUE;
}
}
// update the grid, check for game finish, and switch
// current player index only if the board has changed
if (changed)
{
// add player marker
SetStateByRowAndColumn(row, column, player);
UpdateAndRecalculate();
SwitchPlayer();
// check if the game is finished after switching turns
// because previous player had already made a move
// (we want to see if the next player is able to make
// any moves.)
CheckForFinish(player);
}
}
}
FUNCTION Main()
{
WaitForInitializationComplete();
Initialize(); // set outputs for initial state
}