/*                                                                        *\
|*                $VER: Towers, v1.0 (2.6.94) by Dotoran!                 *|
\*                                                                        */
options results ; signal on SYNTAX ; signal on ERROR ; signal on IOERR
tr=transmit ; se=sendstring ; gc=getchar ; gu=getuser ; type=1 ; plates=5

BEGIN: ; call SETUP ; call READSCORES ; call SCREEN
    do i=1 to plates ; tr '['8+(7-plates)+i';'po.1'H'pl.i ; end i

START0: ; if moves=1 then call time('r')
    tr '[19;13Hca'moves'[19;31Hc9'left(typeT.type,15)

START1: ; se '[19;58Hcb' ; gc ; a=result ; call CHECK
    if index('123IHSNRPQ',a)=0 then signal START1
    if a=1 then do ; tr a ; Fpost=1 ; signal START2 ; end
    if a=2 then do ; tr a ; Fpost=2 ; signal START2 ; end
    if a=3 then do ; tr a ; Fpost=3 ; signal START2 ; end
  signal START4

START2: ; se '[19;70Hca' ; gc ; a=result ; call CHECK
    if index('123IHSNRPQ',a)=0 then signal START2
    if a=1 then do ; tr a ; Tpost=1 ; signal START3 ; end
    if a=2 then do ; tr a ; Tpost=2 ; signal START3 ; end
    if a=3 then do ; tr a ; Tpost=3 ; signal START3 ; end
  signal START4

START3: ; Ftot=pt.Fpost ; Ttot=pt.Tpost ; if po.Fpost.plates=0 then BADIN(er.1)
    if Fpost=Tpost then BADIN(er.2) ; Ft=(Plates+1)-Ftot ; Tt=(Plates+1)-Ttot
    plate=po.Fpost.Ft ; Fp=po.Fpost.Ft ; Tp=po.Tpost.Tt ; if Fp>Tp & Tp~=0 then BADIN(er.3)
    tr '['8+(7-plates)+Ft';'po.Fpost'H'pl.0 ; tr '['7+(7-plates)+Tt';'po.Tpost'H'pl.plate
    pt.Fpost=pt.Fpost-1 ; pt.Tpost=pt.Tpost+1 ; Tt=(plates+1)-pt.Tpost
    po.Fpost.Ft=0 ; po.Tpost.Tt=plate ; moves=moves+1
    call CHECKFORWIN ; call CLEAR
  signal START0

START4: ; if a='I' then do ; tr 'Info'    ; signal INFO    ; end
    if a='H' then do ; tr 'Highs'   ; signal HIGHS   ; end
    if a='S' then do ; tr 'Start'   ; signal RESTART ; end
    if a='N' then do ; tr 'New'     ; signal NEWTYPE ; end
    if a='R' then do ; tr 'ReDraw'  ; call REDRAW    ; end
    if a='P' then do ; tr 'Plates'  ; signal PLATES  ; end
    if a='Q' then do ; tr 'Quit'    ; signal QUIT    ; end
  signal MORE

QUIT: ; se '[23;3HcdYou cbreally cdwish to c9QUIT cdthe Game? ce[cbNoce]cd: cb'
    gc ; a=result ; if a='Y' then do ; tr 'Yes!' ; signal MORE ; end
    tr 'No.' ; call CLEAR
  signal START1

RESTART:
    se '[23;3HcdYou cbreally cdwish to c9START OVER cdin the Game? ce[cbNoce]cd: cb'
    gc ; a=result ; if a='Y' then do ; tr 'Yes!' ; signal BEGIN ; end
    tr 'No.' ; call CLEAR
  signal START1

