summaryrefslogtreecommitdiff
path: root/demos/gtk-demo/listview_minesweeper.c
blob: 236a92fe06dc10cf0d177ecb5b9ead71179568ea (plain)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* Lists/Minesweeper
 * #Keywords: GtkGridView, GListModel, game
 *
 * This demo shows how to develop a user interface for small game using a
 * grid view.
 *
 * It demonstrates how to use the activate signal and single-press behavior
 * to implement rather different interaction behavior to a typical list.
 */

#include "config.h"
#include <glib/gi18n.h>
#include <gtk/gtk.h>

/*** The cell object ***/

/* Create an object that holds the data for a cell in the game */
typedef struct _SweeperCell SweeperCell;
struct _SweeperCell
{
  GObject parent_instance;

  gboolean is_mine;
  gboolean is_visible;
  guint neighbor_mines;
};

enum {
  CELL_PROP_0,
  CELL_PROP_LABEL,

  N_CELL_PROPS
};

#define SWEEPER_TYPE_CELL (sweeper_cell_get_type ())
G_DECLARE_FINAL_TYPE (SweeperCell, sweeper_cell, SWEEPER, CELL, GObject);

G_DEFINE_TYPE (SweeperCell, sweeper_cell, G_TYPE_OBJECT);
static GParamSpec *cell_properties[N_CELL_PROPS] = { NULL, };

