games@tekred.TEK.COM (06/08/88)
Submitted by: Ray Balogh <rabalogh@ccng.waterloo.edu> Comp.sources.games: Volume 4, Issue 30 Archive-name: chessbd [I have no ReGIS capability, so you're on your own. See also the following posting converting std chess nomenclature into a format suitable for this program. -br] #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of shell archive." # Contents: README Makefile chessboard.c boardinit # Wrapped by billr@saab on Tue Jun 7 16:04:14 1988 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f README -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"README\" else echo shar: Extracting \"README\" \(1598 characters\) sed "s/^X//" >README <<'END_OF_README' X XChessboard X---------- X X SYNOPSIS: chessboard [-h] X X DESCRIPTION: X X This is a graphic chess board, using ReGIS graphics for the VT240. X It takes Cartesian algebraic moves (e.g., e2e4) from the standard X input and sends the ReGIS commands to the standard output. X X No legal move checking is done, other than making sure that a piece X exists before it is moved. Castling is specified by moving the king X two squares, and en-passant is also supported. X X The program will set up the VT240 in ReGIS mode, and if the -h option X is specified, it will also set up the tty for cbreak mode and no X echo. X X CAVEAT: X X This program was written without a proper ReGIS manual, partially by X trial and error (especially the usage of the fill function), X and so there is no guarantee that it will work for you on your X terminal. However, it has worked here on several VT240's. X X OPTIONS: X X -h wait for space from /dev/tty before displaying X a move from the standard input. X X FILES: X X boardinit the initialization data file, and search path X $HOME/boardinit X abspath[]/boardinit X X /dev/tty with -h, opened for "hit-space-to-continue" input. X X SUGGESTIONS: X X The following features should be supported: X - A user-defined initial position X - Saving of the current position X - Undoing of moves, and returning to a previous position directly X - Legal move checking X X BUGS: X X The display cannot be redrawn. Therefore, stopping the job will spoil X the display, as will any garbage sent by other programs to your tty. X END_OF_README if test 1598 -ne `wc -c <README`; then echo shar: \"README\" unpacked with wrong size! fi # end of overwriting check fi if test -f Makefile -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"Makefile\" else echo shar: Extracting \"Makefile\" \(204 characters\) sed "s/^X//" >Makefile <<'END_OF_Makefile' X# Makefile for chessboard (ReGIS board display) X XB_FLAGS = -DBSD -DVERBOSE XB_OBJ = chessboard.c XB_INC = X X#### X Xall: chessboard X Xchessboard: $(B_OBJ) X cc -o chessboard $(B_OBJ) X X$(B_OBJ): $(B_INC) X END_OF_Makefile if test 204 -ne `wc -c <Makefile`; then echo shar: \"Makefile\" unpacked with wrong size! fi # end of overwriting check fi if test -f chessboard.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"chessboard.c\" else echo shar: Extracting \"chessboard.c\" \(9668 characters\) sed "s/^X//" >chessboard.c <<'END_OF_chessboard.c' X/* screen is 799x479 */ X X#include <stdio.h> X#include <ctype.h> X X#define WHITE 0 X#define BLACK 1 X#define XORG 160 X#define XSIZE 60 X#define YORG 0 X#define YSIZE 60 X#define PXOFFS 10 X#define PYOFFS 56 X/* Coordinates for warning symbol */ X#define WARNPOSX 700 X#define WARNPOSY 50 X/* Tests for white/black piece */ X#define ISPWHITE( p ) islower( p ) X#define ISPBLACK( p ) isupper( p ) X/* Tests for white/black square */ X#define ISSWHITE( f, r ) ((f + r) % 2) X#define ISSBLACK( f, r ) (1-(f + r) % 2) X Xchar initfile[] = "boardinit"; Xchar abspath[] = "/u/rabalogh/bin/games/chess"; X X/* global variables */ XFILE *tty; Xint textx, texty; /* current text coordinates */ X Xint Xnametofile( fname ) Xchar fname; X{ X if ( islower( fname ) ) X return( fname - 'a' ); X else X return( fname - 'A' ); X} X Xposition( file, rank ) Xint file, rank; X{ X printf( "P[%d,%d]", XORG + XSIZE * file, YORG + YSIZE * (7-rank) ); X} X Xputtext( x, y, intensity, text ) Xint x, y, intensity; Xchar text[]; X{ X} X Xprintmove( movespec ) Xchar movespec[]; X{ X int i; X char s[30]; X static color = WHITE; X static move = 1; X X movespec[9] = '\0'; X printf( "W(R)" ); /* set replace writing mode */ X if ( color == WHITE ) { X sprintf( s, "%3d. %s", move, movespec ); X /* blank out field */ X printf( "P[%d,%d] W(I0) T' '", textx, texty ); X /* print text */ X printf( "P[%d,%d] W(I%d) T'%s'", textx, texty, 2, s ); X color = BLACK; X } else { X sprintf( s, " %s", movespec ); X /* print text */ X printf( "P[%d,%d] W(I%d) T'%s'", textx+80, texty, 2, s ); X if ( (texty += 20) > 479 ) texty = 20; X move++; X color = WHITE; X } X printf( "W(V)" ); /* reset writing mode to overlay */ X} X Xflashwarn( x, y, squarecolor, flashcolor ) Xint x, y, squarecolor, flashcolor; X{ X printf( "P[%d,%d]", x, y ); X printf( "W(I%d) V[+59,+0] V[+0,+59] V[-59,+0] V[+0,-59]", flashcolor ); X printf( "V[+59,+59] P[+0,-59] V[-59,+59] P[+0,-59]" ); X fflush( stdout ); X sleep( 1 ); X printf( "W(I%d) V[+59,+0] V[+0,+59] V[-59,+0] V[+0,-59]", squarecolor ); X printf( "V[+59,+59] P[+0,-59] V[-59,+59] P[+0,-59]" ); X fflush( stdout ); X} X Xhighlight( file, rank, color ) Xint file, rank, color; X{ X position( file, rank ); X X /* highlight the square */ X printf( "W(I%d) V[+59,+0] V[+0,+59] V[-59,+0] V[+0,-59]", color ); X fflush( stdout ); X} X Xunhighlight( file, rank ) Xint file, rank; X{ X position( file, rank ); X /* delete highlighting (border) */ X if ( ISSWHITE( file, rank ) ) X printf( "W(I2)" ); X else X printf( "W(I1)" ); X printf( "V[+59,+0] V[+0,+59] V[-59,+0] V[+0,-59]" ); X fflush( stdout ); X} X Xerase( file, rank ) Xint file, rank; X{ X position( file, rank ); X X /* clear the square */ X printf( "W(I3) V[+59,+0] P[-59,+59] V[+59,+0] P[-59,-59]" ); X if ( ISSWHITE( file, rank ) ) X printf( "W(I2)" ); X else X printf( "W(I1)" ); X printf( "V[+0,+59] W(S1) P[+0,-59] V[+59,+0] W(S0)" ); X} X Xputpiece( file, rank, piece ) Xint file, rank; X{ X position( file, rank ); X /* draw the piece */ X printf( "P[+10,+56] W(I%d)@%c P[-10,-56]", ISPWHITE(piece)?3:0, piece ); X fflush( stdout ); X} X Xremove( file, rank ) Xint file, rank; X{ X highlight( file, rank, 3 ); X sleep( 2 ); X erase( file, rank ); X} X Xredraw( file, rank, piece ) Xint file, rank; Xchar piece; X{ X erase( file, rank ); X highlight( file, rank, 3 ); X putpiece( file, rank, piece ); X sleep( 2 ); X unhighlight( file, rank ); X fflush( stdout ); X} X Xcastle( ksfile, kefile, rsfile, refile, rank ) Xint ksfile, kefile, rsfile, refile, rank; X{ X /* clear the original squares */ X highlight( ksfile, rank, 3 ); highlight( rsfile, rank, 3 ); X sleep( 1 ); X erase( ksfile, rank ); erase( rsfile, rank ); X /* redraw the king and rook */ X erase( kefile, rank ); erase( refile, rank ); X highlight( kefile, rank, 3 ); highlight( refile, rank, 3 ); X if ( rank == 0 ) { X putpiece( kefile, 0, 'k' ); putpiece( refile, 0, 'r' ); X } else { X putpiece( kefile, 7, 'K' ); putpiece( refile, 7, 'R' ); X } X sleep( 1 ); X unhighlight( kefile, rank ); unhighlight( refile, rank ); X fflush( stdout ); X} X Xen_passant( sf, sr, ef, er, piece ) Xint sf, sr, ef, er; Xchar piece; X{ X /* highlight the two pawns */ X highlight( sf, sr, 3 ); highlight( ef, sr, 3 ); X sleep( 1 ); X /* erase them */ X erase( sf, sr ); erase( ef, sr ); X highlight( ef, er, 3 ); X sleep( 1 ); X putpiece( ef, er, piece ); X unhighlight( ef, er ); X fflush( stdout ); X} X Xwaitcmd() X{ X fflush( tty ); X rewind( tty ); X system( "stty cbreak -echo" ); X while ( fgetc(tty) != ' ' ) ; X system( "stty -cbreak -echo" ); X rewind( tty ); X} X Xmain( argc, argv ) Xint argc; Xchar *argv[]; X{ X int holdflag; X int nargs; X char piece; X char promoted_value; X char sfname, efname; X int sf, sr, ef, er; X char promval; X int xpos, ypos; X FILE *chessinit; X char s[1024]; X char buf[1024]; X X /* board appears upside down; lower case are white */ X static X char board[8][8] = {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r', X 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', X ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', X ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', X ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', X ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', X 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', X 'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R' X }; X X /* search current directory, HOME directory, and an absolute directory X * for initialization file */ X if ((chessinit = fopen(initfile, "r")) == NULL) { X sprintf(buf, "%s/%s", getenv("HOME"), initfile); X if ((chessinit = fopen(buf, "r")) == NULL) { X sprintf(buf, "%s/%s", abspath, initfile); X if ((chessinit = fopen(buf, "r")) == NULL) { X fprintf(stderr, "can't open board init file '%s'\n", initfile); X exit(1); X } X } X } X X if ( argc >= 1 ) X holdflag = (! isatty( fileno(stdin) )) X && (strcmp( argv[1], "-h" ) == 0); X else X holdflag = 0; X X if ( holdflag && (tty = fopen( "/dev/tty", "r+" )) == NULL) { X fprintf( stderr, "cant open /dev/tty\n" ); X exit( 1 ); X } X X /* turn off echoing of typed characters */ X system( "stty -echo" ); X X /* initialize variables */ X textx = 0; texty = 20; X X /* initialize board */ X printf( "P1p S(E) S(C0) W(F3)" ); X while( fgets( s, 1024-1, chessinit ) != NULL ) X printf( s ); X fflush( stdout ); X /* sleep( 14 ); */ /* wait for terminal to finish drawing */ X if ( holdflag ) X waitcmd(); X X X /* MAIN LOGIC */ X X while( fgets( s, 1024-1, stdin ) != NULL ) { X X if ( strncmp(s,"illegal",7) == 0 || strncmp(s,"ambiguous",9) == 0 ) { X flashwarn( WARNPOSX, WARNPOSY, 0, 2 ); X continue; X } X X nargs = sscanf( s, " %c%1d%c%1d%c", X &sfname, &sr, &efname, &er, &promval ); X if ( nargs < 4 ) { X flashwarn( WARNPOSX, WARNPOSY, 0, 3 ); X continue; X } X X /* do conversion to ints on file/rank specifiers */ X sr--; er--; X sf = nametofile( sfname ); X ef = nametofile( efname ); X X /* check if coordinates are bad */ X if ( sr < 0 || sr > 7 || sf < 0 || sf > 7 X || er < 0 || er > 7 || ef < 0 || ef > 7 ) { X flashwarn( WARNPOSX, WARNPOSY, 0, 3 ); X continue; X } X X /* check if the piece exists */ X if ( board[ sr ][ sf ] == ' ' ) { X /* square is empty -- flash warning */ X flashwarn( XORG + XSIZE * sf, YORG + YSIZE * (7-sr), X (ISSWHITE( sf, sr )?2:1), 3 ); X continue; X } X X printmove( s ); X X /* check for CASTLE */ X if ( board[sr][sf] == 'k' && abs(sf-ef) == 2 ) { X /* white castles */ X if ( ef == 6 ) { X /* king's side */ X board[ 0 ][ 4 ] = ' '; X board[ 0 ][ 6 ] = 'k'; X board[ 0 ][ 7 ] = ' '; X board[ 0 ][ 5 ] = 'r'; X castle( 4, 6, 7, 5, 0 ); X /* goto end_wait; */ X } else { X /* queen's side */ X board[ 0 ][ 4 ] = ' '; X board[ 0 ][ 2 ] = 'k'; X board[ 0 ][ 0 ] = ' '; X board[ 0 ][ 3 ] = 'r'; X castle( 4, 2, 0, 3, 0 ); X /* goto end_wait; */ X } X } else X if ( board[sr][sf] == 'K' && abs(sf-ef) == 2 ) { X /* black castles */ X if ( ef == 6 ) { X /* king's side */ X board[ 7 ][ 4 ] = ' '; X board[ 7 ][ 6 ] = 'K'; X board[ 7 ][ 7 ] = ' '; X board[ 7 ][ 5 ] = 'R'; X castle( 4, 6, 7, 5, 7 ); X /* goto end_wait; */ X } else { X /* queen's side */ X board[ 7 ][ 4 ] = ' '; X board[ 7 ][ 2 ] = 'K'; X board[ 7 ][ 0 ] = ' '; X board[ 7 ][ 3 ] = 'R'; X castle( 4, 2, 0, 3, 7 ); X /* goto end_wait; */ X } X } else X X /* check for EN PASSANT */ X if ( (sr == 3 && er == 2 && board[sr][sf] == 'P' X || sr == 4 && er == 5 && board[sr][sf] == 'p') X && sf != ef && board[er][ef] == ' ' ) { X piece = board[sr][sf]; X board[sr][sf] = ' '; X board[sr][ef] = ' '; X board[er][ef] = piece; X en_passant( sf, sr, ef, er, piece ); X /* goto end_wait; */ X } else X X /* check for PAWN PROMOTION */ X if ( er == 7 && board[sr][sf] == 'p' X || er == 0 && board[sr][sf] == 'P' ) { X /* promote to appropriate piece */ X switch( promval ) { X case 'q': X case 'Q': X case 'r': X case 'R': X case 'b': X case 'B': X case 'n': X case 'N': X if ( islower(promval) ) X promval = toupper(promval); X promoted_value = (er == 7) ? tolower(promval) : promval; X break; X default: X promoted_value = (er == 7) ? 'q' : 'Q'; break; X } X board[er][ef] = promoted_value; X board[sr][sf] = ' '; X remove( sf, sr ); X redraw( ef, er, promoted_value ); X /* goto end_wait; */ X } else { X X /* If not CASTLE, EN PASSANT, or PAWN PROMOTION */ X X /* update the board in memory */ X piece = board[ sr ][ sf ]; X board[ sr ][ sf ] = ' '; X board[ er ][ ef ] = piece; X X /* erase and redraw the piece */ X remove( sf, sr ); X redraw( ef, er, piece ); X X } X X/* end_wait: */ X if ( holdflag ) X waitcmd(); X } X X fflush( stdout ); X sleep( 1 ); X printf( "/" ); X} END_OF_chessboard.c if test 9668 -ne `wc -c <chessboard.c`; then echo shar: \"chessboard.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f boardinit -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"boardinit\" else echo shar: Extracting \"boardinit\" \(2264 characters\) sed "s/^X//" >boardinit <<'END_OF_boardinit' XW(P(M1)) XW(P1) XW(S[,0]) XW(S(x)[0]) X X@:W XW(S1) XP[160,-60] XW(I2) V[+60,+0] XW(I1) V[+60,+0] XW(I2) V[+60,+0] XW(I1) V[+60,+0] XW(I2) V[+60,+0] XW(I1) V[+60,+0] XW(I2) V[+60,+0] XW(I1) V[+60,+0] X@; X X@:B XW(S1) XP[160,-60] XW(I1) V[+60,+0] XW(I2) V[+60,+0] XW(I1) V[+60,+0] XW(I2) V[+60,+0] XW(I1) V[+60,+0] XW(I2) V[+60,+0] XW(I1) V[+60,+0] XW(I2) V[+60,+0] X@; X X@:L XW(I3) XW(S0) XP[160,+120] V[+480,+0] X@; X XW(I3) XW(S0) XP[160,60] V[+480,+0] X X@W @L @B @L @W @L @B @L @W @L @B @L @W @L @B X@:P X V(B) X [+34,+0][+0,-5][-5,-5][-4,+0] X [+0,-5][+5,-7][+0,-5][-5,-7] X [-5,-3][-6,+0] X [-5,+3][-5,+7] X [+0,+5][+5,+7][+0,+5] X [-4,+0][-5,+5] X V(E) X@; X@:N X V(B) X [+34,+0][+0,-5][-5,-5][-2,+0] X [+0,-12][+10,+2][+5,-5][-15,-15] X [-10,+0][-5,-5][-5,+5][-5,+0] X [+0,+20][+5,+0][+0,+10][-2,+0] X [-5,+5] X V(E) X@; X@:B X V(B) X [+34,+0][+0,-5][-5,-5][-4,+0] X [+0,-12][+5,-3][+0,-5][-3,-5] X [-5,+5][+0,-7][-3,-3][+2,-3] X [-4,-2][-4,+2] X [+2,+3][-8,+5][-3,+5][+0,+5] X [+5,+3][+0,+12] X [-4,+0][-5,+5] X V(E) X@; X@:R X V(B) X [+34,+0][+0,-5][-5,-5][-4,+0][+0,-8] X [+7,-3][+0,-20] X [-7,+0][+0,+7][-4,+0][+0,-7][-8,+0][+0,+7][-4,+0][+0,-7][-7,+0] X [+0,+20][+7,+3] X [+0,+8][-4,+0][-5,+5] X V(E) X@; X@:Q X V(B) X [+34,+0][+0,-5][-5,-5][-4,+0] X [+0,-5][+3,-2][-3,-3][+3,-3][-3,-2][+0,-5] X [+5,-3][+0,-4][-5,-3] X [+4,-10][-8,+5][-4,-5][-4,+5][-8,-5][+4,+10] X [-5,+3][+0,+4][+5,+3] X [+0,+5][-3,+2][+3,+3][-3,+3][+3,+2][+0,+5] X [-4,+0][-5,+5] X V(E) X@; X@:K X V(B) X X [+34,+0] X [+0,-5][-5,-5][-4,+0] X [+0,-4][+3,-2][-3,-3][+3,-3][-3,-2][+0,-5] X [+5,-3][+0,-4][-5,-3][-5,+0] X [+0,-6][+5,+0][+0,-4][-5,+0][+0,-4] X [-6,+0] X [+0,+4][-5,+0][+0,+4][+5,+0][+0,+6] X [-5,+0][-5,+3][+0,+4][+5,+3] X [+0,+5][-3,+2][+3,+3][-3,+3][+3,+2][+0,+4] X [-4,+0][-5,+5] X V(E) X@; XW(P(M1)) XW(P1) XW(S0) X XW(I0) X XP[170,56] @R XP[+60,+0] @N XP[+60,+0] @B XP[+60,+0] @Q XP[+60,+0] @K XP[+60,+0] @B XP[+60,+0] @N XP[+60,+0] @R X XP[170,116] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P X XW(I3) X XP[170,416] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P XP[+60,+0] @P X XP[170,476] @R XP[+60,+0] @N XP[+60,+0] @B XP[+60,+0] @Q XP[+60,+0] @K XP[+60,+0] @B XP[+60,+0] @N XP[+60,+0] @R END_OF_boardinit if test 2264 -ne `wc -c <boardinit`; then echo shar: \"boardinit\" unpacked with wrong size! fi # end of overwriting check fi echo shar: End of shell archive. exit 0