home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume4
/
chtrans
/
chtrans.c
next >
Wrap
C/C++ Source or Header
|
1988-06-07
|
4KB
|
156 lines
/*
* Copyright (c) 1987 by Ray Balogh (rabalogh@ccng.waterloo.edu)
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to redistribute it freely,
* subject to the following restrictions:
*
* 1. The author is not responsible for the consequences of use of
* this software, no matter how awful, even if they arise
* from defects in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
*/
/* see "regexp.c" for the copyright notice pertaining to the regular
* expression parser.
*/
#include <stdio.h>
#include "regexp.h"
#define DUNNO '_'
char buf[20];
char gram1_castle[] =
"^[oO0]-[oO0](-[oO0])?(ch|mt|[+])?[!?,.;]*$";
char gram1_general[] =
"^([NBRQK]?)([a-h]?)([1-8]?)([xX:-]?)([a-h]?)([1-8]?)(ch|mt|[+])?[!?,.;]*$";
char gram1_ignore[] =
"^([0-9]+\\.?|a|be|each|he|ch|mt|Re:)$";
#define SUBEX(p,i) (p->startp[i])
#define NILSUBEX(p,i) (p->endp[i] == p->startp[i])
#define SUBEXLEN(p,i) (p->endp[i] - p->startp[i])
#define BDCHAR(p,i) ((!NILSUBEX(p,i)) ? *SUBEX(p,i) : DUNNO)
/* gobble (possibly nested) comments */
gobble( s, openc, closec )
char *s, openc, closec;
{
int c;
while ( *++s != '\0' )
if ( *s == openc )
gobble( s, openc, closec );
else if ( *s == closec )
return;
while ( (c = getchar()) != EOF && c != (int)closec )
if ( c == (int)openc )
gobble( " ", openc, closec );
}
/* check if s contains any valid move spec. char */
int
testmove( s )
char *s;
{
static int testtab[128] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,-1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
0,0,50,0,0,0,0,0,0,0,0,50,0,0,50,-1,0,50,50,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,50,50,50,50,50,50,50,50,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
register int sum;
register char *p;
for ( sum = 0, p = s; *p != '\0' ; p++ )
sum += testtab[ (int)*p & 0xFF ];
return ( sum > 50 || sum < -2 );
}
/* ARGSUSED */
main(argc, argv)
int argc;
char *argv[];
{
regexp *g1_ignore, *g1_castle, *g1_general, *expr;
int i,j;
char piece, sep, f1, r1, f2, r2;
char s[256];
static int ply = 0;
if ( (g1_ignore = regcomp(gram1_ignore)) == NULL ||
(g1_castle = regcomp(gram1_castle)) == NULL ||
(g1_general = regcomp(gram1_general)) == NULL ) {
fprintf( stderr, "bad grammar\n" );
exit(1);
}
while ( scanf("%s",s) == 1 ) {
/* gobble comments */
if ( *s == '#' ) {
gobble( s, '\0', '\n' );
continue;
}
if ( *s == '(' ) {
gobble( s, '(', ')' );
continue;
}
/* DEBUG fprintf( stderr, "[%s] ", s ); */
if ( ! testmove(s) ) /* make cursory validity check */
continue;
if ( regexec(g1_ignore, s) == 1 ) {
continue;
} else
if ( regexec(g1_castle, s) == 1 ) {
expr = g1_castle;
if ( expr->startp[1] == expr->endp[1] ) /* king's side */
{sprintf( buf, "Ke%c-g%c", DUNNO, DUNNO ); trans(buf, ++ply);}
else
{sprintf( buf, "Ke%c-c%c", DUNNO, DUNNO ); trans(buf, ++ply);}
continue;
} else
if ( regexec(g1_general, s) == 1 ) {
expr = g1_general;
piece = BDCHAR(expr,1);
f1 = BDCHAR(expr,2); r1 = BDCHAR(expr,3);
f2 = BDCHAR(expr,5); r2 = BDCHAR(expr,6);
sep = BDCHAR(expr,4);
if ( f2 == DUNNO && r2 == DUNNO ) {
f2 = f1; r2 = r1;
f1 = DUNNO; r1 = DUNNO;
}
if ( piece == DUNNO )
piece = 'P';
/* if sep is not given, leave it as unspecified
if ( sep == DUNNO )
sep = '-';
*/
if (sep != '-' && sep != DUNNO)
sep = ':';
sprintf( buf, "%c%c%c%c%c%c%.*s",
piece, f1, r1, sep, f2, r2, SUBEXLEN(expr,7), SUBEX(expr,7) );
trans(buf, ++ply);
continue;
} else {
/* fprintf( stderr, "syntax error\n" ); */
continue;
}
}
exit(0);
}