[cairo-commit] cairo-demo/tsetse ChangeLog, 1.1, 1.2 tsetse.c, 1.2, 1.3

Carl Worth commit at pdx.freedesktop.org
Sat Feb 19 13:06:24 PST 2005


Committed by: cworth

Update of /cvs/cairo/cairo-demo/tsetse
In directory gabe:/tmp/cvs-serv21114

Modified Files:
	ChangeLog tsetse.c 
Log Message:

        * tsetse.c (deal): Don't deal from a deck with no cards.
        (new_game): Add new function to start a new game, (and bind to
        'N').


Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/tsetse/ChangeLog,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- ChangeLog	19 Feb 2005 18:39:23 -0000	1.1
+++ ChangeLog	19 Feb 2005 21:06:21 -0000	1.2
@@ -1,3 +1,9 @@
+2005-02-19  Carl Worth  <cworth at cworth.org>
+
+	* tsetse.c (deal): Don't deal from a deck with no cards.
+	(new_game): Add new function to start a new game, (and bind to
+	'N').
+
 2005-02-19  Keith Packard  <keithp at keithp.com>
 
 	* tsetse.c:

Index: tsetse.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/tsetse/tsetse.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- tsetse.c	19 Feb 2005 18:39:23 -0000	1.2
+++ tsetse.c	19 Feb 2005 21:06:21 -0000	1.3
@@ -26,6 +26,8 @@
 #include <cairo.h>
 #include <cairo-xlib.h>
 
+#include <assert.h>
+
 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
 
 #define SHRINK(cr, x) cairo_translate (cr, (1-(x))/2.0, (1-(x))/2.0); cairo_scale (cr, (x), (x))
@@ -45,9 +47,10 @@
     shape_t shape;
 } card_t;
 
+#define DECK_MAX_CARDS (NUM_NUMBERS * NUM_COLORS * NUM_SHADINGS * NUM_SHAPES)
 typedef struct deck {
     int num_cards;
-    card_t cards[NUM_NUMBERS * NUM_COLORS * NUM_SHADINGS * NUM_SHAPES];
+    card_t cards[DECK_MAX_CARDS];
 } deck_t;
 
 typedef struct slot {
@@ -105,18 +108,21 @@
 static void win_handle_events(win_t *win);
 static void win_print_help(win_t *win);
 
-static int quit_cb(win_t *win);
+static int new_game_cb(win_t *win);
 static int reshuffle_cb(win_t *win);
+static int quit_cb(win_t *win);
 
 static const callback_doc_t callback_doc[] = {
-    { quit_cb,		"Exit the program" },
+    { new_game_cb,      "New game" },
     { reshuffle_cb,	"Return dealt cards to deck and reshuffle" },
+    { quit_cb,		"Exit the program" }
 };
 
 static key_binding_t key_binding[] = {
     /* Keysym, Alias, Keycode, callback */
-    { "Q",	0, 0, quit_cb },
-    { "R",      0, 0, reshuffle_cb }
+    { "N",      0, 0, new_game_cb },
+    { "R",      0, 0, reshuffle_cb },
+    { "Q",	0, 0, quit_cb }
 };
 
 int
@@ -418,8 +424,12 @@
     int i, r;
     card_t tmp;
 
+    assert (deck->num_cards <= DECK_MAX_CARDS);
+
     for (i=deck->num_cards - 1; i>=0; i--) {
 	r = (int) i * (rand() / (RAND_MAX + 1.0));
+	assert (r >= 0);
+	assert (r <= i);
 	tmp = deck->cards[i];
 	deck->cards[i] = deck->cards[r];
 	deck->cards[r] = tmp;
@@ -470,13 +480,24 @@
 
     for (i=0; i < board->num_slots; i++)
 	if (! board->slots[i].has_card) {
-	    board->slots[i].card = deck->cards[deck->num_cards-- - 1];
-	    board->slots[i].has_card = 1;
+	    if (deck->num_cards > 0) {
+		board->slots[i].card = deck->cards[deck->num_cards-- -1];
+		board->slots[i].has_card = 1;
+	    }
 	}
 
     board->needs_deal = 0;
 }
 
+/* Begin a new game */
+static void
+new_game (deck_t *deck, board_t *board)
+{
+    deck_init (deck);
+    board_init (board);
+    deal (deck, board);
+}
+
 /* Return the dealt cards to the deck, reshuffle, and deal again. */
 static void
 reshuffle (deck_t *deck, board_t *board)
@@ -484,9 +505,11 @@
     int i;
 
     for (i=0; i < board->num_slots; i++) {
-	    deck->cards[deck->num_cards++ - 1] = board->slots[i].card;
+	if (board->slots[i].has_card) {
+	    deck->cards[++deck->num_cards - 1] = board->slots[i].card;
 	    board->slots[i].has_card = 0;
 	    board->slots[i].selected = 0;
+	}
     }
 
     deck_shuffle (deck);
@@ -500,9 +523,7 @@
     Window root;
     XGCValues gcv;
 
-    deck_init (&win->deck);
-    board_init (&win->board);
-    deal (&win->deck, &win->board);
+    new_game (&win->deck, &win->board);
 
     win->dpy = dpy;
     win->width = 600;
@@ -702,9 +723,12 @@
 /* Callbacks */
 
 static int
-quit_cb (win_t *win)
+new_game_cb (win_t *win)
 {
-    return 1;
+    new_game (&win->deck, &win->board);
+    win->needs_refresh = 1;
+
+    return 0;
 }
 
 static int
@@ -715,3 +739,9 @@
 
     return 0;
 }
+
+static int
+quit_cb (win_t *win)
+{
+    return 1;
+}




More information about the cairo-commit mailing list