[comp.sources.d] mahjongg shuffling fix

pokey@well.UUCP (Jef Poskanzer) (07/02/88)

Attached please find a patch that makes tile shuffling work much much better.
---
Jef

             Jef Poskanzer   jef@rtsg.ee.lbl.gov   ...well!pokey
                            Use only as directed.

- - - - - - - - - -

*** /tmp/,RCSt1a02923	Thu Jun 30 23:05:49 1988
--- mahjongg.c	Thu Jun 30 23:03:02 1988
***************
*** 19,24 ****
--- 19,28 ----
   */
  
  #ifndef lint
+ static char rcsid[] =
+     "@(#) $Header: mahjongg.c,v 1.2 88/06/30 23:02:56 jef Exp $ (LBL)";
+ #endif
+ #ifndef lint
  static char *rcs = "$header$ Copyright 1988 Mark Holm";
  #endif !lint
  
***************
*** 799,805 ****
  {
  int 	i;
  int 	j;
! int	pool[42];
  boolean ok;
  boolean dir;
  char	seed_text[80];
--- 803,810 ----
  {
  int 	i;
  int 	j;
! int	k;
! int	pool[144];
  boolean ok;
  boolean dir;
  char	seed_text[80];
***************
*** 854,861 ****
  	tile_count = 144;
  
  	/* initialize tile pool */
! 	for(i = 0; i < 34; i++) pool[i] = 4;
! 	for(; i < 42; i++) pool[i] = 1;
  
  	/* assign values to each location. Board is built from upper left *
  	 * to lower right, bottom to top. Exception are left tile for row *
--- 859,868 ----
  	tile_count = 144;
  
  	/* initialize tile pool */
! 	for(i = 0; i < 34; i++)
! 	    pool[i*4] = pool[i*4 + 1] = pool[i*4 + 2] = pool[i*4 + 3] = i;
! 	for(; i < 42; i++)
! 	    pool[102 + i] = i;
  
  	/* assign values to each location. Board is built from upper left *
  	 * to lower right, bottom to top. Exception are left tile for row *
***************
*** 868,895 ****
  
  	    if (!oldimage) { /* not repeating last board */
  
! 		/* Randomly seed index into pool. Randomly *
! 		 *  run up or down list until unused tile  *
! 		 *  is found or end of list. If end of     *
! 		 *  list reseed and run in opposite        *
! 		 *  direction in list until unused tile is *
! 		 *  found. If beginning of list found,     *
! 		 *  start over. 			   */
! 
! 		ok = FALSE;
! 		while (ok == FALSE) {
! 			i = RANDOM(41);
! 			/* Up, up, up! */
! 			dir = random()&01;
! 			while ((i < 42 || i >=0) && pool[i] == 0) (dir) ? i++ : i--;
! 			if (i == 42 || i < 0) { /* Thud! Reverse march! */
! 				i = RANDOM(41);
! 				while ((i < 42 || i >= 0) && pool[i] == 0) (dir) ? i-- : i++;
! 			}
! 			if (i == 42 || i < 0) continue; /* Missed! try again */
! 			pool[i]--;
! 			ok = TRUE;
! 		}
  
  		/* all flowers and all seasons */
  
--- 875,886 ----
  
  	    if (!oldimage) { /* not repeating last board */
  
! 		/* randomly choose a tile */
! 		do {
! 		    k = RANDOM(144);
! 		    i = pool[k];
! 		} while ( i == -1 );
! 		pool[k] = -1;
  
  		/* all flowers and all seasons */