/* cccc h h eeeee ssss ssss 2222 cccc ** c h h e s s 2 c ** c hhhhh eeee sss sss 222 c ** c h h e s s 2 .. c ** cccc h h eeeee ssss ssss 22222 .. cccc ** ** c h e s s 2 . c dennette@wiz-worx.com ** ** test of C compiler on various platforms ** ... draws a chessboard ** ** 29-jun-76 FORTRAN - XDS Sigma 9 - original version ** 27-oct-79 FORTRAN - PDP 11/70 ** 08-jul-82 C - VAX 780 ** 01-apr-83 Pascal - IBM PC ** 04-dec-83 C - DEC Rainbow (Aztec CII) ** 22-dec-83 - modified to read data from a file ** 30-may-84 C - SAGE-IV (DRI CP/M-68K) ** 09-sep-84 C - DEC Rainbow (CI-C86) ** 23-aug-93 C - Microsoft (ANSI) Visual C++ ** 01-aug-95 C - changed default board ** 1997-03-17 C - outputs "small" board ** 2009-04-18 C - uses lower case for black instead of "-" ** 2009-04-20 C - reads .FEN files ** ** usage: A>CHESS2 filename ** ** reads data from named file to display chessboard - see files ** CHESS*.FEN for examples */ #include #include #define DARK '*' #define LIGHT '.' FILE *in_fcb; char inbuf[80]; // "3r2kr/ppp2n1p/7B/5q1N/1bp5/2Pp4/PP2RPPP/R2Q2K1" char board[8][16], pcolor, sqcolor; char mask[7] = { ' ', 'P', 'B', 'N', 'R', 'Q', 'K' }; long buffer; int bp=0; int bcol, brow, col, re, row, ua, sq[8][8] = { /* default values */ { 0, 0, 0, -4, 0, 0, -6, -4 }, /* . * . r . * k r */ { -1, -1, -1, 0, 0, -3, 0, -1 }, /* p p p . * n * p */ { 0, 0, 0, 0, 0, 0, 0, 2 }, /* . * . * . * . B */ { 0, 0, 0, 0, 0, -5, 0, 3 }, /* * . * . * q * N */ { 0, -2, -1, 0, 0, 0, 0, 0 }, /* . b p * . * . * */ { 0, 0, 1, -1, 0, 0, 0, 0 }, /* * . P p * . * . */ { 1, 1, 0, 0, 4, 1, 1, 1 }, /* P P . * R P P P */ { 4, 0, 0, 5, 0, 0, 6, 0 } /* R . * Q * . K . */ }; /* "SHANNON'S SACRIFICE" - SCIENTIFIC AMERICAN, FEB'50 */ ///////////////////////////////////////////////////////////////// int nextok( void ) { char token; int val; if (inbuf[bp] == '/') // skip seperator bp++; token = inbuf[bp]; // get next token switch (token) { case 'K': val = 6; bp++; break; case 'k': val = -6; bp++; break; case 'Q': val = 5; bp++; break; case 'q': val = -5; bp++; break; case 'R': val = 4; bp++; break; case 'r': val = -4; bp++; break; case 'N': val = 3; bp++; break; case 'n': val = -3; bp++; break; case 'B': val = 2; bp++; break; case 'b': val = -2; bp++; break; case 'P': val = 1; bp++; break; case 'p': val = -1; bp++; break; case '1': val = 0; bp++; break; case '2': case '3': case '4': case '5': case '6': case '7': case '8': val = 0; inbuf[bp]--; break; // decrement buffer } return (val); } ///////////////////////////////////////////////////////////////// void main( int argc, char **argv ) { /* read chessboard data from named file ** ... or use default if no file named */ if (argc>1) { if ((in_fcb = fopen(argv[1],"r")) == NULL) { fprintf(stderr, "\007\nCannot open data file '%s'\007\n", argv[1]); exit(-1); /* barf on open error */ } else { fscanf(in_fcb, "%s", &inbuf); /* ... get string */ fclose(in_fcb); for (ua=0 ; ua<8 ; ua++) /* for each row ... */ for (re=0 ; re<8 ; re++) /* for each column ... */ sq[ua][re] = nextok(); /* ... get value */ } } /* ** for each sq[ua][re] determine the kind of piece ('mask'), ** the color of the piece ('pcolor'), and the color of the ** square ('sqcolor'). */ for (ua=0 ; ua<8 ; ua++) { for (re=0 ; re<8 ; re++) { buffer = mask[(sq[ua][re]<0 ? abs(sq[ua][re]) : sq[ua][re])]; if (sq[ua][re] < 0) /* pcolor==BLACK */ buffer = tolower(buffer); sqcolor = (((ua+re)%2)==0 ? LIGHT : DARK); /* ** now that you've got it, put it on paper ** if the sq[ua][re] is empty (value 0), put the square color, ** otherwise use the 'buffer' as the kind of piece */ printf(" %c", (sq[ua][re]==0 ? sqcolor : buffer)); } printf("\n"); } }