static const char *
sweeper_cell_get_label (SweeperCell *self)
{
  static const char *minecount_labels[10] = { "", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

  if (!self->is_visible)
    return "?";
  
  if (self->is_mine)
    return "💣";

  return minecount_labels[self->neighbor_mines];
}

static void
sweeper_cell_get_property (GObject    *object,
                           guint       property_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  SweeperCell *self = SWEEPER_CELL (object);

  switch (property_id)
    {
    case CELL_PROP_LABEL:
      g_value_set_string (value, sweeper_cell_get_label (self));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
sweeper_cell_class_init (SweeperCellClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->get_property = sweeper_cell_get_property;

  cell_properties[CELL_PROP_LABEL] =
    g_param_spec_string ("label",
                         "label",
                         "label to display for this row",
                         NULL,
                         G_PARAM_READABLE);

  g_object_class_install_properties (gobject_class, N_CELL_PROPS, cell_properties);
}

static void
sweeper_cell_init (SweeperCell *self)
{
}

static void
sweeper_cell_reveal (SweeperCell *self)
{
  if (self->is_visible)
    return;

  self->is_visible = TRUE;

  g_object_notify_by_pspec (G_OBJECT (self), cell_properties[CELL_PROP_LABEL]);
}

static SweeperCell *
sweeper_cell_new (void)
{
  return g_object_new (SWEEPER_TYPE_CELL, NULL);
}

/*** The board object ***/

/* Create an object that holds the data for the game */
typedef struct _SweeperGame SweeperGame;
struct _SweeperGame
{
  GObject parent_instance;

  GPtrArray *cells;
  guint width;
  guint height;
  gboolean playing;
  gboolean win;
};

enum {
  GAME_PROP_0,
  GAME_PROP_HEIGHT,
  GAME_PROP_PLAYING,
  GAME_PROP_WIDTH,
  GAME_PROP_WIN,

  N_GAME_PROPS
};

#define SWEEPER_TYPE_GAME (sweeper_game_get_type ())
G_DECLARE_FINAL_TYPE (SweeperGame, sweeper_game, SWEEPER, GAME, GObject);

static GType
sweeper_game_list_model_get_item_type (GListModel *model)
{
  return SWEEPER_TYPE_GAME;
}

static guint
sweeper_game_list_model_get_n_items (GListModel *model)
{
  SweeperGame *self = SWEEPER_GAME (model);

  return self->width * self->height;
}

static gpointer
sweeper_game_list_model_get_item (GListModel *model,
                                  guint       position)
{
  SweeperGame *self = SWEEPER_GAME (model);

  return g_object_ref (g_ptr_array_index (self->cells, position));
}

static void
sweeper_game_list_model_init (GListModelInterface *iface)
{
  iface->get_item_type = sweeper_game_list_model_get_item_type;
  iface->get_n_items = sweeper_game_list_model_get_n_items;
  iface->get_item = sweeper_game_list_model_get_item;
}

G_DEFINE_TYPE_WITH_CODE (SweeperGame, sweeper_game, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, sweeper_game_list_model_init))

static GParamSpec *game_properties[N_GAME_PROPS] = { NULL, };

static void
sweeper_game_dispose (GObject *object)
{
  SweeperGame *self = SWEEPER_GAME (object);

  g_clear_pointer (&self->cells, g_ptr_array_unref);

  G_OBJECT_CLASS (sweeper_game_parent_class)->dispose (object);
}

static void
sweeper_game_get_property (GObject    *object,
                           guint       property_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  SweeperGame *self = SWEEPER_GAME (object);

  switch (property_id)
    {
    case GAME_PROP_HEIGHT:
      g_value_set_uint (value, self->height);
      break;

    case GAME_PROP_PLAYING:
      g_value_set_boolean (value, self->playing);
      break;

    case GAME_PROP_WIDTH:
      g_value_set_uint (value, self->width);
      break;

    case GAME_PROP_WIN:
      g_value_set_boolean (value, self->win);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
sweeper_game_class_init (SweeperGameClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->dispose = sweeper_game_dispose;
  gobject_class->get_property = sweeper_game_get_property;

  game_properties[GAME_PROP_HEIGHT] =
    g_param_spec_uint ("height",
                       "height",
                       "height of the game grid",
                       1, G_MAXUINT, 8,
                       G_PARAM_READABLE);

  game_properties[GAME_PROP_PLAYING] =
    g_param_spec_boolean ("playing",
                          "playing",
                          "if the game is still going on",
                          FALSE,
                          G_PARAM_READABLE);

  game_properties[GAME_PROP_WIDTH] =
    g_param_spec_uint ("width",
                       "width",
                       "width of the game grid",
                       1, G_MAXUINT, 8,
                       G_PARAM_READABLE);

  game_properties[GAME_PROP_WIN] =
    g_param_spec_boolean ("win",
                          "win",
                          "if the game was won",
                          FALSE,
                          G_PARAM_READABLE);

  g_object_class_install_properties (gobject_class, N_GAME_PROPS, game_properties);
}

static void
sweeper_game_reset_board (SweeperGame *self,
                          guint        width,
                          guint        height)
{
  guint i;

  g_ptr_array_set_size (self->cells, 0);

  for (i = 0; i < width * height; i++)
    {
      g_ptr_array_add (self->cells, sweeper_cell_new ());
    }

  if (self->width != width)
    {
      self->width = width;
      g_object_notify_by_pspec (G_OBJECT (self), game_properties[GAME_PROP_WIDTH]);
    }
  if (self->height != height)
    {
      self->height = height;
      g_object_notify_by_pspec (G_OBJECT (self), game_properties[GAME_PROP_HEIGHT]);
    }
  if (!self->playing)
    {
      self->playing = TRUE;
      g_object_notify_by_pspec (G_OBJECT (self), game_properties[GAME_PROP_PLAYING]);
    }
  if (self->win)
    {
      self->win = FALSE;
      g_object_notify_by_pspec (G_OBJECT (self), game_properties[GAME_PROP_WIN]);
    }
}

static void
sweeper_game_place_mines (SweeperGame *self,
                          guint        n_mines)
{
  guint i;

  for (i = 0; i < n_mines; i++)
    {
      SweeperCell *cell;

      do {
        cell = g_ptr_array_index (self->cells, g_random_int_range (0, self->cells->len));
      } while (cell->is_mine);

      cell->is_mine = TRUE;
    }
}

static SweeperCell *
get_cell (SweeperGame *self,
          guint        x,
          guint        y)
{
  return g_ptr_array_index (self->cells, y * self->width + x);
}

static void
sweeper_game_count_neighbor_mines (SweeperGame *self,
                                   guint        width,
                                   guint        height)
{
  guint x, y, x2, y2;

  for (y = 0; y < height; y++)
    {
      for (x = 0; x < width; x++)
        {
          SweeperCell *cell = get_cell (self, x, y);

          for (y2 = MAX (1, y) - 1; y2 < MIN (height, y + 2); y2++)
            {
              for (x2 = MAX (1, x) - 1; x2 < MIN (width, x + 2); x2++)
                {
                  SweeperCell *other = get_cell (self, x2, y2);

                  if (other->is_mine)
                    cell->neighbor_mines++;
                }
            }
        }
    }
}

static void
sweeper_game_new_game (SweeperGame *self,
                       guint        width,
                       guint        height,
                       guint        n_mines)
{
  guint n_items_before;

  g_return_if_fail (n_mines <= width * height);

  n_items_before = self->width * self->height;

  g_object_freeze_notify (G_OBJECT (self));

  sweeper_game_reset_board (self, width, height);
  sweeper_game_place_mines (self, n_mines);
  sweeper_game_count_neighbor_mines (self, width, height);

  g_list_model_items_changed (G_LIST_MODEL (self), 0, n_items_before, width * height);

  g_object_thaw_notify (G_OBJECT (self));
}

static void
sweeper_game_init (SweeperGame *self)
{
  self->cells = g_ptr_array_new_with_free_func (g_object_unref);

  sweeper_game_new_game (self, 8, 8, 10);
}

static void
celebrate (gboolean win)
{
  char *path;
  GtkMediaStream *stream;

  if (win)
    path = g_build_filename (GTK_DATADIR, "sounds", "freedesktop", "stereo", "complete.oga", NULL);
  else
    path = g_build_filename (GTK_DATADIR, "sounds", "freedesktop", "stereo", "suspend-error.oga", NULL);
  stream = gtk_media_file_new_for_filename (path);
  gtk_media_stream_set_volume (stream, 1.0);
  gtk_media_stream_play (stream);
  g_signal_connect (stream, "notify::ended", G_CALLBACK (g_object_unref), NULL);
  g_free (path);
}

static void
sweeper_game_end (SweeperGame *self,
                  gboolean     win)
{
  if (self->playing)
    {
      self->playing = FALSE;
      g_object_notify_by_pspec (G_OBJECT (self), game_properties[GAME_PROP_PLAYING]);
      celebrate (win);
    }

  if (self->win != win)
    {
      self->win = win;
      g_object_notify_by_pspec (G_OBJECT (self), game_properties[GAME_PROP_WIN]);
    }
}

static void
sweeper_game_check_finished (SweeperGame *self)
{
  guint i;

  if (!self->playing)
    return;

  for (i = 0; i < self->cells->len; i++)
    {
      SweeperCell *cell = g_ptr_array_index (self->cells, i);

      /* There's still a non-revealed cell that isn't a mine */
      if (!cell->is_visible && !cell->is_mine)
        return;
    }

  sweeper_game_end (self, TRUE);
}

static void
sweeper_game_reveal_cell (SweeperGame *self,
                          guint        position)
{
  SweeperCell *cell;

  if (!self->playing)
    return;

  cell = g_ptr_array_index (self->cells, position);
  sweeper_cell_reveal (cell);

  if (cell->is_mine)
    sweeper_game_end (self, FALSE);

  sweeper_game_check_finished (self);
}

G_MODULE_EXPORT void
minesweeper_cell_clicked_cb (GtkGridView *gridview,
                             guint        pos,
                             SweeperGame *game)
{
  sweeper_game_reveal_cell (game, pos);
}

G_MODULE_EXPORT void
minesweeper_new_game_cb (GtkButton   *button,
                         SweeperGame *game)
{
  sweeper_game_new_game (game, 8, 8, 10);
}

static GtkWidget *window = NULL;

GtkWidget *
do_listview_minesweeper (GtkWidget *do_widget)
{
  if (window == NULL)
    {
      GtkBuilder *builder;

      g_type_ensure (SWEEPER_TYPE_GAME);

      builder = gtk_builder_new_from_resource ("/listview_minesweeper/listview_minesweeper.ui");
      window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
      gtk_window_set_display (GTK_WINDOW (window),
                              gtk_widget_get_display (do_widget));
      g_object_add_weak_pointer (G_OBJECT (window), (gpointer *) &window);

      g_object_unref (builder);
    }

  if (!gtk_widget_get_visible (window))
    gtk_widget_set_visible (window, TRUE);
  else
    gtk_window_destroy (GTK_WINDOW (window));

  return window;
}