File: minesweeper.cpp

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (307 lines) | stat: -rw-r--r-- 7,323 bytes parent folder | download | duplicates (4)
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
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Guido Tack <tack@gecode.org>
 *
 *  Copyright:
 *     Guido Tack, 2006
 *
 *  This file is part of Gecode, the generic constraint
 *  development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>

#include <cctype>

using namespace Gecode;

namespace {
  extern const char *specs[];
  extern const unsigned int n_examples;
  int spec_size(const char *s);
  int mineField(const char *s, int n, int i, int j);
}

/**
 * \brief %Example: Minesweeper
 *
 * This is the classical MineSweeper game.
 *
 * The instances are taken from
 *   http://www.janko.at/Raetsel/Minesweeper/index.htm
 *
 * \ingroup Example
 *
 */
class MineSweeper : public Script {
private:
  const char *spec;
  int size;
  BoolVarArray b;

  /// Access position (\a h,\a w) in the matrix.
  BoolVar pos(int h, int w) {
    return b[h*size + w];
  }
  /// Access position (\a h,\a w) in the matrix.
  const BoolVar pos(int h, int w) const {
    return b[h*size + w];
  }

  /// Return the fields in \a m around position (\a x,\a y)
  BoolVarArgs fieldsAround(Matrix<BoolVarArray>& m,
                           int x, int y) {
    int bvsize=0;
    for (int ix = std::max(0, x-1); ix<=x+1 && ix<size; ix++)
      for (int iy = std::max(0, y-1); iy<=y+1 && iy<size; iy++)
        bvsize++;
    bvsize--; // remove (x,y) itself
    BoolVarArgs b(bvsize);
    int count=0;
    for (int ix = std::max(0, x-1); ix<=x+1 && ix<size; ix++)
      for (int iy = std::max(0, y-1); iy<=y+1 && iy<size; iy++)
        if (ix != x || iy != y)
          b[count++] = m(ix,iy);

    return b;
  }

public:
  /// Actual model
  MineSweeper(const SizeOptions& opt)
    : Script(opt), spec(specs[opt.size()]),
      size(spec_size(spec)),
      b(*this,size*size,0,1) {
    Matrix<BoolVarArray> m(b, size, size);

    // Initialize matrix and post constraints
    for (int h=0; h<size; h++)
      for (int w=0; w<size; w++) {
        int v = mineField(spec, size, h, w);
        if (v != -1) {
          rel(*this, m(w, h), IRT_EQ, 0);
          linear(*this, fieldsAround(m, w, h), IRT_EQ, v);
        }
      }

    // Install branching
    branch(*this, b, BOOL_VAR_NONE(), BOOL_VAL_MAX());
  }

  /// Print solution
  virtual void
  print(std::ostream& os) const {
    for (int h = 0; h < size; ++h) {
      os << '\t';
      for (int w = 0; w < size; ++w) {
        int v = mineField(spec, size, h, w);
        if (v != -1)
          os << v << " ";
        else if (pos(h,w).val() == 1)
          os << "* ";
        else
          os << ". ";
      }
      os << std::endl;
    }
    os << std::endl;
  }

  /// Constructor for cloning \a s
  MineSweeper(MineSweeper& s) :
    Script(s), spec(s.spec), size(s.size) {
    b.update(*this, s.b);
  }
  /// Copy space during cloning
  virtual Space*
  copy(void) {
    return new MineSweeper(*this);
  }

};


/** \brief Main-function
 *  \relates MineSweeper
 */
int
main(int argc, char* argv[]) {
  SizeOptions opt("MineSweeper");
  opt.size(0);
  opt.parse(argc,argv);
  if (opt.size() >= n_examples) {
    std::cerr << "Error: size must be between 0 and "
              << n_examples-1 << std::endl;
    return 1;
  }
  Script::run<MineSweeper,DFS,SizeOptions>(opt);
  return 0;
}


namespace  {

  /** \name Minesweeper specifications
    *
    * A specification is a square matrix of characters. Alphanumeric characters represent
    * the number of mines adjacent to that field. Dots represent fields with an unknown number
    * of mines adjacent to it (or an actual mine).
    *
    * \relates MineSweeper
    */
  //@{

  /// The specifications
  const char* specs[] = {
      // 0
      "..2.3."
      "2....."
      "..24.3"
      "1.34.."
      ".....3"
      ".3.3..",
      // 1
      ".2.211.."
      "..4.2..2"
      "2..2..3."
      "2.22.3.3"
      "..1...4."
      "1...2..3"
      ".2.22.3."
      "1.1..1.1",
      // 2
      "1..2.2.2.."
      ".32...4..1"
      "...13...4."
      "3.1...3..."
      ".21.1..3.2"
      ".3.2..2.1."
      "2..32..2.."
      ".3...32..3"
      "..3.33...."
      ".2.2...22.",
      // 3
      "2...3.1."
      ".5.4...1"
      "..5..4.."
      "2...4.5."
      ".2.4...2"
      "..5..4.."
      "2...5.4."
      ".3.3...2",
      // 4
      "0.0.1..11."
      "1.2.2.22.."
      "......2..2"
      ".23.11...."
      "0......2.1"
      "...22.1..."
      ".....3.32."
      ".5.2...3.1"
      ".3.1..3..."
      ".2...12..0",
      // 5
      ".21.2.2..."
      ".4..3...53"
      "...4.44..3"
      "4.4..5.6.."
      "..45....54"
      "34....55.."
      "..4.4..5.5"
      "2..33.6..."
      "36...3..4."
      "...4.2.21.",
      // 6
      ".32..1.."
      "....1..3"
      "3..2...4"
      ".5...5.."
      "..6...5."
      "3...5..4"
      "2..5...."
      "..2..34.",
      // 7
      ".1.....3."
      "...343..."
      "244...443"
      "...4.4..."
      ".4.4.3.6."
      "...4.3..."
      "123...133"
      "...322..."
      ".2.....3.",
      // 8
      "......."
      ".23435."
      ".1...3."
      "...5..."
      ".1...3."
      ".12234."
      ".......",
      // 9
      "2...2...2"
      ".4.4.3.4."
      "..4...1.."
      ".4.3.3.4."
      "2.......2"
      ".5.4.5.4."
      "..3...3.."
      ".4.3.5.6."
      "2...1...2"
    };

  /// Number of specifications
  const unsigned int n_examples = sizeof(specs)/sizeof(char*);

  /// Compute the size of a specification
  int spec_size(const char *s) {
    int l = std::strlen(s);
    int res = static_cast<int>(std::sqrt(static_cast<float>(l)));
    return res;
  }

  /// Return value at position (\a i,\a j) in the example \a s of size \a n
  int mineField(const char *s, int n, int i, int j) {
    assert(spec_size(s) == n);
    assert(i >= 0 && i < n);
    assert(j >= 0 && j < n);
    char c = s[i*n + j];
    if (!std::isalnum(c))
      return -1;
    if (std::isdigit(c))
      return c - '0';
    if (std::islower(c))
      c = static_cast<char>(std::toupper(c));
    // std::alpha(c) == true
    int res = (c - 'A') + 10;
    if (res > n) return 0;
    else return res;
  }
  //@}
}

// STATISTICS: example-any