Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Quick and dirty GamesGrid .CBG viewer in flex [LONG]

0 views
Skip to first unread message

Gary Wong

unread,
Sep 28, 1997, 3:00:00 AM9/28/97
to

There are lots of nice annotated matches on the GamesGrid site at
http://www.gamesgrid.com/cgi-bin/pages.pl?match which are not a lot of use
to Unix people (or anyone else without the inclination to track down a
Win 95 machine just to view the matches), so if you're like me and want
to see what's inside a .CBG file, this flex program might help you. Try
something like:

$ flex cbg.l
$ cc -o cbg lex.yy.c -ll
$ ./cbg < match.cbg

WARNING: it's VERY quick and dirty; all it does is update the board position
based on the moves, and extract the annotations from the RTF jumble (and
display it in FIBS-style Xs and Os). It's not supposed to be pretty, or
complete.

(It seems to require flex, though I tried not to use any flex-specific things.
Not entirely sure why ordinary lex fails -- might be because of the NULs in
the input?)

Cheers,
Gary (GaryW on FIBS).
---- 8< ---- cut here ---- cbg.l ---- cut here ---- 8< ----
%s COMMENT
%s MOVE
%s ROLL
%s RTF
%s RTFIGNORE

%{
/*
* cbg.l
*
* by Gary Wong (ga...@cs.auckland.ac.nz), 1997
*
* NB: REQUIRES flex -- lex may fail silently
*/

#include <stdlib.h>

int fX;
int aBoard[ 28 ];

void PrintBoard( void ) {

int x, y;

puts( fX ? " +13-14-15-16-17-18------19-20-21-22-23-24-+" :
" +12-11-10--9--8--7-------6--5--4--3--2--1-+" );

for( y = 0; y < 5; y++ ) {
putchar( ' ' );
putchar( '|' );

for( x = 13; x < 19; x++ ) {
putchar( ' ' );
putchar( aBoard[ x ] > y ? 'X' :
-aBoard[ x ] > y ? 'O' : ' ' );
putchar( ' ' );
}

putchar( '|' );
putchar( ' ' );
putchar( -aBoard[ 0 ] > y ? 'O' : ' ' );
putchar( ' ' );
putchar( '|' );

for( x = 19; x < 25; x++ ) {
putchar( ' ' );
putchar( aBoard[ x ] > y ? 'X' :
-aBoard[ x ] > y ? 'O' : ' ' );
putchar( ' ' );
}

puts( "|" );
}

putchar( fX ? 'v' : '^' );
puts( "| |BAR| |" );

for( y = 4; y >= 0; y-- ) {
putchar( ' ' );
putchar( '|' );

for( x = 12; x > 6; x-- ) {
putchar( ' ' );
putchar( aBoard[ x ] > y ? 'X' :
-aBoard[ x ] > y ? 'O' : ' ' );
putchar( ' ' );
}

putchar( '|' );
putchar( ' ' );
putchar( aBoard[ 25 ] > y ? 'X' : ' ' );
putchar( ' ' );
putchar( '|' );

for( x = 6; x; x-- ) {
putchar( ' ' );
putchar( aBoard[ x ] > y ? 'X' :
-aBoard[ x ] > y ? 'O' : ' ' );
putchar( ' ' );
}

puts( "|" );
}

puts( fX ? " +12-11-10--9--8--7-------6--5--4--3--2--1-+" :
" +13-14-15-16-17-18------19-20-21-22-23-24-+" );
}

void InitBoard( void ) {

int i;

for( i = 0; i < 28; i++ )
aBoard[ i ] = 0;

aBoard[ 24 ] = 2;
aBoard[ 1 ] = -2;

aBoard[ 13 ] = 5;
aBoard[ 12 ] = -5;

aBoard[ 8 ] = 3;
aBoard[ 17 ] = -3;

aBoard[ 6 ] = 5;
aBoard[ 19 ] = -5;

fX = 1;
}

void Move( char *sz ) {

int aPoint[ 9 ], iPoint;
char *pch = sz;

printf( "*** Move:%s\n", sz );

for( iPoint = 0; aPoint[ iPoint ] = strtol( pch, &pch, 10 ); iPoint++ ) {
if( aPoint[ iPoint ] == -1 )
aPoint[ iPoint ] = fX ? 26 : 27;
else if( !fX )
aPoint[ iPoint ] = 25 - aPoint[ iPoint ];

if( *pch == '*' ) {
aBoard[ aPoint[ iPoint ] ] = 0;

if( fX )
aBoard[ 0 ]--;
else
aBoard[ 25 ]++;

pch++;
}
}

aPoint[ iPoint ] = -1;

for( iPoint = 0; aPoint[ iPoint ] >= 0; iPoint += 2 )
if( fX ) {
aBoard[ aPoint[ iPoint ] ]--; aBoard[ aPoint[ iPoint + 1 ] ]++;
} else {
aBoard[ aPoint[ iPoint ] ]++; aBoard[ aPoint[ iPoint + 1 ] ]--;
}

fX = !fX;

PrintBoard();
}

%}

%%

%{
int nRTF = 0, nRTFIgnore = 0;

InitBoard();
%}

<RTF>"{" nRTF++;
<RTF>"}" if( !--nRTF ) BEGIN(INITIAL);
<RTF>\\fonttbl|\\colortbl|\\\* { BEGIN(RTFIGNORE); nRTFIgnore = 1; }
<RTF>\\par" "? putchar( '\n' );
<RTF>\\[a-z]+-?[0-9]*" "? ;
<RTF>\\[\\\{\}] ECHO;
<RTF>\\. ;
<RTF>\r ;
<RTF>. ECHO;

<RTFIGNORE>"{" nRTFIgnore++;
<RTFIGNORE>"}" if( !--nRTFIgnore ) { nRTF--; BEGIN(RTF); }
<RTFIGNORE>. ;

<MOVE>(" "-?[0-9]+\*?)+ { Move( yytext ); BEGIN(INITIAL); }
<MOVE>. { Move( " (no move)" ); BEGIN(INITIAL); }

<ROLL>[0-9]\ [0-9] { printf( "*** Roll: %s\n", yytext );
BEGIN(INITIAL); }

<COMMENT>[ -~]* { ECHO; putchar( '\n' ); BEGIN(INITIAL); }
<COMMENT>. BEGIN(INITIAL);

"{" { BEGIN(RTF); nRTF = 1; }
"\0Y* . "[-0-9]+" a" puts( "*** Accept" );
"\0Y* . "[-0-9]+" d" puts( "*** Double" );
"\0Y* . "[-0-9]+" k" puts( "*** Reject" );
"\0Y* . "[-0-9]+" m" BEGIN(MOVE);
"\0Y* . "[-0-9]+" r" BEGIN(ROLL);
"\0Y* . "[-0-9]+" W" puts( "*** Wins" );
"\0Y* . "[-0-9]+" ." { puts( "*** New game" ); InitBoard();
PrintBoard(); }
"\0"((B\*)|(.\$))" " BEGIN(COMMENT);
\n ;
. ;

%%
---- 8< ---- cut here ---- 8< ----
--
Gary Wong, Computer Science Department, University of Auckland, New Zealand
ga...@cs.auckland.ac.nz http://www.cs.auckland.ac.nz/~gary/

0 new messages