File: lmem_window.c

package info (click to toggle)
lmemory 0.6c-9
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,164 kB
  • sloc: sh: 4,797; ansic: 1,479; makefile: 36
file content (388 lines) | stat: -rw-r--r-- 8,409 bytes parent folder | download | duplicates (6)
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
/* 
 *  Linux memory game
 *  By Xiaoguang Zhang
 */

#include <gtk/gtk.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include "lmemory.h"

GtkWidget *xpm_widget(GtkWidget *parent, gchar *xpm_filename);
GtkWidget *xpm_widget_default( GtkWidget *parent);

  int flipped;
  int flipped_btn[3];

/*
remove a pixmap
*/
void
lmem_remove_card(GtkWidget * widget,GtkWidget * pixmap)
{
	gtk_widget_hide(pixmap);
	gtk_object_ref(GTK_OBJECT(pixmap));
  	gtk_container_remove(GTK_CONTAINER(widget),pixmap);
}

/*
Each card has two pixmaps. When it's flipped we hide the one that is
showing and show the one hidden
*/
void
flip_pixmap (GtkWidget * widget, GtkWidget * pixmap1, GtkWidget * pixmap2)
{
        lmem_remove_card(widget,pixmap1);

	gtk_widget_show(pixmap2);
  	gtk_container_add(GTK_CONTAINER(widget),pixmap2);
	gtk_object_unref(GTK_OBJECT(pixmap2));
}

/*
Move all cards on their backs by lmem_jump
*/
void
pixmaps_jump ( gint lmem_jump )
{
  gint lmem_temp[NUM];
  gint j, k;

  j = 0;
  while (j<NUM)
  {
    lmem_temp[j] = lmem_state[j];
    j++;
  }

  j = 0;
  while (j<NUM)
  {
/*
Skip the blank squares (no cards)
*/
    if (lmem_state[j] != 0) {
    k = j+lmem_jump;
/* Wrap around if we move to the last square */
    if(k>=NUM) k = k-NUM;
    while (lmem_temp[k]==0)
    {
      k++;
      if(k==NUM) k=0;
    }

    lmem_state[j] = lmem_temp[k];
    lmem_temp[k] = 0; /* erase this because it's been used */
    }
    j++;
  }

}

/*
Flip the card that is clicked
*/
void
lmem_flip_card (GtkWidget * widget, gpointer * data)
{
  gint k, l, lmem_jump;
  gint j;
  char *frame_label;

  click_count++;
  asprintf(&frame_label, "%s:  %i",indicator[lmem_level],click_count);
  gtk_frame_set_label((GtkFrame *)frame,(gchar *)frame_label);

  if(lmem_state[(int) data]>0){
/*
If there are already two cards turned up, we turn those two cards
back.
*/
    if(flipped == match_card) {
    j = 0;
    while ( j <= flipped-1 ) {
      k = flipped_btn[j];
      if(lmem_state[k]<0){
        flip_pixmap (button[k], second_pixmap[-lmem_state[k]-match_card],
	   default_pixmap[k]);
	lmem_state[k] = -lmem_state[k];
      }
      j++;
    }
    flipped = 0;

/*
Moved the other cards if the skill level is appropriate
*/
      switch (lmem_level)
      {
	case MASTER:
	case DAEMON:
	  lmem_jump = 1;
	  break;
	default:
	  lmem_jump = 0;
      }
      if(lmem_jump>0) pixmaps_jump(lmem_jump);
    }

    k = (int) data;
      flipped_btn[flipped] = k;
      l = lmem_state[k]-match_card;
      flip_pixmap (button[k], default_pixmap[k], second_pixmap[l]);
/*
If two cards are matched remove both
*/
      if( (flipped==match_card-1) &&
          (lmem_state[flipped_btn[0]]/match_card==-lmem_state[k]/match_card) &&
          (lmem_state[flipped_btn[0]]/match_card==lmem_state[flipped_btn[flipped-1]]/match_card) ){
        lmem_remove_card(button[k],second_pixmap[l]);
        lmem_state[k]=0;
      j = 0;
      while ( j <= flipped-1 ) {
	k = flipped_btn[j];
	l = -lmem_state[k]-match_card;
        lmem_remove_card(button[k],second_pixmap[l]);
        lmem_state[k]=0;
        j++;
      }
        flipped = 0;
/*
Move the other cards if the skill level is appropriate
*/
        switch (lmem_level)
        {
	  case SKILLED:
	  case MASTER:
	    lmem_jump = 1;
	    break;
	  case DAEMON:
	    lmem_jump = lln_get_random ( (double) NUM);
	    break;
	  default:
	    lmem_jump = 0;
        }
        if(lmem_jump>0) pixmaps_jump(lmem_jump);
      }else{
/*
If a card is flipped, its lmem_state is also flipped :-)
*/
	lmem_state[k] = -lmem_state[k];
        flipped++;
      }
  }

}


void
delete_event (GtkWidget * widget, GdkEvent * event, gpointer * data)
{
  gtk_main_quit ();
}

