/**************************************************************************\
|*                                                                        *|
|*    C-Net DiGits v1.1    Written by: Dotoran, Halloween Night, 1992!    *|
|*                                                                        *|
|*       From an Original Idea by Stephane Edwardson                      *|
|*                                                                        *|
|*          For the BEST in Life: Frontiers BBS (716)/823-9892!           *|
|*                                                                        *|
\**************************************************************************/

options results ; signal on SYNTAX ; signal on ERROR ; signal on IOERR
tr=transmit ; se=sendstring ; gc=getchar ; qu=query

tr '\f1'
tr '\z5                       \z0'
tr '\z5  \z4\cb    DiGits v1.1    \z5  \z0'
tr '\z5                       \z0'
tr '\n1\cfWritten by: Dotoran'
se '\n1\ceNeed Instructions? \cb(\c9y\cb/\c9N\cb)\ce: \c9' ; gc ; a=result
call CHECK ; if a='Y' then do ; tr 'Yes!\n1' ; signal DOCS ; end
tr 'No.\n1'

START:
    tr '\cfUse which \cdGrid \cfsize?\n1'
    tr '\ce3\cbx\ce3\cf, \ce4\cbx\ce4\cf, \ce5\cbx\ce5\cf, \ce6\cbx\ce6\cf, or \ce7\cbx\ce7\n1'
    se '\cdGrid \cfSize? \cb(\ce3\cb/\ce4\cb/\c95\cb/\ce6\cb/\ce7\cb)\cf: \ce'
    yourtotal=0 ; mytotal=0 ; c='\ce' ; gc ; grid=result
    call CHECK ; if grid<3 | grid>7 then do ; c='\c9' ; grid=5 ; end
    se c||grid ; call MAKEGRID ; call FIRSTMOVE ; tr '\f1' ; call SHOWGRID
    if fm=1 then signal MYMOVE ; else signal YOURMOVE

YOURMOVE:
    se '['grid*2+6';3H\cfWhich Row\cc:  \h1\ca' ; gc ; row=result ; call CHECK
    if row='Q' then do ; tr '\c9Quit' ; signal QUIT ; end ; else tr row
    se '  \cfWhich Col\cc:  \h1\cb' ; gc ; col=result ; call CHECK
    if col='Q' then do ; tr '\c9Quit' ; signal QUIT ; end ; else tr col
    if (row~=grow & col~=gcol) | gr.row.col='0' then do
        tr '  \c9Invalid \ceMove\cf...(\cdENTER\cf)\i1' ; call CHECK
        call CLEAR ; signal YOURMOVE ; end
    yourtotal=yourtotal+gr.row.col ; gr.row.col='0'
    grow=row ; gcol=col ; call SHOWGRID ; signal MYMOVE

QUIT:
    se '['grid*2+8';3H\cfWant to \cdQUIT \cbPlaying\cf? \ce(\cdy\ce/\cdN\ce)\cf: \cd'
    gc ; a=result ; call CHECK ; if a~='Y' then do ; tr 'No.\w1'
        call CLEAR ; signal YOURMOVE ; end
    tr 'Yes!' ; signal GOODBYE

MYMOVE:
    do i=9 to 1 by -1
        do j=1 to grid
            if gr.grow.j=i then do ; row=grow ; col=j ; leave i ; end
        end j
        do j=1 to grid
            if gr.j.gcol=i then do ; row=j ; col=gcol ; leave i ; end
        end j
    end i
    tr '['grid*2+6';3H\ceI \cftake the \c9'gr.row.col' \cfat'
    tr '  Row\cc: \ca'row'  \cfCol\cc: \cb'col
    tr '\n1  \cfPress \ceANY KEY \cfnow...\g1' ; call CHECK
    mytotal=mytotal+gr.row.col ; gr.row.col='0'
    grow=row ; gcol=col ; call SHOWGRID ; signal YOURMOVE

FIRSTMOVE:
    se '\n2\cfShould \ceI \cfgo first? \cb(\cey\cb/\ceN\cb)\cf: \ce'
    gc ; a=result ; call CHECK
    if a='Y' then do ; tr 'Yes' ; fm=1 ; return ; end
    tr 'No.' ; fm=0
  return

