-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.mzn
40 lines (30 loc) · 912 Bytes
/
minesweeper.mzn
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
int : width;
int : height;
int : w = width-1;
int : h = height-1;
array [0..w, 0..h] of var bool : mined;
int : nclues;
array [1..nclues, 1..3] of int : clues;
int : nguesses;
array [1..nguesses, 1..3] of int : guesses;
constraint
forall (c in 1..nclues)
( let { int : x = clues[c,1],
int : y = clues[c,2],
int : n = clues[c,3] }
in if n == -1
then mined[x,y] = true
else mined[x,y] = false /\
sum( [ bool2int(mined[x+i,y+j]) | i,j in [-1,0,1] ] ) == n
endif );
constraint
forall (g in 1..nguesses)
( let { int : x = guesses[g,1],
int : y = guesses[g,2] }
in mined[x,y] = (guesses[g,3] == 1) );
solve satisfy;
% width = 10;
% height = 10;
% nclues = 1;
% clues = [| 0,0,0 |];
output [ if fix(mined[x,y]) then "*" else "." endif ++ if x == w then "\n" else "" endif | y in 0..h, x in 0..w ] ++ ["\n"];