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
|
implement Sweeper;
#
# michael@vitanuova.com
#
# Copyright © 2000 Vita Nuova Limited. All rights reserved.
# Copyright © 2001 Vita Nuova Holdings Limited. All rights reserved.
#
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Point, Rect, Image, Font, Context, Screen, Display: import draw;
include "tk.m";
tk: Tk;
Toplevel: import tk;
include "tkclient.m";
tkclient: Tkclient;
include "daytime.m";
daytime: Daytime;
include "rand.m";
rand: Rand;
stderr: ref Sys->FD;
Sweeper: module
{
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
mainwin: ref Toplevel;
score: int;
mines: int;
WIDTH: con 220;
HEIGHT: con 220;
EASY: con 20;
SZB: con 10;
SZI: con SZB+2; # internal board is 2 larger than visible board
Cell: adt {
mine, state: int;
};
board: array of array of Cell;
UNSELECTED, SELECTED, MARKED: con (1<<iota);
init(ctxt: ref Draw->Context, nil: list of string)
{
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
tk = load Tk Tk->PATH;
tkclient = load Tkclient Tkclient->PATH;
daytime = load Daytime Daytime->PATH;
rand = load Rand Rand->PATH;
stderr = sys->fildes(2);
rand->init(daytime->now());
daytime = nil;
tkclient->init();
if(ctxt == nil)
ctxt = tkclient->makedrawcontext();
(win, wmcmd) := tkclient->toplevel(ctxt, "", "Mine Sweeper", Tkclient->Hide);
mainwin = win;
sys->pctl(Sys->NEWPGRP, nil);
cmdch := chan of string;
tk->namechan(win, cmdch, "cmd");
display_board();
pid := -1;
finished := 0;
init_board();
tkclient->onscreen(win, nil);
tkclient->startinput(win, "kbd"::"ptr"::nil);
for (;;) {
alt {
s := <-win.ctxt.kbd =>
tk->keyboard(win, s);
s := <-win.ctxt.ptr =>
tk->pointer(win, *s);
c := <-win.ctxt.ctl or
c = <-win.wreq or
c = <- wmcmd => # wm commands
case c {
"exit" =>
if(pid != -1)
kill(pid);
exit;
* =>
tkclient->wmctl(win, c);
}
c := <- cmdch => # tk commands
(nil, toks) := sys->tokenize(c, " ");
case hd toks {
"b" =>
x := int hd tl toks;
y := int hd tl tl toks;
i := board_check(x, y);
case i {
-1 =>
display_mines();
display_lost();
finished = 1;
0 to 8 =>
if (finished)
break;
score++;
board[x][y].state = SELECTED;
display_square(x, y, sys->sprint("%d", i), "olive");
if (i == 0) { # check all adjacent zeros
display_zeros(x, y);
}
display_score();
if (score+mines == SZB*SZB) {
display_mines();
display_win();
finished = 1;
}
* =>
;
}
cmd(mainwin, "update");
"b3" =>
x := int hd tl toks;
y := int hd tl tl toks;
mark_square(x, y);
cmd(mainwin, "update");
"restart" =>
init_board();
display_score();
reset_display();
finished = 0;
* =>
sys->fprint(stderr, "%s\n", c);
}
}
}
}
display_board() {
i, j: int;
pack: string;
for(i = 0; i < len win_config; i++)
cmd(mainwin, win_config[i]);
for (i = 1; i <= SZB; i++) {
cmd(mainwin, sys->sprint("frame .f%d", i));
pack = "";
for (j = 1; j <= SZB; j++) {
pack += sys->sprint(" .f%d.b%dx%d", i, i, j);
cmd(mainwin, sys->sprint("button .f%d.b%dx%d -text { } -width 14 -command {send cmd b %d %d}", i, i, j, i, j));
cmd(mainwin, sys->sprint("bind .f%d.b%dx%d <ButtonRelease-3> {send cmd b3 %d %d}", i, i, j, i, j));
}
cmd(mainwin, sys->sprint("pack %s -side left", pack));
cmd(mainwin, sys->sprint("pack .f%d -side top -fill x", i));
}
for (i = 0; i < len win_config2; i++)
cmd (mainwin, win_config2[i]);
}
reset_display()
{
for (i := 1; i <= SZB; i++) {
for (j := 1; j <= SZB; j++) {
s := sys->sprint(".f%d.b%dx%d configure -text { } -bg #dddddd -activebackground #eeeeee", i, i, j);
cmd(mainwin, s);
}
}
cmd(mainwin, "update");
}
init_board()
{
i, j: int;
score = 0;
mines = 0;
board = array[SZI] of array of Cell;
for (i = 0; i < SZI; i++)
board[i] = array[SZI] of Cell;
# initialize board
for (i = 0; i < SZI; i++)
for (j =0; j < SZI; j++) {
board[i][j].mine = 0;
board[i][j].state = UNSELECTED;
}
# place mines
for (i = 0; i < EASY; i++) {
j = rand->rand(SZB*SZB);
if (board[(j/SZB)+1][(j%SZB)+1].mine == 0) { # rand could yield same result twice
board[(j/SZB)+1][(j%SZB)+1].mine = 1;
mines++;
}
}
cmd(mainwin, "update");
}
display_score()
{
cmd(mainwin, ".f.l configure -text {Score: "+ sys->sprint("%d", score)+ "}");
}
display_win()
{
cmd(mainwin, ".f.l configure -text {You have Won}");
}
display_lost()
{
cmd(mainwin, ".f.l configure -text {You have Lost}");
}
display_mines()
{
for (i := 1; i <= SZB; i++)
for (j := 1; j <= SZB; j++)
if (board[i][j].mine == 1)
display_square(i, j, "M", "red");
}
display_square(i, j: int, v: string, c: string) {
cmd(mainwin, sys->sprint(".f%d.b%dx%d configure -text {%s} -bg %s -activebackground %s", i, i, j, v, c, c));
cmd(mainwin, "update");
}
mark_square(i, j: int) {
case board[i][j].state {
UNSELECTED =>
board[i][j].state = MARKED;
display_square(i, j, "?", "orange");
MARKED =>
board[i][j].state = UNSELECTED;
display_square(i, j, " ", "#dddddd");
}
}
board_check(i, j: int) : int
{
if (board[i][j].mine == 1)
return -1;
if (board[i][j].state&(SELECTED|MARKED))
return -2;
c := 0;
for (x := i-1; x <= i+1; x++)
for (y := j-1; y <= j+1; y++)
if (board[x][y].mine == 1)
c++;
return c;
}
display_zeros(i, j: int)
{
for (x := i-1; x <= i+1; x++) {
for (y := j-1; y <= j+1; y++) {
if (x <1 || x>SZB || y<1 || y>SZB)
continue;
if (board_check(x, y) == 0) {
score++;
board[x][y].state = SELECTED;
display_square(x, y, "0", "olive");
display_zeros(x, y);
}
}
}
}
fatal(s: string)
{
sys->fprint(stderr, "%s\n", s);
exit;
}
sleep(t: int)
{
sys->sleep(t);
}
kill(pid: int): int
{
fd := sys->open("#p/"+string pid+"/ctl", Sys->OWRITE);
if(fd == nil)
return -1;
if(sys->write(fd, array of byte "kill", 4) != 4)
return -1;
return 0;
}
cmd(top: ref Toplevel, s: string): string
{
e := tk->cmd(top, s);
if (e != nil && e[0] == '!')
sys->fprint(stderr, "sweeper: tk error on '%s': %s\n", s, e);
return e;
}
win_config := array[] of {
"frame .f -width 220 -height 220",
"menubutton .f.sz -text Options -menu .f.sz.sm",
"menu .f.sz.sm",
".f.sz.sm add command -label restart -command { send cmd restart }",
"pack .f.sz -side left",
"label .f.l -text {Score: }",
"pack .f.l -side right",
"frame .ft",
"label .ft.l -text { }",
"pack .ft.l -side left",
"pack .f -side top -fill x",
"pack .ft -side top -fill x",
};
win_config2 := array[] of {
"pack propagate . 0",
"update",
};
|