Source Code

Forum Archive : Source Code

 
GamesGrid .CBG viewer in flex

From:   Gary Wong
Address:   gary@cs.auckland.ac.nz
Date:   28 September 1997
Subject:   Quick and dirty GamesGrid .CBG viewer in flex [LONG]
Forum:   rec.games.backgammon
Google:   ieyg1qpdeao.fsf@cs20.cs.auckland.ac.nz

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 (gary@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
       gary@cs.auckland.ac.nz        http://www.cs.auckland.ac.nz/~gary/
 
Did you find the information in this article useful?          

Do you have any comments you'd like to add?     

 

Source Code

Benchmark player "pubeval.c"  (Gerry Tesauro, Feb 1993) 
GamesGrid .CBG viewer in flex  (Gary Wong, Sept 1997) 
Match equity calculator  (Claes Thornberg, Sept 1996)  [Recommended reading]
Move generator  (Gary Wong, Oct 1998)  [Long message]
Rfibs and sfibs recorder and replayer  (Jan Spitzkowsky, Aug 1994) 
race2.c  (Marc Ringuette, Dec 1993)  [Long message]
race4.c  (Marc Ringuette, Dec 1993)  [Long message]

[GammOnLine forum]  From GammOnLine       [Long message]  Long message       [Recommended reading]  Recommended reading       [Recent addition]  Recent addition
 

  Book Suggestions
Books
Cheating
Chouettes
Computer Dice
Cube Handling
Cube Handling in Races
Equipment
Etiquette
Extreme Gammon
Fun and frustration
GNU Backgammon
History
Jellyfish
Learning
Luck versus Skill
Magazines & E-zines
Match Archives
Match Equities
Match Play
Match Play at 2-away/2-away
Miscellaneous
Opening Rolls
Pip Counting
Play Sites
Probability and Statistics
Programming
Propositions
Puzzles
Ratings
Rollouts
Rules
Rulings
Snowie
Software
Source Code
Strategy--Backgames
Strategy--Bearing Off
Strategy--Checker play
Terminology
Theory
Tournaments
Uncategorized
Variations

 

Return to:  Backgammon Galore : Forum Archive Main Page