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
|
implement Gatherengine;
include "sys.m";
sys: Sys;
include "draw.m";
include "sets.m";
All, None: import Sets;
include "../spree.m";
spree: Spree;
Attributes, Range, Object, Clique, Member: import spree;
include "objstore.m";
objstore: Objstore;
include "../gather.m";
clique: ref Clique;
Black, White, Nocolour: con iota; # first two must be 0 and 1.
N: con 8;
boardobj: ref Object;
board: array of array of int;
pieces: array of int;
turn := Nocolour;
members := array[2] of ref Member; # member ids of those playing
Point: adt {
x, y: int;
add: fn(p: self Point, p1: Point): Point;
inboard: fn(p: self Point): int;
};
clienttype(): string
{
return "othello";
}
init(srvmod: Spree, g: ref Clique, nil: list of string, nil: int): string
{
sys = load Sys Sys->PATH;
clique = g;
spree = srvmod;
objstore = load Objstore Objstore->PATH;
if (objstore == nil) {
sys->print("othello: cannot load %s: %r", Objstore->PATH);
return "bad module";
}
objstore->init(srvmod, g);
return nil;
}
maxmembers(): int
{
return 2;
}
readfile(nil: int, nil: big, nil: int): array of byte
{
return nil;
}
propose(members: array of string): string
{
if (len members != 2)
return "need exactly two members";
return nil;
}
archive()
{
objstore->setname(boardobj, "board");
}
start(pl: array of ref Member, archived: int)
{
members = pl;
board = array[N] of {* => array[N] of {* => Nocolour}};
pieces = array[2] of {* => 0};
if (archived) {
objstore->unarchive();
boardobj = objstore->get("board");
for (i := 0; i < N; i++) {
for (j := 0; j < N; j++) {
a := boardobj.getattr(pt2attr((j, i)));
if (a != nil) {
piece := int a;
board[j][i] = piece;
if (piece != Nocolour)
pieces[piece]++;
}
}
}
turn = int boardobj.getattr("turn");
} else {
boardobj = clique.newobject(nil, All, nil);
boardobj.setattr("members", string members[Black].name + " " + string members[White].name, All);
for (ps := (Black, (3, 3)) :: (Black, (4, 4)) :: (White, (3, 4)) :: (White, Point(4, 3)) :: nil;
ps != nil;
ps = tl ps) {
(colour, p) := hd ps;
setpiece(colour, p);
}
turn = Black;
boardobj.setattr("turn", string Black, All);
}
}
cliqueover()
{
turn = Nocolour;
boardobj.setattr("winner", string winner(), All);
boardobj.setattr("turn", string turn, All);
}
command(member: ref Member, cmd: string): string
{
{
(n, toks) := sys->tokenize(cmd, " \n");
assert(n > 0, "unknown command");
case hd toks {
"move" =>
assert(n == 3, "bad command usage");
assert(turn != Nocolour, "clique has finished");
assert(member == members[White] || member == members[Black], "you are not playing");
assert(member == members[turn], "it is not your turn");
p := Point(int hd tl toks, int hd tl tl toks);
assert(p.x >= 0 && p.x < N && p.y >= 0 && p.y < N, "invalid move position");
assert(board[p.x][p.y] == Nocolour, "position is already occupied");
assert(newmove(turn, p, 1), "cannot move there");
turn = reverse(turn);
if (!canplay()) {
turn = reverse(turn);
if (!canplay())
cliqueover();
}
boardobj.setattr("turn", string turn, All);
return nil;
}
sys->print("othello: unknown client command '%s'\n", hd toks);
return "who knows";
} exception e {
"parse:*" =>
return e[6:];
}
}
Directions := array[] of {Point(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)};
setpiece(colour: int, p: Point)
{
v := board[p.x][p.y];
if (v != Nocolour)
pieces[v]--;
board[p.x][p.y] = colour;
pieces[colour]++;
boardobj.setattr(pt2attr(p), string colour, All);
}
pt2attr(pt: Point): string
{
s := " ";
s[0] = pt.x + 'a';
s[1] = pt.y + 'a';
return s;
}
# member colour has tried to place a piece at mp.
# return -1 if it's an illegal move, 0 otherwise.
# (in which case appropriate updates are sent out all round).
# if update is 0, just check for the move's validity
# (no change to the board, no updates sent)
newmove(colour: int, mp: Point, update: int): int
{
totchanged := 0;
for (i := 0; i < len Directions; i++) {
d := Directions[i];
n := 0;
for (p := mp.add(d); p.inboard(); p = p.add(d)) {
n++;
if (board[p.x][p.y] == colour || board[p.x][p.y] == Nocolour)
break;
}
if (p.inboard() && board[p.x][p.y] == colour && n > 1) {
if (!update)
return 1;
totchanged += n - 1;
for (p = mp.add(d); --n > 0; p = p.add(d))
setpiece(reverse(board[p.x][p.y]), p);
}
}
if (totchanged > 0) {
setpiece(colour, mp);
return 1;
}
return 0;
}
# who has most pieces?
winner(): int
{
if (pieces[White] > pieces[Black])
return White;
else if (pieces[Black] > pieces[White])
return Black;
return Nocolour;
}
# is there any possible legal move?
canplay(): int
{
for (y := 0; y < N; y++)
for (x := 0; x < N; x++)
if (board[x][y] == Nocolour && newmove(turn, (x, y), 0))
return 1;
return 0;
}
reverse(colour: int): int
{
if (colour == Nocolour)
return Nocolour;
return !colour;
}
Point.add(p: self Point, p1: Point): Point
{
return (p.x + p1.x, p.y + p1.y);
}
Point.inboard(p: self Point): int
{
return p.x >= 0 && p.x < N && p.y >= 0 && p.y < N;
}
assert(b: int, err: string)
{
if (b == 0)
raise "parse:" + err;
}
|