/* CCCC H H EEEEE SSSS SSSS CCCC ** C H H E S S C ** C HHHHH EEEE SSS SSS C ** C H H E S S .. C ** CCCC H H EEEEE SSSS SSSS .. CCCC ** ** c h e s s . c DAHarrod ** ** 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 ** ** usage: A>CHESS filename ** ** reads data from named file to display chessboard - see files ** CHESS.DAT and CHESS2.DAT for examples */ #include #include #define PERIOD '.' #define BLANK ' ' #define BLACK 'B' #define WHITE 'W' FILE *in_fcb; char board[64][80], pcolor, sqcolor; long buffer, mask[7] = { 0, 0xe9e88, 0xe9e9e, 0x9db99, 0xe9ea9, 0x699a5, 0x9aca9 }; int bcol, brow, col, re, row, ua, sq[8][8] = { /* "SHANNON'S SACRIFICE" - SCIENTIFIC AMERICAN, FEB'50 */ { 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 . */ }; /* default values */ void main( int argc, char **argv ) { /* read chessboard data from named file ** ... or use default if no file named */ if ((in_fcb = fopen(argv[1],"r")) == NULL) { /* barf on open error */ if (argc>1) fprintf(stderr, "\007\nCannot open data file '%s'\007\n", argv[1]); } else { for (ua=0 ; ua<8 ; ua++) /* for each row ... */ for (re=0 ; re<8 ; re++) /* for each column ... */ fscanf(in_fcb, "%d", &sq[ua][re]); /* ... get value */ fclose(in_fcb); } /* ** fill the board with periods & spaces */ for (re=0 ; re<8 ; re++) for (ua=0 ; ua<8 ; ua++) { sqcolor = (((ua+re)%2)==0 ? BLANK : PERIOD); for (row=0 ; row<8 ; row++) { brow = ua*8 + row; for (col=0 ; col<10 ; col++) { bcol = re*10 + col; board[brow][bcol] = sqcolor; } } } /* ** 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])]; pcolor = (sq[ua][re]<0 ? BLACK : WHITE); sqcolor = (((ua+re)%2)==0 ? BLANK : PERIOD); /* ** now strip the 'buffer' a bit at a time, and using each ** bit as a guide, move the 'sqcolor' or 'pcolor' into the ** corresponding position in the 4 x 5 matrix for the sq[ua][re]. */ for (row=6 ; row>1 ; row--) { brow = ua*8 + row; for (col=6 ; col>2 ; col--) { bcol = re*10 + col; board[brow][bcol] = (((int)buffer%2)!=0 ? pcolor : sqcolor); buffer /= 2; } } } /* ** now that you've got it, put it on paper */ for (row=0 ; row<64 ; row++) { if ((row%8)!=0) { for (col=0 ; col<80 ; col++) printf("%c",board[row][col]); printf("\n"); } } }