# To unbundle, sh this file echo Makefile 1>&2 cat >Makefile <<'End of Makefile' O_FILES = iqtester.o usage.o help.o CFLAGS = CC = gcc iqtester: $(O_FILES) $(CC) $(CFLAGS) -o iqtester $(O_FILES) End of Makefile echo Test 1>&2 cat >Test <<'End of Test' # set n = 1000000 set s = 200 # set a = 0 set b = 0 echo $a $b iqtester -s $s -n $n -m $a $b > games$a$b.txt # grep new games$a$b.txt /dev/null # set a = 1 set b = 0 echo $a $b iqtester -s $s -n $n -m $a $b > games$a$b.txt # grep new games$a$b.txt /dev/null # set a = 2 set b = 0 echo $a $b iqtester -s $s -n $n -m $a $b > games$a$b.txt # grep new games$a$b.txt /dev/null # set s = 12345 set a = 2 set b = 1 echo $a $b iqtester -s $s -n $n -m $a $b > games$a$b.txt # grep new games$a$b.txt /dev/null # Count End of Test echo help.c 1>&2 cat >help.c <<'End of help.c' /* * help - give program help */ #include help(argv) char** argv; { fprintf(stderr, "%s: HELP:\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " iqtester - simulate an IQ Tester at Po'Folks Restaurant\n"); fprintf(stderr, "\n"); fprintf(stderr, " The board is numbered like this:\n"); fprintf(stderr, "\n"); fprintf(stderr, " 00\n"); fprintf(stderr, " 10 11\n"); fprintf(stderr, " 20 21 22\n"); fprintf(stderr, " 30 31 32 33\n"); fprintf(stderr, " 40 41 42 43 44\n"); fprintf(stderr, "\n"); fprintf(stderr, " You begin with a piece in every hole except one.\n"); fprintf(stderr, " You remove pieces with \"jumps\" like in checkers.\n"); fprintf(stderr, " A win is when one piece remains. A \"genius\" win\n"); fprintf(stderr, " is when the last pices is n the original empty square.\n"); fprintf(stderr, "\n"); fprintf(stderr, " example usage:\n"); fprintf(stderr, "\n"); fprintf(stderr, " iqtester -n 300 -s 442 -m 0 1 > games01.txt\n"); fprintf(stderr, "\n"); fprintf(stderr, " meaning:\n"); fprintf(stderr, "\n"); fprintf(stderr, " Run 300 games of the IQ Tester simulator,\n"); fprintf(stderr, " making random choices of jumps. Use random\n"); fprintf(stderr, " number seed 442. Start with empty space 0,1\n"); fprintf(stderr, " (0th row, 1st column). If game is won print\n"); fprintf(stderr, " out the moves.\n"); fprintf(stderr, "\n"); } /* end of help */ End of help.c echo iqtester.c 1>&2 cat >iqtester.c <<'End of iqtester.c' /** ** iqtester - simulate "iq tester" puzzle ** ** CREATED: 02007.05.06 ABS copied from t_filter ** **/ #include "iqtester.h" main(argc, argv) int argc; char** argv; { /* declare functions */ double atof(); /* declare local variables */ int i, j, k, n, ts, ii, jj; int p0, p1, p2; int numPegs; int lowestPegs; int nowStuck; int tri_hist[5][5][16]; /* tri_hist[row][column][timestep]; */ int timestep; int sum, actual_games; /* random stuff */ int r0, r; int m = RAND_MAX; float rf; char input_line[255]; /* initialize global variables (including parameters) */ defaultParms(); Debug = FALSE; NumGames = 10; Seed = 0; M0 = 4; M1 = 2; sum = 0; parseArgs(argc, argv); /* debug writes */ if (Debug != FALSE) { for (i=0; i<=35; i++) { fprintf(stderr, "%s: DEBUG: i = %d, Iq = %d %d %d %d %d %d\n", argv[0], i, Iq[i][0][0], Iq[i][1][0], Iq[i][0][1], Iq[i][1][1], Iq[i][0][2], Iq[i][1][2]); } } /* initialize local variables */ nowStuck = FALSE; numPegs = 14; timestep = 0; lowestPegs = 14; { /* this should be a function */ srandom((unsigned)Seed); } /* read */ /* fscanf(stdin, "%d", &IntGlobal); */ /* loop and write */ for (n = 0; n < NumGames; n++) { actual_games = n; defaultParms(); /****************************************** fprintf(stdout, "=====================\n"); fprintf(stdout, "game number %d\n", n); fprintf(stdout, "=====================\n"); ******************************************/ numPegs = 14; timestep = 0; #ifdef NEVER_DEFINED writeTri(); #endif nowStuck = FALSE; while (numPegs > 1 && nowStuck == FALSE) { { /* this should be a function */ r0 = random(); rf = (float)r0/(float)m; r = (int)(36*rf); /* 36 */ } p0 = Tri[Iq[r][0][0]][Iq[r][1][0]]; p1 = Tri[Iq[r][0][1]][Iq[r][1][1]]; p2 = Tri[Iq[r][0][2]][Iq[r][1][2]]; if (Debug != FALSE) { fprintf(stdout, "%s: DEBUG: next guess is %d, Iq = %d %d %d %d %d %d; pattern is %d %d %d ", argv[0], r, Iq[r][0][0], Iq[r][1][0], Iq[r][0][1], Iq[r][1][1], Iq[r][0][2], Iq[r][1][2], p0, p1, p2); } if (p0 == 1 && p1 == 1 && p2 == 0) { /* jump OK */ if (Debug != FALSE) { fprintf(stdout, "jump OK\n"); } /* copy timestep */ #ifdef PARANOID fprintf(stdout, "+++++++++++++++ timestep = %d \n", timestep); #endif for (ii = 0; ii < 5; ii++) { for (jj = 0; jj < 5; jj++) { tri_hist[ii][jj][timestep] = Tri[ii][jj]; #ifdef PARANOID fprintf(stdout, "tri_hist[%d][%d][timestep] = %d\n", ii, jj, tri_hist[ii][jj][timestep]); #endif } } /* jump */ Tri[Iq[r][0][0]][Iq[r][1][0]] = 0; Tri[Iq[r][0][1]][Iq[r][1][1]] = 0; Tri[Iq[r][0][2]][Iq[r][1][2]] = 1; numPegs--; timestep++; #ifdef NEVER_DEFINED writeTri(); fprintf(stdout, "---------------------\n"); fprintf(stdout, "number of pegs = %d\n", numPegs); fprintf(stdout, "---------------------\n"); #endif nowStuck = TRUE; /* test for ANY way to move */ for (r = 0; r < 36; r++) { p0 = Tri[Iq[r][0][0]][Iq[r][1][0]]; p1 = Tri[Iq[r][0][1]][Iq[r][1][1]]; p2 = Tri[Iq[r][0][2]][Iq[r][1][2]]; if ((p0 == 1) && (p1 == 1) && (p2 == 0)) { nowStuck = FALSE; } } if (nowStuck != FALSE) { /* fprintf(stdout, "now stuck with %d pegs left\n", numPegs); */ if (numPegs < lowestPegs || numPegs == 1) { lowestPegs = numPegs; if (Tri[M0][M1] == 1 && numPegs == 1) { fprintf(stdout, "=====================\n"); fprintf(stdout, "game number %d\n", n); fprintf(stdout, "=====================\n"); fprintf(stdout, "a new genius record: 1 peg left\n"); /* n = NumGames + 1; */ for (ts = 0; ts < timestep; ts++) { /* write a move */ for (i=0; i<=4; i++) { for (k=0; k<=4 - i; k++) { fprintf(stdout, " "); } for (j=0; j<=i; j++) { fprintf(stdout, "%d ", tri_hist[i][j][ts]); } fprintf(stdout, "\n"); } fprintf(stdout, "---------------------\n"); } writeTri(); n = NumGames + 1; } else if (numPegs > 1) { fprintf(stdout, "a new record: %d pegs left\n", numPegs); } else { fprintf(stdout, "a new record or tie: 1 peg left\n"); for (ts = 0; ts < timestep; ts++) { /* write a move */ for (i=0; i<=4; i++) { for (k=0; k<=4 - i; k++) { fprintf(stdout, " "); } for (j=0; j<=i; j++) { fprintf(stdout, "%d ", tri_hist[i][j][ts]); } fprintf(stdout, "\n"); } fprintf(stdout, "---------------------\n"); } writeTri(); } } } } else { /* jump not OK */ if (Debug != FALSE) { fprintf(stdout, "jump not OK\n"); } } } /* end while */ sum = sum + numPegs; } /* end for n */ fprintf(stdout, "---------------------\n"); fprintf(stdout, "stats: average attempt left %f pegs\n", (float)sum/(float)actual_games); } /* end main */ /* * defaultParms - initialize global variables */ defaultParms() { int i, j, k; for (i=0; i<=4; i++) { for (j=0; j<=4; j++) { Tri[i][j] = 9; } } for (i=0; i<=4; i++) { for (j=0; j<=i; j++) { Tri[i][j] = 1; } } if (Debug != FALSE) { for (i=0; i<=4; i++) { for (j=0; j<=4; j++) { fprintf(stdout, "DEBUG: Tri[%d][%d] = %d\n", i, j, Tri[i][j]); } } } Tri[M0][M1] = 0; /* the hole */ /* it would be better to read this from a file */ for (i=0; i<=35; i++) { for (j=0; j<=1; j++) { for (k=0; k<=2; k++) { Iq[i][j][k] = 0; } } } /* 1. A1 B1 C1 */ Iq[0][0][0] = 1 - 1; /* A */ Iq[0][1][0] = 1 - 1; /* 1 */ Iq[0][0][1] = 2 - 1; /* B */ Iq[0][1][1] = 1 - 1; /* 1 */ Iq[0][0][2] = 3 - 1; /* C */ Iq[0][1][2] = 1 - 1; /* 1 */ /* 2. A1 B2 C3 */ Iq[1][0][0] = 1 - 1; /* A */ Iq[1][1][0] = 1 - 1; /* 1 */ Iq[1][0][1] = 2 - 1; /* B */ Iq[1][1][1] = 2 - 1; /* 2 */ Iq[1][0][2] = 3 - 1; /* C */ Iq[1][1][2] = 3 - 1; /* 3 */ /* 3. B1 C1 D1 */ Iq[2][0][0] = 2 - 1; /* B */ Iq[2][1][0] = 1 - 1; /* 1 */ Iq[2][0][1] = 3 - 1; /* C */ Iq[2][1][1] = 1 - 1; /* 1 */ Iq[2][0][2] = 4 - 1; /* D */ Iq[2][1][2] = 1 - 1; /* 1 */ /* 4. B1 C2 D3 */ Iq[3][0][0] = 2 - 1; /* B */ Iq[3][1][0] = 1 - 1; /* 1 */ Iq[3][0][1] = 3 - 1; /* C */ Iq[3][1][1] = 2 - 1; /* 2 */ Iq[3][0][2] = 4 - 1; /* D */ Iq[3][1][2] = 3 - 1; /* 3 */ /* 5. B2 C2 D2 */ Iq[4][0][0] = 2 - 1; /* B */ Iq[4][1][0] = 2 - 1; /* 2 */ Iq[4][0][1] = 3 - 1; /* C */ Iq[4][1][1] = 2 - 1; /* 2 */ Iq[4][0][2] = 4 - 1; /* D */ Iq[4][1][2] = 2 - 1; /* 2 */ /* 6. B2 C3 D4 */ Iq[5][0][0] = 2 - 1; /* B */ Iq[5][1][0] = 2 - 1; /* 2 */ Iq[5][0][1] = 3 - 1; /* C */ Iq[5][1][1] = 3 - 1; /* 3 */ Iq[5][0][2] = 4 - 1; /* D */ Iq[5][1][2] = 4 - 1; /* 4 */ /* 7. C1 D1 E1 */ Iq[6][0][0] = 3 - 1; /* C */ Iq[6][1][0] = 1 - 1; /* 1 */ Iq[6][0][1] = 4 - 1; /* D */ Iq[6][1][1] = 1 - 1; /* 1 */ Iq[6][0][2] = 5 - 1; /* E */ Iq[6][1][2] = 1 - 1; /* 1 */ /* 8. C1 D2 E3 */ Iq[7][0][0] = 3 - 1; /* C */ Iq[7][1][0] = 1 - 1; /* 1 */ Iq[7][0][1] = 4 - 1; /* D */ Iq[7][1][1] = 2 - 1; /* 2 */ Iq[7][0][2] = 5 - 1; /* E */ Iq[7][1][2] = 3 - 1; /* 3 */ /* 9. C1 C2 C3 */ Iq[8][0][0] = 3 - 1; /* C */ Iq[8][1][0] = 1 - 1; /* 1 */ Iq[8][0][1] = 3 - 1; /* C */ Iq[8][1][1] = 2 - 1; /* 2 */ Iq[8][0][2] = 3 - 1; /* C */ Iq[8][1][2] = 3 - 1; /* 3 */ /* 10. C2 D2 E2 */ Iq[9][0][0] = 3 - 1; /* C */ Iq[9][1][0] = 2 - 1; /* 2 */ Iq[9][0][1] = 4 - 1; /* D */ Iq[9][1][1] = 2 - 1; /* 2 */ Iq[9][0][2] = 5 - 1; /* E */ Iq[9][1][2] = 2 - 1; /* 2 */ /* 11. C2 D3 E4 */ Iq[10][0][0] = 3 - 1; /* C */ Iq[10][1][0] = 2 - 1; /* 2 */ Iq[10][0][1] = 4 - 1; /* D */ Iq[10][1][1] = 3 - 1; /* 3 */ Iq[10][0][2] = 5 - 1; /* E */ Iq[10][1][2] = 4 - 1; /* 4 */ /* 12. C3 D3 E3 */ Iq[11][0][0] = 3 - 1; /* C */ Iq[11][1][0] = 3 - 1; /* 3 */ Iq[11][0][1] = 4 - 1; /* D */ Iq[11][1][1] = 3 - 1; /* 3 */ Iq[11][0][2] = 5 - 1; /* E */ Iq[11][1][2] = 3 - 1; /* 3 */ /* 13. C3 D4 E5 */ Iq[12][0][0] = 3 - 1; /* C */ Iq[12][1][0] = 3 - 1; /* 3 */ Iq[12][0][1] = 4 - 1; /* D */ Iq[12][1][1] = 4 - 1; /* 4 */ Iq[12][0][2] = 5 - 1; /* E */ Iq[12][1][2] = 5 - 1; /* 5 */ /* 14. D1 D2 D3 */ Iq[13][0][0] = 4 - 1; /* D */ Iq[13][1][0] = 1 - 1; /* 1 */ Iq[13][0][1] = 4 - 1; /* D */ Iq[13][1][1] = 2 - 1; /* 2 */ Iq[13][0][2] = 4 - 1; /* D */ Iq[13][1][2] = 3 - 1; /* 3 */ /* 15. D2 D3 D4 */ Iq[14][0][0] = 4 - 1; /* D */ Iq[14][1][0] = 2 - 1; /* 2 */ Iq[14][0][1] = 4 - 1; /* D */ Iq[14][1][1] = 3 - 1; /* 3 */ Iq[14][0][2] = 4 - 1; /* D */ Iq[14][1][2] = 4 - 1; /* 4 */ /* 16. E1 E2 E3 */ Iq[15][0][0] = 5 - 1; /* E */ Iq[15][1][0] = 1 - 1; /* 1 */ Iq[15][0][1] = 5 - 1; /* E */ Iq[15][1][1] = 2 - 1; /* 2 */ Iq[15][0][2] = 5 - 1; /* E */ Iq[15][1][2] = 3 - 1; /* 3 */ /* 17. E2 E3 E4 */ Iq[16][0][0] = 5 - 1; /* E */ Iq[16][1][0] = 2 - 1; /* 2 */ Iq[16][0][1] = 5 - 1; /* E */ Iq[16][1][1] = 3 - 1; /* 3 */ Iq[16][0][2] = 5 - 1; /* E */ Iq[16][1][2] = 4 - 1; /* 4 */ /* 18. E3 E4 E5 */ Iq[17][0][0] = 5 - 1; /* E */ Iq[17][1][0] = 3 - 1; /* 3 */ Iq[17][0][1] = 5 - 1; /* E */ Iq[17][1][1] = 4 - 1; /* 4 */ Iq[17][0][2] = 5 - 1; /* E */ Iq[17][1][2] = 5 - 1; /* 5 */ /* reflect */ for (i=18; i<=35; i++) { for (j=0; j<=1; j++) { for (k=0; k<=2; k++) { Iq[i][j][k] = Iq[i -18][j][2 - k]; } } } } /* end defaultParms() */ /* * parseArgs - parse arguments into global variables */ parseArgs(argc, argv) int argc; char** argv; { int i; for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'D' : Debug = TRUE; break; case 'h' : help(argv); exit(0); break; case 'm' : M0 = atoi(&argv[++i][0]); M1 = atoi(&argv[++i][0]); if ((M0 > 4) || (M1 > M0)) { fprintf(stderr, "%s: ERROR: illegal values: %d %d\n", argv[0], M0, M1); usage(argv); exit(0); } break; case 'n' : NumGames = atoi(&argv[++i][0]); break; case 's' : Seed = atoi(&argv[++i][0]); break; case 'u' : usage(argv); exit(0); break; default: fprintf(stderr, "%s: ERROR: illegal flag: %s\n", argv[0], argv[i]); usage(argv); exit(0); break; } /* end switch */ } /* end if */ } /* end for(i) */ } /* end parseArgs() */ /* * writeTri - print out triangular game board */ writeTri() { int i, j, k; for (i=0; i<=4; i++) { for (k=0; k<=4 - i; k++) { fprintf(stdout, " "); } for (j=0; j<=i; j++) { fprintf(stdout, "%d ", Tri[i][j]); } fprintf(stdout, "\n"); } } End of iqtester.c echo iqtester.h 1>&2 cat >iqtester.h <<'End of iqtester.h' /* * iqtester.h -- include file for iqtester.c */ /* includes */ #include #include #include #include /* constants */ #define TRUE (1) #define FALSE (0) /* global variables */ int IntGlobal; /* integer: a signed whole number */ int Iq[36][2][3]; /* guess, coord, triplet */ int Tri[5][5]; /* flag-settable parameters (global variables) */ int Debug; /* Boolean: TRUE = debug writes */ int NumGames; /* integer: a signed whole number */ int Seed; int M0; int M1; End of iqtester.h echo usage.c 1>&2 cat >usage.c <<'End of usage.c' /* * usage - give program usage */ #include usage(argv) char** argv; { fprintf(stderr, "%s: USAGE: %s [flags] > outfile\n", argv[0], argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " flag meaning default\n"); fprintf(stderr, " ----------------- ---------------------- -------\n"); fprintf(stderr, " -D debug FALSE \n"); fprintf(stderr, " -m where hole is 4,2\n"); fprintf(stderr, " -h help on this program \n"); fprintf(stderr, " -n number of games to play 10\n"); fprintf(stderr, " -s random number seed 0\n"); fprintf(stderr, " -u usage (this message) FALSE\n"); fprintf(stderr, "\n"); } /* end of usage */ End of usage.c