// chessfen.cpp : Defines the entry point for the console application. // // cccc h h eeeee ssss ssss fffff eeeee n n cccc pppp pppp // c h h e s s f e nn n c p p p p // c hhhhh eeee sss sss ffff eeee n n n c pppp pppp // c h h e s s f e n nn .. c p p // cccc h h eeeee ssss ssss f eeeee n n .. cccc p p // #include "stdafx.h" /* c h e s s f e n . c p p dennette@wiz-worx.com ** ** test of C compiler on various platforms ** ... converts Decimal Board Format to Farnsworth-Edwards Notation ** ** 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 - outputs Forsyth-Edwards Notation (FEN) ** ** usage: A>CHESSFEN filename ** ** reads data from named file to display chessboard - see files ** CHESS*.DAT for examples */ #include #include #define DARK '*' #define LIGHT '.' #define BLACK '-' #define WHITE ' ' FILE *in_fcb; char board[8][16], pcolor, sqcolor; char mask[7] = { ' ', 'P', 'B', 'N', 'R', 'Q', 'K' }; long buffer; 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 */ // // Output: 3r2kr/ppp2n1p/7R/5k1Q/1bp5/2Pp4/PP2RPPP/R2Q2K1 // 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) { /* barf on open error */ fprintf(stderr, "\007\nCannot open data file '%s'\007\n", argv[1]); exit(-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); } } /* ** 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); if (sq[ua][re] == 0) { // empty square sqcolor = '1'; while ((sq[ua][re+1] == 0) && (re<7)) { sqcolor++; re++; } } /* ** 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(" "); if (pcolor == BLACK) buffer = tolower(buffer); printf("%c",(sq[ua][re]==0 ? sqcolor : buffer)); } if (ua < 7) printf("/"); } exit(0); }