INFO:
    tr '[8;17Hcf<-- cePost 1[8;40Hcf<-- cePost 2[8;63Hcf<-- cePost 3'
    tr '[10;24HcbPlates[10;47HPlates[11;27Hcf|[11;49H|'
    tr '[12;26H/ \[12;48H/ \' ; tr '[21;1H'in.type'[K'
    se '[22;1HcdHowever, no caLarger cbPlate cdmay c9sit atop cda '
    tr 'caSmaller cbPlatecd, and you may only move'
    se '[23;1HceOne cbPlate cdat a time.    Press caENTER cdto continue the game...i1'
    tr '[8;17H          [8;40H          [8;63H          [10;24H      '
    tr '[10;47H      [11;27H [11;49H [12;26H   [12;48H   '
    tr '[20;1H[3J'com ; call CLEAR
  signal START1

HIGHS:
    tr '[1;1Hz5cf'center('The Six Highest Scores in "'typeT.type'" With 'plates' Plates!',76)'z0'
    tr 'cf# caPlayer Handle      cbMoves cdPL  ceTime    cf# caPlayer Handle     cbMoves cdPL  ceTime'
    tr 'c9=====================================  ====================================='
    do i=1 to 3 ; j=i+3 ; parse var hi.type.ht.i h1.i'|'p1.i'|'m1.i'|'t1.i'|'t2.i'|'t3.i'|'jk
    se 'cf'i' ca'left(h1.i,20)' cb'left(m1.i,3)' cd'p1.i' ce't1.i'cf:ce't2.i'cf:ce't3.i'  '
    parse var hi.type.ht.j h1.j'|'p1.j'|'m1.j'|'t1.j'|'t2.j'|'t3.j'|'jk
    tr 'cf'j' ca'left(h1.j,20)' cb'left(m1.j,3)' cd'p1.j' ce't1.j'cf:ce't2.j'cf:ce't3.j'  ' ; end i
    tr 'c9=====================================  ====================================='
    se '[8;32HcdPress[8;41HEnteri1' ; do i=0 to 6 ; tr ti.i ; end i ; tr '[8;32H     [8;41H         ' ; call CLEAR
  signal START0

NEWTYPE:
    se '[23;3Hce[cb1ce] cd1 to 3ce, [cb2ce] cd1-2-3ce, [cb3ce] cd1 to 2ce, [cb4ce] cd1-3-1ce, '
    se '[cb5ce] cd1 to 1ce, [cbRESUMEce]cd: cb'
    gc ; a=result ; if a='R' | c2d(a)=13 | index('12345R',a)=0 then do
        tr 'Resume' ; call CLEAR ; signal START1 ; end ; else do ; tr a ; type=a ; call CLEAR ; end
    se '[23;3Hce[cb1ce] cdResume using cfNEW cdGoalce, [cb2ce] cdReStart using cfNEW cdGoal ce[cbRESUMEce]cd: cb'
    gc ; a=result ; if a='2' then do ; tr 'ReStart' ; signal BEGIN ; end
    tr 'Resume' ; winflag=0 ; call CLEAR
  signal START0

REDRAW: ; call SCREEN
    do i=1 to plates ; pl=po.1.i ; tr '['8+(7-plates)+i';'po.1'H'pl.pl ; end i
    do i=1 to plates ; pl=po.2.i ; tr '['8+(7-plates)+i';'po.2'H'pl.pl ; end i
    do i=1 to plates ; pl=po.3.i ; tr '['8+(7-plates)+i';'po.3'H'pl.pl ; end i
  signal START0

PLATES: ; if moves>0 then do ; BADIN(er.4) ; signal START1 ; end
    se '[23;3HcdNumber of cfPlates cdto Play - ce[cb3ce] [cb4ce] '
    se '[cb5ce] [cb6ce] [cb7ce]cd? ce[cb5ce]cd: cb'
    gc ; a=result ; if a='5' | c2d(a)=13 | index('34567',a)=0 then do
        tr '5' ; call CLEAR ; plates=5 ; signal BEGIN ; end
    tr a ; plates=a
  signal BEGIN

BADIN: ; parse arg text ; se '[23;1H'center(text,81)'w1' ; call CLEAR
  signal START1

GOODIN: ; parse arg text ; do 30 ; se '[23;1H'center(text,81)'[23;1H[K' ; end ; return

CLEAR: ; tr '[23;1H[K[19;58H      [19;70H       ' ; return

UPDATE: ; t=trunc(time('e'),0) ; hrs=right(t%3600,2,'0')
    mins=right(t%60,2,'0') ; secs=right(t-mins*60,2,'0')
  return

CHECKFORWIN: ; a=0 ; do i=1 to plates ; if po.3.i=i then a=a+1 ; end i
    b=0 ; do i=1 to plates ; if po.2.i=i then b=b+1 ; end i
    c=0 ; do i=1 to plates ; if po.1.i=i then c=c+1 ; end i
    if a=plates & type=1 then signal YOUWON
    if b=plates & type=2 then do ; winflag=1 ; call GOODIN(er.10) ; return ; end
    if a=plates & type=2 & winflag=1 then signal YOUWON
    if b=plates & type=3 then signal YOUWON
    if a=plates & type=4 then do ; winflag=1 ; call GOODIN(er.10) ; return ; end
    if c=plates & type=4 & winflag=1 then signal YOUWON
    if c=0 & type=5 then do ; winflag=1 ; call GOODIN(er.10) ; return ; end
    if c=plates & type=5 & winflag=1 then signal YOUWON
  return

YOUWON: ; tr '[19;13Hca'moves
    do i=1 to 7 ; do j=1 to 10 ; tr '['12-j';'j*2+i'H'wi.i ; end j
    do j=1 to 10 ; tr '['1+j';'38+j*2+i'H'wi.i ; end j
    do j=1 to 10 ; tr '['11+j';'60-j*2+i'H'wi.i ; end j
    do j=1 to 10 ; tr '['22-j';'22-j*2+i'H'wi.i ; end j
    end i ; do i=8 to 13 ; tr wi.i ; end i ; call UPDATE ; call CHECKSCORES
    se '[23;3Hc7cdIn ce'hrs'cf:ce'mins'cf:ce'secs' cdand ca'moves
    se ' cdmoves, you solved the game! Play again? ce[cbYesce]cd: cb'
    gc ; a=result ; if a='N' then do ; tr 'No.' ; signal MORE ; end ; else tr 'Yes!'
  signal BEGIN

CHECKSCORES:
    hi.type.ht.7=handle'|'plates'|'moves'|'hrs'|'mins'|'secs'|x'
    good=0 ; do i=6 to 1 by -1 ; j=i+1
    parse var hi.type.ht.i z'|'z'|'a1'|'a2'|'a3'|'a4'|'jk ; h1=a1||a2||a3||a4
    parse var hi.type.ht.j z'|'z'|'a1'|'a2'|'a3'|'a4'|'jk ; h2=a1||a2||a3||a4
    if h1>h2 then do ; t=hi.type.ht.i ; hi.type.ht.i=hi.type.ht.j
        hi.type.ht.j=t ; good=1 ; end
    end i ; if good=1 then do ; call GOODIN(er.11) ; call SAVESCORES ; end
  return

READSCORES: /* Handle | Plates | Moves | Hrs | Mins | Secs | junk */
    if ~exists(df'Towers.Highs') then do
        call open(f1,df'Towers.Highs','w') ; do i=1 to 150
            call writeln(f1,'No One Yet|0|999|00|00|00|x') ; end i
        call close(f1) ; end
    call open(f1,df'Towers.Highs','r')
        do i=1 to 5 ; do j=1 to 5 ; do k=1 to 6
            hi.i.j.k=readln(f1) ; end k ; end j ; end i
    call close(f1)
  return

SAVESCORES:
    call open(f1,df'Towers.Highs','w')
        do i=1 to 5 ; do j=1 to 5 ; do k=1 to 6
            call writeln(f1,hi.i.j.k) ; end k ; end j ; end i
    call close(f1)
  return

SETUP: ; changewhere "Towers, v1.0" ; gu 1 ; handle=result ; gu 28 ; term=result
    if term<2 then do ; tr 'f1[12;10HcaYou need a terminal with IBM/ANSI support to play Towers!w3' ; signal MORE ; end
    po.1=5 ; po.2=28 ; po.3=51 ; moves=0 ; winflag=0 ; ht=plates-2
    do i=1 to plates ; po.1.i=i ; end i ; pt.1=plates ; pt.2=0 ; pt.3=0
    do i=2 to 3 ; do j=1 to plates ; po.i.j=0 ; end j ; end i
    gu 1100454 ; oldmore=result ; setobject 0 ; putuser 1100454 ; gu 1311992 ; df=result
    pl.0='          z3 z0cbÝ          ' ; pl.1='      c1ßÛz1c9ßßz3 z0cbÝz1c9ßßz0c1Ûß      '
    pl.2='     c2ßÛz2caßßßz3 z0cbÝz2caßßßz0c2Ûß     ' ; pl.3='    c3ßÛz3cbßßßßz3 z0cbÝz3cbßßßßz0c3Ûß    '
    pl.4='   c4ßÛz4ccßßßßßz3 z0cbÝz4ccßßßßßz0c4Ûß   ' ; pl.5='  c5ßÛz5cdßßßßßßz3 z0cbÝz5cdßßßßßßz0c5Ûß  '
    pl.6=' c6ßÛz6ceßßßßßßßz3 z0cbÝz6ceßßßßßßßz0c6Ûß ' ; pl.7='c7ßÛz7cfßßßßßßßßz3 z0cbÝz7cfßßßßßßßßz0c7Ûß'
    wi.1='[0m[41m [1;31mÛÜÜÜÜÜÜÜÛÜÜÜÜÜÜÜÛ [0m' ; wi.2='[1;31;42m [32mÛÜÜÜÜÜÜÛÜÜÜÜÜÜÛ [0m'
    wi.3='[1;32;43m [33mÛÜÜÜÜÜÛÜÜÜÜÜÛ [0m' ; wi.4='[1;33;44m [34mÛÜÜÜÜÛÜÜÜÜÛ [0m'
    wi.5='[1;34;45m [35mÛÜÜÜÛÜÜÜÛ [0m' ; wi.6='[1;35;46m [36mÛÜÜÛÜÜÛ [0m' ; wi.7='[1;36;47m [37mÛÜÛÜÛ [0m'
    wi.8='[9;26H[44m                             [0m' ; wi.9='[10;24H[44m         [1;33mCongratulations         [0m'
    wi.10='[11;24H[1;33;44m                                 [0m' ; wi.11='[12;24H[1;33;44m                                 [0m'
    wi.12='[13;24H[1;33;44m'center(handle,33)'[0m' ; wi.13='[14;26H[1;33;44m                             [0m'
    er.1="c9There are no plates on that post!"
    er.2="c9You can't move a plates to the same post!"
    er.3="c9You can't move a larger plate on top of a smaller plate!"
    er.4="c9You can't change the number of plates in the middle of a game!"
    er.10="caHey, you're almost a Winner! The first stage is complete!"
    er.11="cbHey, a NEW High Score! Saving..." ; nums='0 0 three four five six seven'
    typeT.1='The 1 to 3 Game' ; in.1='cdGiven cathree cePosts cdand ca'subword(nums,plates,1)' cbPlatescd, move the c9Pyramid cdfrom cePost 1 cdto cePost 3cd.'
    typeT.2='The 1-2-3 Game'  ; in.2='cdMove the c9Pyramid cdfrom cePost 1 cdto cePost 2cd, then from cePost 2 cdto cePost 3cd.'
    typeT.3='The 1 to 2 Game' ; in.3='cdGiven cathree cePosts cdand ca'subword(nums,plates,1)' cbPlatescd, move the c9Pyramid cdfrom cePost 1 cdto cePost 2cd.'
    typeT.4='The 1-3-1 Game'  ; in.4='cdMove the c9Pyramid cdfrom cePost 1 cdto cePost 3cd, then from cePost 3 cdback to cePost 1cd.'
    typeT.5='The 1 to 1 Game' ; in.5='cdMove the c9Pyramid cbOFF cePost 1cd, then back cbONTO cePost 1cd.'
  return

MORE: ; setobject oldmore ; putuser 1100454 ; exit

SCREEN: ; tr 'f1' ; ti.0='[1;1H[K' ; ti.6='[7;1H[K'
    ti.1='[2;1H        [1;31mÛÛÛÛÛÛ[36mÝ   [31mÛÛÛÛÛ[36mÝ   [31mÛÛ[36mÝ      [31mÛÛ[36mÝ  [31mÛÛÛÛÛÛ[36mÝ  [31mÛÛÛÛÛÛ[36mÝ    [31mÛÛÛÛÛ[36mÝ[K' ; tr ti.1
    ti.2='[3;1H          [31mÛÛ[36mÝ    [31mÛÛ[36mÝ  [31mÛÛ[36mÝ  [31mÛÛ[36mÝ      [31mÛÛ[36mÝ  [31mÛÛ[36mÝ      [31mÛÛ[36mÝ  [31mÛÛ[36mÝ  [31mÛÛ[36mÝ[K' ; tr ti.2
    ti.3='[4;1H          [31mÛÛ[36mÝ    [31mÛÛ[36mÝ  [31mÛÛ[36mÝ  [31mÛÛ[36mÝ  [31mÛ[36mÝ  [31mÛÛ[36mÝ  [31mÛÛÛÛ[36mÝ    [31mÛÛÛÛÛÛ[36mÝ    [31mÛÛÛÛÛ[36mÝ[K' ; tr ti.3
    ti.4='[5;1H          [31mÛÛ[36mÝ    [31mÛÛ[36mÝ  [31mÛÛ[36mÝ  [31mÛÛ[36mÝ [31mÛÛÛ[36mÝ [31mÛÛ[36mÝ  [31mÛÛ[36mÝ      [31mÛÛ[36mÝ  [31mÛÛ[36mÝ       [31mÛÛ[36mÝ[K' ; tr ti.4
    ti.5='[6;1H          [31mÛÛ[36mÝ     [31mÛÛÛÛÛ[36mÝ    [31mÛÛÛÛ[36mÝ[31mÛÛÛÛ[36mÝ   [31mÛÛÛÛÛÛ[36mÝ  [31mÛÛ[36mÝ  [31mÛÛ[36mÝ   [31mÛÛÛÛÛ[36mÝ  [37m1.0[K' ; tr ti.5
    tr 'n1[14C[0;30;43mß[22Cß[22Cß[0m' ; tr '[14C[30;43m [1;33;40mÝ[21C[43m [40mÝ[21C[43m [40mÝ'
    tr '[14C[43m [40mÝ[21C[43m [40mÝ[21C[43m [40mÝ' ; tr '[14C[43m [40mÝ[21C[43m [40mÝ[21C[43m [40mÝ'
    tr '[14C[43m [40mÝ[21C[43m [40mÝ[21C[43m [40mÝ' ; tr '[14C[43m [40mÝ[21C[43m [40mÝ[21C[43m [40mÝ'
    tr '[14C[43m [40mÝ[21C[43m [40mÝ[21C[43m [40mÝ' ; tr '[14C[43m [40mÝ[21C[43m [40mÝ[21C[43m [40mÝ'
    tr '  [0;30;43mß   [32mÄÄÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÄÄ   [30mß[0m'
    tr '[1;33mÜ[43m [0;36;43mÄÄÍÍÍÍÍÍÍÍÍz6c4 1 z3c6ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍz6c4 2 z3c6ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍz6c4 3 z3c6ÍÍÍÍÍÍÍÍÍÍÄÄ [1;33;40mÜ'
    tr 'ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß'
    tr '     cdMovesce:[13CcdGamece:[22CcdFromce:[9CcdToce:n1'
    com='[21;1H  ce[cbIce]cdnfoce, [cbHce]cdighsce, [cbSce]cdtart Overce, '
    com=com||'[cbNce]cdew Typece, [cbRce]cdeDrawce, [cbPce]cdlatesce, [cbQce]cduit[K' ; tr com
  return

CHECK: ; if result~='###PANIC' then return
    bufferflush ; tr 'f1[12;30Hc9Loss of Carrier!' ; signal MORE

SYNTAX: ; ERROR: ; IOERR:
    tr 'n2ce*** c9BOOM ce*** c9Error Detectedce!' ; tr 'c6ERc9:cb'rc' c6(ca'errortext(rc)'c6) @ cf'sigl
    tr 'cdSysop has been notified...' ; logentry 'ER:'rc' ('errortext(rc)') @ 'sigl
    logentry 'while using Towers, v1.0' ; bufferflush
  signal MORE

/** END *******************************************************************\
\***************************************** Frontiers BBS! (716)/823-9892 **/