void
lmem_pixmaps(GtkWidget *window, GSList *image_files)
{
  gint k, l;

  k = 0;
  while( k < NUM)
  {
      if(lmem_state[k] == 0) {
        default_pixmap[k] = (gpointer) xpm_widget_default(window);
        gtk_container_add (GTK_CONTAINER (button[k]), default_pixmap[k]);
        gtk_widget_show (default_pixmap[k]);
      }
      lmem_state[k] = 0;
      k++;
  }

  k = 0;
  while( k < NUM)
  {
      second_pixmap[k] = (gpointer) xpm_widget(window, image_files->data);
      gtk_object_ref(GTK_OBJECT(second_pixmap[k]));
      
      if( (((k+1)%match_card)==0 || same_card==1 ) )
        image_files = image_files->next;
      k++;
  }
}

/*
Reset to a new game
*/
void
lmem_Newgame (GtkWidget *window, guint callback_action, GtkWidget * widget)
{
  gint j, k, l;
  GSList *image_files;
  char *frame_label;

  click_count=0;
/*  printf("lmem_level=%i\n",lmem_level); */
  asprintf(&frame_label, "%s:  0",indicator[lmem_level]);
  gtk_frame_set_label((GtkFrame *)frame,(gchar *)frame_label);
/*
First remove any card that is turned up
*/
    k=0;
    while(k<NUM){
      if(lmem_state[k]<0){
	l = -lmem_state[k]-match_card;
        lmem_remove_card(button[k],second_pixmap[l]);
	lmem_state[k]=0;
      }
      k++;
    }

/*
Reread the image files
*/
  image_files = lmem_dir_list(NULL,lmem_image_dir);
  if(image_files == NULL) exit(1);
  lmem_pixmaps(window, image_files);

/*
Randomly assign the image files to positions on the grid
*/
    j=0;
    while(j<NUM){

      l = lln_get_random((double) NUM-j);
      k=-1;
      while(l>0){
	k++;
        if(lmem_state[k]==0) l--;
      }
      lmem_state[k] = j+match_card;

      j++;
    }
/*
If the skill level is LITTLE_ONE then we start with all the
cards turned up
*/
    if(lmem_level == LITTLE_ONE){
      k=0;
      while(k<NUM){
        l = lmem_state[k]-match_card;
        flip_pixmap (button[k], default_pixmap[k], second_pixmap[l]);
	default_pixmap[k] = second_pixmap[l];
        second_pixmap[l] = (gpointer) xpm_widget_default(window);
        gtk_object_ref(GTK_OBJECT(second_pixmap[l]));
        k++;
      }
    }
    flipped = 0;
    flipped_btn[0] = -1;
    flipped_btn[1] = -1;
    flipped_btn[match_card-1] = -1;

}

const char *indicator[5] = {"Little One","Beginner","Skilled","Master","Daemon"};
void
lmem_setup()
{
  gint x, z, i, j, k, l;

  GtkWidget *window;
  GtkWidget *box[SIZE];
  GtkWidget *bigbox;
  GtkWidget *vbox;
  GtkWidget *menubar;
  GtkWidget *reset_box;
  GtkWidget *reset_button;
  GtkWidget *label;
  gint pixmap_size = PIXMAP_SIZE;

  lln_set_random();

  /* create a new window */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "The memory game");
  gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);

  gtk_signal_connect (GTK_OBJECT (window), "delete_event",
		      GTK_SIGNAL_FUNC (delete_event), NULL);

  /* sets the border width of the window. */
  gtk_container_border_width (GTK_CONTAINER (window), 10);

  /* we create boxes to pack widgets into */
  bigbox = gtk_vbox_new (FALSE, 0);

  /* put the box into the main window. */
  gtk_container_add (GTK_CONTAINER (window), bigbox);

  get_main_menu (window, &menubar);
  gtk_box_pack_start (GTK_BOX (bigbox), menubar, FALSE, TRUE, 0);
/*  gtk_widget_show (menubar); */
  frame = gtk_frame_new(NULL);
  gtk_box_pack_start (GTK_BOX (bigbox), frame, FALSE, TRUE, 0);
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox);

  k = 0;
  j = 0;
  while (j < SIZE)
    {box[j] = gtk_hbox_new (TRUE, 0);
  /* put the horizontal boxes into the vertical one */
  gtk_box_pack_start (GTK_BOX (vbox), box[j], TRUE, TRUE, 1);

  /* Make a row of buttons */
  i = 0;
  while (i < SIZEP)
    {
      button[k] = gtk_button_new ();
      gtk_widget_set_usize ( button[k], pixmap_size, pixmap_size);

      gtk_box_pack_start (GTK_BOX (box[j]), button[k], TRUE, TRUE, 1);

      /* Now when the button is clicked, we call the "show_number" function
       *        * with a pointer to the button as it's argument */
      gtk_signal_connect (GTK_OBJECT (button[k]), "clicked",
			  GTK_SIGNAL_FUNC (lmem_flip_card), (gpointer) k);
      k++;
      i++;
    }
      j++;
    }

  /* Default to BEGINNER level and same_card = 2*/

    lmem_level = BEGINNER;
    match_card = 2;
    same_card = 2;

    gtk_widget_show_all (window);

    lmem_Newgame (window,0,NULL);

}

void
lln_set_random ()
{
  time_t t1;

  (void) time (&t1);
  /* use time in seconds to set seed */
  srand ((int) t1);
}

int
lln_get_random (double max)
{
  int i;

  i = 1 + (int) (max * rand () / (RAND_MAX + 1.0));
  //1+(rand() % max);

  return (i);
}