SHOWGRID:
    gnum='1   2   3   4   5   6   7'
    co=substr('123456789abcdef',random(1,15,time('s')),1)
    tr '\f0\n1    \o1\cf'substr(gnum,1,grid*4)'\c'co'DiGits, v1.0\o0'
    tr '  \c9Ú'copies('ÄÄÄÄ',grid-1)'ÄÄÄ¿' ; mrows='' ; mcols=''
    do i=1 to grid ; hrow='' ; if grow=i then hrow='\z5\cb'
        se '\o1\cf'word(gnum,i)'\o0\c9 ³\ce' ; do j=1 to grid ; hcol=''
        mrows=mrows||gr.grow.i ; mcols=mcols||gr.j.gcol
            if gcol=j then hcol='\z5\cb'
            gr=gr.i.j ; if gr='0' then gr=' '
            se hrow||hcol' 'gr' \z0\ce '
        end j ; hi='' ; if i=grid then tr '\h1\c9³'
            else tr '\h1\c9³\n1  ³'copies('    ',grid-1)'   ³'
    end i
    tr '  À'copies('ÄÄÄÄ',grid-1)'ÄÄÄÙ'
    tr '  \cfRow\cc: \ca'grow'  \cfCol\cc: \cb'gcol ; call CLEAR
    tr '[4;'grid*4+5'H\cfYour Total\cc: \ca'yourtotal'     '
    tr '[6;'grid*4+5'H\cf  My Total\cc: \cb'mytotal'     '
    if mrows+mcols=0 then signal WIN
  return

WIN:
    if yourtotal>mytotal then se '[8;'grid*4+5'H\o1\ceYou Win! \o0'
    if yourtotal<mytotal then se '[8;'grid*4+5'H\o1\caI Win! \o0'
    if yourtotal=mytotal then se '[8;'grid*4+5'H\o1\c9We Tied! \o0'
    se '\cfAgain? \cb(\cdY\cb/\cdn\cb)\cf: \cd' ; gc ; a=result ; call CHECK
    if a='N' then do ; tr 'No.' ; signal GOODBYE ; end
    tr 'Yes!\f1' ; signal START
  exit

MAKEGRID:
    do i=1 to grid
        do j=1 to grid
            gr.i.j=random(1,9,time('s'))
        end j
    end i
    grow=random(1,grid,time('s'))
    gcol=random(1,grid,time('s'))
  return

CLEAR:
    tr '['grid*2+6';1H'copies(' ',40)
    tr copies(' ',40) ; tr copies(' ',40)
    tr copies(' ',40)
  return

GOODBYE:
    tr '\f1\n5\cfFinal Scores\cc:\n1'
    tr '    \cfYou\cc: \ca'yourtotal'   \cfMe\cc: \cb'mytotal
    tr '\n1\cfPlay \c9Again \cfSoon...'
    bufferflush
  exit

DOCS:
    se '   \cfThe game of \ceDiGits \cfwas originally taken from a similar game '
    se 'called \caD-Jits\cf, by \caStephane Edwardson\cf. The game is fairly easy to '
    tr 'play, but can be rather tough to actually win.\n1'
    se '   You are given a grid filled with numbers from \c91 \cfthru \c99\cf. This '
    se 'grid can be between \ca3 \cfand \ca7 \cfblocks wide. One \cecolumn \cfand one \cerow '
    se '\cfwithin the grid will be \cbhighlighted\cf. You may remove any number '
    se 'in any of the highlighted squares and that number will be \cdadded \cf'
    se 'to your score. The location of the selected number will become '
    tr 'the \ceintersection \cfof the next highlighted row and column.\n1'
    se '   Play continues until the \carow \cfand \cacolumn \cehighlighted \cfcontain '
    se '\c9no more numbers\cf, at which time the person with the \cdhighest \cfscore '
    tr 'wins the game.\n1'
    se '   Press \ceany key \cfto select \cdgrid \cfsize...\g1\n2' ; call CHECK
  signal START

CHECK:
    if result~='###PANIC' then return
    tr '\f1\na\q1\cfLoss of Carrier...'
    bufferflush
  exit

SYNTAX:
ERROR:
IOERR:
    tr '\n2*** BOOM *** Error Detected!'
    tr 'ER:'rc' ('errortext(rc)') @ 'sigl
    tr 'Sysop has been notified...'
    logentry 'ER:'rc' ('errortext(rc)') @ 'sigl
    logentry 'while using DiGits'
    bufferflush
  exit

/*                        < END OF FILE MARKER >                         */
