-+-+-+-+-+-+-+-+ START OF PART 2 -+-+-+-+-+-+-+-+ X for(cnt=1;cnt<=lvl*FAC1;cnt++) X `7B X do X `7B X r`5Bx`5D=RANDOM1()%4+1; X `7D X while(r`5Bx-1`5D==r`5Bx`5D); X df=abs(r`5Bx-2`5D-r`5Bx-1`5D); X switch(df) X `7B X case(2): if(r`5Bx-2`5D ==1 `7C`7C r`5Bx-1`5D ==1) X `7B X r`5Bx`5D=RANDOM1()%2; X if(r`5Bx`5D==0) X r`5Bx`5D=2; X else X r`5Bx`5D=4; X `7D X else X `7B X r`5Bx`5D=RANDOM1()%2; X if(r`5Bx`5D==0) X r`5Bx`5D=1; X else X r`5Bx`5D=3; X `7D X break; X case(3): r`5Bx`5D=RANDOM1()%2+2; X break; X `7D X do_switch(rw,r`5Bx`5D); X x++; X `7D X `7D X while(test_row(rw) == 1); X`7D X X Xdo_switch(rw,mv)`09/* switch the numbers for the player */ Xint *rw`5B`5D; Xint mv; X`7B X switch(mv) X `7B X case(1): switch_number(rw,1,2,3,4); X break; X case(2): switch_number(rw,2,3,4,5); X break; X case(3): switch_number(rw,5,6,7,8); X break; X case(4): switch_number(rw,6,7,8,9); X break; X `7D X`7D X X Xswitch_number(rwx,n1,n2,n3,n4)`09/* switch the numbers */ Xint *rwx`5B`5D,n1,n2,n3,n4; X`7B X int *temp; X temp = rwx`5Bn1`5D; X rwx`5Bn1`5D = rwx`5Bn4`5D; X rwx`5Bn4`5D = temp; X temp = rwx`5Bn2`5D; X rwx`5Bn2`5D = rwx`5Bn3`5D; X rwx`5Bn3`5D = temp; X`7D $ CALL UNPACK SWITCH.C;1 943612219 $ create 'f' X/* X** Written by Stephan Dasia. X** X** permission is granted to freely distribute this code provided that you: X** X** 1) don't charge for it X** 2) leave my name and header on it X** 3) clearly document your changes and place your name on them X** 4) and please send the changes to me X** X*/ X/* Shuffle: screen()`09The game screen`09`09`09`09`09*/ X/*`09`09`09`09`09`09`09`09`09*/ X/* help()`09This procedure is the online help`09`09*/ X/*`09`09`09`09`09`09`09`09`09*/ X X#include "shuffle.h" X X Xscreen() /* game screen */ X`7B X move(8,13); X printw("+-----------2-----------+ "); X printw(" +-----------4-----------+\n"); X printw(" `7C +---2---+ `7C "); X printw(" `7C +---4---+ `7C\n"); X printw(" +---+ +---+ +---+ +---+ +---+"); X printw(" +---+ +---+ +---+ +---+\n"); X printw(" `7C `7C `7C `7C `7C `7C `7C `7C `7C `7C"); X printw(" `7C `7C `7C `7C `7C `7C `7C `7C\n"); X printw(" +---+ +---+ +---+ +---+ +---+"); X printw(" +---+ +---+ +---+ +---+\n"); X printw(" `7C +---1---+ `7C "); X printw(" `7C +---3---+ `7C\n"); X printw(" +-----------1-----------+"); X printw(" +-----------3-----------+\n"); X move(17,0); X printw("+-----------------+---------------------+-------------------"); X printw("-------------+\n"); X printw("`7C LEVEL: `7C `7C"); X printw(" `7C\n"); X printw("+-----------------+---------------------+-------------------"); X printw("-------------+\n"); X`7D X X Xhelp()`09`09`09/* help screen */ X`7B X move(0,30); X printw("S H U F F L E\n"); X move(1,30); X printw("-------------"); X move(3,1); X printw("\tThe problem in this game is to get the"); X printw(" numbers from 1 to 9\n"); X printw("\tin the correct order using keys "); X printw("1 to 4.\n"); X printw("\tAvailable commands are :\n"); X printw("\t1 : to switch the boxes one to four.\n"); X printw("\t2 : to switch the boxes two to five.\n"); X printw("\t3 : to switch the boxes five to eight.\n"); X printw("\t4 : to switch the boxes six to nine.\n"); X printw("\tq : to end the game.\n"); X printw("\t?,h : to get this screen.\n"); X printw("\t`5ER : to redraw screen\n"); X move(16,34); X printw("press \n"); X`7D $ CALL UNPACK VISUAL.C;1 1900236529 $ create 'f' X/* Random number generators: X * X * rnd_init (unsigned seed)`20 X *`09`09`09: initializes the generator X * X * rnd_i ()`09`09: returns positive integers `5B0,0x7fffffff`5D X * rnd_u ()`09`09: returns unsigned's `5B0,0xffffffff`5D X * rnd_ri (long n)`09: returns positive integers `5B0,n-1`5D X * rnd_01d ()`09`09: returns doubles`09 `5B0.0,1.0) X *`09`09`09 Note: ")" is no typo - rnd_01d will not return a 1.0, X * but can return the next smaller FP number. X * rnd_ned (double lam): returns neg. exponential distributed doubles `5B0. V0,+inf) X * rnd_nedi (double rt): same with lam = 1/rt - used to save divides X * X * Algorithm M as describes in Knuth's "Art of Computer Programming", Vol 2 V. 1969 X * is used with a linear congruential generator (to get a good uniform X * distribution) that is permuted with a Fibonacci additive congruential X * generator to get good independence. X * X * Bit, byte, and word distributions were extensively tested and pass X * Chi-squared test near perfect scores (>7E8 numbers tested, Uniformity X * assumption holds with probability > 0.999) X * X * Run-up tests for on 7E8 numbers confirm independence with X * probability > 0.97. X * X * Plotting random points in 2d reveals no apparent structure. X * X * Autocorrelation on sequences of 5E5 numbers (A(i) = SUM X(n)*X(n-i), i=1 V..512) X * results in no obvious structure (A(i) `7E const). X * X * On a SUN 3/60, rnd_u() takes about 19.4 usec per call, which is about 44 V% X * slower than Berkeley's random() (13.5 usec/call). X * X * Except for speed and memory requirements, this generator outperforms X * random() for all tests. (random() scored rather low on uniformity tests, X * while independence test differences were less dramatic). X * X * Thanks to M.Mauldin, H.Walker, J.Saxe and M.Molloy for inspiration & hel Vp. X * X * (c) Copyright 1988 by A. Nowatzyk X * X */ X X/* LC-parameter selection follows recommendations in`20 X * "Handbook of Mathematical Functions" by Abramowitz & Stegun 10th, edi. X */ X#define LC_A 66049`09`09 /* = 251`5E2, `7E= sqrt(2`5E32)`09`09`09*/ X#define LC_C 3907864577`09`09 /* result of a long trial & error series V */ X X#define Xrnd(x) (x * LC_A + LC_C) /* the LC polynomial`09`09`09*/ X`09`09`09 Xstatic unsigned long Fib`5B55`5D;`09 /* will use X(n) = X(n-55) - X(n-24) V`09*/ Xstatic int Fib_ind;`09`09 /* current index in circular buffer`09`09*/ Xstatic unsigned long Xrnd_var;`09 /* LCA - recurrence variable`09`09*/ Xstatic unsigned long auxtab`5B256`5D; /* temporal permutation table`09`09* V/ Xstatic unsigned long prmtab`5B64`5D = `7B /* spatial permutation table`09`09 V*/ X 0xffffffff, 0x00000000, 0x00000000, 0x00000000, /* 3210 */ X 0x0000ffff, 0x00ff0000, 0x00000000, 0xff000000, /* 2310 */ X 0xff0000ff, 0x0000ff00, 0x00000000, 0x00ff0000, /* 3120 */ X 0x00ff00ff, 0x00000000, 0xff00ff00, 0x00000000, /* 1230 */ X X 0xffff0000, 0x000000ff, 0x00000000, 0x0000ff00, /* 3201 */ X 0x00000000, 0x00ff00ff, 0x00000000, 0xff00ff00, /* 2301 */ X 0xff000000, 0x00000000, 0x000000ff, 0x00ffff00, /* 3102 */ X 0x00000000, 0x00000000, 0x00000000, 0xffffffff, /* 2103 */ X X 0xff00ff00, 0x00000000, 0x00ff00ff, 0x00000000, /* 3012 */ X 0x0000ff00, 0x00000000, 0x00ff0000, 0xff0000ff, /* 2013 */ X 0x00000000, 0x00000000, 0xffffffff, 0x00000000, /* 1032 */ X 0x00000000, 0x0000ff00, 0xffff0000, 0x000000ff, /* 1023 */ X X 0x00000000, 0xffffffff, 0x00000000, 0x00000000, /* 0321 */ X 0x00ffff00, 0xff000000, 0x00000000, 0x000000ff, /* 0213 */ X 0x00000000, 0xff000000, 0x0000ffff, 0x00ff0000, /* 0132 */ X 0x00000000, 0xff00ff00, 0x00000000, 0x00ff00ff /* 0123 */ X`7D; X Xunion hack `7B`09`09`09 /* used to access doubles as unsigneds`09*/ X double d; X unsigned long u`5B2`5D; X`7D; X Xstatic union hack man;`09`09 /* mantissa bit vector`09`09`09*/ X Xrnd_init (seed)`09`09`09 /* modified: seed 0-31 use precomputed stuff */ X unsigned seed; X`7B X register unsigned long u; X register int i; X double x, y; X union hack t; X X static unsigned seed_tab`5B32`5D = `7B X`09`090xbdcc47e5, 0x54aea45d, 0xec0df859, 0xda84637b, X`09`090xc8c6cb4f, 0x35574b01, 0x28260b7d, 0x0d07fdbf, X`09`090x9faaeeb0, 0x613dd169, 0x5ce2d818, 0x85b9e706, X`09`090xab2469db, 0xda02b0dc, 0x45c60d6e, 0xffe49d10, X`09`090x7224fea3, 0xf9684fc9, 0xfc7ee074, 0x326ce92a, X`09`090x366d13b5, 0x17aaa731, 0xeb83a675, 0x7781cb32, X`09`090x4ec7c92d, 0x7f187521, 0x2cf346b4, 0xad13310f, X`09`090xb89cff2b, 0x12164de1, 0xa865168d, 0x32b56cdf `7D; X X if (seed < 32) X`09u = seed_tab`5Bseed`5D; X else X`09u = seed `5E seed_tab`5Bseed & 31`5D; X X for (i = 55; i--;)`09`09 /* set up Fibonacci additive congruential`09 V*/ X`09Fib`5Bi`5D = u = Xrnd(u); X X for (i = 256; i--;) X`09auxtab`5Bi`5D = u = Xrnd(u); X X Fib_ind = u % 55;`09`09 /* select a starting point`09`09`09*/ X X Xrnd_var = u; X X if (sizeof(x) != 2 * sizeof(unsigned long)) `7B X`09x = 0.0; X`09y = 1.0; X`09y /= x;`09`09`09 /*** intentional divide by 0: rnd_01d will X`09`09`09`09`09 not work because a double doesn't fit X`09`09`09`09`09 in 2 unsigned longs on your machine! ***/ X `7D; X X x = 1.0; X y = 0.5; X do `7B`09`09`09 /* find largest fp-number < 2.0`09`09*/ X`09t.d = x; X`09x += y; X`09y *= 0.5; X `7D while (x != t.d && x < 2.0); X X man.d = 1.0; X man.u`5B0`5D `5E= t.u`5B0`5D; X man.u`5B1`5D `5E= t.u`5B1`5D;`09`09 /* man is now 1 for each mantissa V bit`09*/ X`7D X Xlong rnd_i () X/* X * returns a positive, uniformly distributed random number in `5B0,0x7ffffff Vf`5D X */ X`7B`20 X register unsigned long i, j, *t = Fib; X X i = Fib_ind; X j = t`5Bi`5D;`09`09`09`09 /* = X(n-55) */ X j -= (i >= 24) ? t`5Bi - 24`5D : t`5Bi + 21`5D; /* = X(n-24) */ X t`5Bi`5D = j; X if (++i >= 55) i = 0; X Fib_ind = i; X X t = &auxtab`5B(j >> 24) & 0xff`5D; X i = *t; X Xrnd_var = *t = Xrnd(Xrnd_var); X t = &prmtab`5Bj & 0x3c`5D; X X j = *t++ & i; X j `7C= *t++ & ((i << 24) `7C ((i >> 8) & 0x00ffffff)); X j `7C= *t++ & ((i << 16) `7C ((i >> 16) & 0x0000ffff)); X j `7C= *t & ((i << 8) `7C ((i >> 24) & 0x000000ff)); X `20 X return j & 0x7fffffff; X`7D X Xunsigned long rnd_u () X/* X * same as rnd_i, but gives full 32 bit range X */ X`7B`20 X register unsigned long i, j, *t = Fib; X X i = Fib_ind; X j = t`5Bi`5D;`09`09`09`09 /* = X(n-55) */ X j -= (i >= 24) ? t`5Bi - 24`5D : t`5Bi + 21`5D; /* = X(n-24) */ X t`5Bi`5D = j; X if (++i >= 55) i = 0; X Fib_ind = i; X X t = &auxtab`5B(j >> 24) & 0xff`5D; X i = *t; X Xrnd_var = *t = Xrnd(Xrnd_var); X t = &prmtab`5Bj & 0x3c`5D; X X j = *t++ & i; X j `7C= *t++ & ((i << 24) `7C ((i >> 8) & 0x00ffffff)); X j `7C= *t++ & ((i << 16) `7C ((i >> 16) & 0x0000ffff)); X j `7C= *t & ((i << 8) `7C ((i >> 24) & 0x000000ff)); X `20 X return j; X`7D X Xlong rnd_ri (rng) X long rng; X/* X * randint: Return a random integer in a given Range `5B0..rng-1`5D X * Note: 0 < rng X */ X`7B X register unsigned long r, a; X X do `7B X`09r = rnd_i(); X`09a = (r / rng) + 1; X`09a *= rng; X `7D while (a >= 0x7fffffff); X `20 X a--; X return a - r; X`7D X Xdouble rnd_01d () X/* X * returns a uniformly distributed double in the range of `5B0..1) X * or 0.0 <= rnd_01d() < 1.0 to be precise X * X * Note: this code assumes that 2 'unsigned long's can hold a 'double' X * (works on SUN-3's, SUN-4's, MIPS, VAXen, IBM RT's) X */ X`7B X union hack t; X X t.d = 1.0; X X t.u`5B0`5D `7C= rnd_u() & man.u`5B0`5D;`09 /* munch in 1st part * V/ X t.u`5B1`5D `7C= rnd_u() & man.u`5B1`5D;`09 /* munch in 2nd part * V/ X X return t.d - 1.0; X`7D X Xdouble rnd_ned (lam) X double lam; X/* X * returns a neg. exponential distributed double in the range of `5B0..+infi Vnity) X * or 0.0 <= rnd_neg() < +infinity to be precise X * X * Note: this code assumes that 2 'unsigned long's can hold a 'double' X * it also assumes that 'log()' behaves as advertised. X * X */ X`7B X union hack t; X X t.d = 1.0; X X t.u`5B0`5D `7C= rnd_u() & man.u`5B0`5D;`09 /* munch in 1st part * V/ X t.u`5B1`5D `7C= rnd_u() & man.u`5B1`5D;`09 /* munch in 2nd part * V/ X X return -log(2.0 - t.d) / lam; X`7D X Xdouble rnd_nedi (lam) X double lam; X/* X * same as 'rnd_ned' called with 1/lam X * X * This is used to save a divide operation in some places X * X */ X`7B X union hack t; X X t.d = 1.0; X X t.u`5B0`5D `7C= rnd_u() & man.u`5B0`5D;`09 /* munch in 1st part * V/ X t.u`5B1`5D `7C= rnd_u() & man.u`5B1`5D;`09 /* munch in 2nd part * V/ X X return -log(2.0 - t.d) * lam; X`7D $ CALL UNPACK XRAND.C;1 1829458228 $ v=f$verify(v) $ EXIT