#define DEBUG 0 /* This is the preprocessor for the Java applet ... it is called with two arguments: 1) the first is the board definition as defined by the problem 2) the second is the solution output by the entries */ #include #include #include #include /* GLOBAL VARIABLES */ char board[26][60]; /* board[row][column] */ char moves[10000][5]; /* 10,000 moves should be enough! */ char startrow, stoprow; int startcol, stopcol; int num; /* number of moves read */ int rows, cols; /* Number of rows, columns on board */ int vdist, hdist; int jump, walk, pcount; int frow, fcol, score2; int x, y, z; /* looping variables */ /* MAIN ROUTINE */ main(argc, argv) int argc; char *argv[]; { FILE *fp; /* file pointer */ FILE *fm; /* file pointer */ int out; /* return from reads */ /* Open board file for reading */ switch ( argc ) { case 3: fp = fopen( argv[1], "r" ); if ( fp == NULL ) { printf( "Can't open board file '%s'\n", argv[1] ); exit( -1 ); } fm = fopen( argv[2], "r" ); if ( fm == NULL ) { printf( "Can't open move file '%s'\n", argv[2] ); exit( -1 ); } break; default: printf( "Usage: %s \n", argv[0] ); exit( -1 ); } /******************************* BOARD FILE *********************************/ rows=1; while ( (out=fscanf( fp, "%s", &board[rows][1])) > 0 ) { if (DEBUG) printf("Read row %d - out=%d\n",rows,out); rows++ ; } rows--; /********************************* MOVE FILE *********************************/ num=0; while ( (out=fscanf( fm, "%c%d %c%d\n", &startrow, &startcol, &stoprow, &stopcol)) > 0 ) { if (DEBUG) printf("Read move %d - out=%d\n",num,out); num++ ; /* Note adjustments so that row and column numbers start at zero */ moves[num][1]=startrow-'A'; moves[num][2]=startcol-1; moves[num][3]=stoprow-'A'; moves[num][4]=stopcol-1; } /******************************** CHECK FILE *********************************/ cols=1; while (board[1][cols] > 'A') cols++ ; cols--; printf("int rowlim=%d;\nint collim=%d;\nint moves=%d;\n",rows,cols,num); printf("\n// 'O' is an open hole, 'X' is a filled hole, '_' is not available\n"); printf("int board_table[][] = {\n"); for (x=1;x<=rows;x++) { printf(" { "); for (y=1;y<=cols;y++) { printf("'%c',",board[x][y]); } printf("'0'},\n"); } printf("};\n"); printf("\n// zero is a walk, one is a jump ... the other four numbers are\n"); printf("// the starting row/column and the ending row/column.\n"); printf("// unlike the puzzle, the first row and column are ZERO.\n"); printf("int moves_table[][] = {\n"); for (z=1;z<=num;z++) { vdist=abs(moves[z][1]-moves[z][3]); hdist=abs(moves[z][2]-moves[z][4]); if(DEBUG) printf(" vdist=%d and hdist=%d\n",vdist,hdist); /* CHECK TO MAKE SURE THIS IS A VALID MOVE DiSTANCE-WISE */ jump=0; walk=0; if (vdist==0 && hdist==2) jump=1; if (vdist==2 && hdist==0) jump=1; if (vdist==0 && hdist==1) walk=1; if (vdist==1 && hdist==0) walk=1; if (jump==0 && walk==0) { printf("ERROR: move %d was neither a jump nor a walk\n",z); exit(1); } if(walk) { printf(" { 0 ,%d,%d,%d,%d },\n", moves[z][1],moves[z][2],moves[z][3],moves[z][4]); } if(jump) { printf(" { 1 ,%d,%d,%d,%d },\n", moves[z][1],moves[z][2],moves[z][3],moves[z][4]); } } /* end of z loop */ printf("};\n"); } /* end of main */