home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / AmiSlate-Source / SlateRexx / tictactoe.rexx < prev   
Encoding:
OS/2 REXX Batch file  |  1995-08-05  |  15.4 KB  |  538 lines

  1. /* TicTacToe for AmiSlate v1.0! */
  2.  
  3. /* Constants for use with AmiSlate's ARexx interface */
  4. AMode.DOT      =  0 
  5. AMode.PEN      =  1 
  6. AMode.LINE     =  2 
  7. AMode.CIRCLE   =  3 
  8. AMode.SQUARE   =  4 
  9. AMode.POLY     =  5 
  10. AMode.FLOOD    =  6 
  11. AMode.CLEAR    =  7 
  12.  
  13. AMessage.TIMEOUT     = 1    /* No events occurred in specified time period */
  14. AMessage.MESSAGE     = 2    /* Message recieved from remote Amiga */
  15. AMessage.MOUSEDOWN   = 4    /* Left mouse button press in drawing area */
  16. AMessage.MOUSEUP     = 8    /* Left mouse button release in drawing area */
  17. AMessage.RESIZE      = 16    /* Window was resized--time to redraw screen? */ 
  18. AMessage.QUIT        = 32    /* AmiSlate is shutting down */
  19. AMessage.CONNECT     = 64    /* Connection established */
  20. AMessage.DISCONNECT  = 128    /* Connection broken */
  21. AMessage.TOOLSELECT  = 256    /* Tool Selected */
  22. AMessage.COLORSELECT = 512    /* Palette Color selected */
  23. AMessage.KEYPRESS    = 1024    /* Key pressed */
  24. AMessage.MOUSEMOVE   = 2048     /* Mouse was moved */
  25.  
  26. /* Get our host's name--always given as first argument when run from Amislate */
  27. parse arg CommandPort ActiveString
  28.  
  29. if (length(CommandPort) == 0) then do
  30.     say ""
  31.     say "Usage:  rx tictactoe.rexx <REXXPORTNAME>"
  32.     say "        (REXXPORTNAME is usually AMISLATE)"
  33.     say ""
  34.     say "Or run from the Rexx menu within AmiSlate."
  35.     say ""
  36.     exit 0
  37.     end
  38.  
  39. /* Send all commands to this host */
  40. address (CommandPort) 
  41.  
  42.  
  43. options results
  44.  
  45. /* Reserves pixels for a future toolbar -- currently, none */
  46. ToolBarHeight = 0
  47.  
  48. /* Check to see which tool is selected, whether we are connected */
  49. BFlood = 0
  50.  
  51. /* Parse command line argument to see if we've been activated by 
  52.    a remote request or a local user */
  53. check = upper(left(ActiveString,3))
  54. if (upper(left(ActiveString,3)) ~= 'RE') then 
  55.     do
  56.         BActive = 1
  57.     end
  58.     else
  59.     do    
  60.         BActive = 2
  61.     end
  62.  
  63. /* See if we're connected */
  64. GetRemoteStateAttrs stem rstateattrs.
  65.  
  66. if (rstateattrs.mode > -1) then 
  67.     do
  68.         BConnectMode = 1
  69.     end
  70.     else
  71.     do
  72.         BConnectMode = 0
  73.     end
  74.     
  75. /* Disable drawing */
  76. lock on
  77.  
  78. /* Initialize TicTacToe board */
  79. success = InitTTTArray()
  80.  
  81. /* Initiator (X) goes first */
  82. turn = 1
  83. moves = 0
  84.  
  85. /* Handshaking for two-computer game */
  86. if (BConnectMode = 1) then 
  87. do
  88.     if (BActive == 1) then 
  89.     do
  90.  
  91.         SetWindowTitle '"'||"Requesting game from remote user"||'"' 
  92.     RemoteRexxCommand '"'||"Would you like to play TicTacToe?"||'"' "slaterexx:TicTacToe.rexx"
  93.     
  94.         waitevent stem handshake. MESSAGE
  95.         if (handshake.message == 0) then 
  96.         do
  97.             SetWindowTitle '"'||"TicTacToe Game Refused"||'"'
  98.             exit
  99.         end
  100.     success = DrawTTTBoard()
  101.     end
  102.     else
  103.     do
  104.         /* Examine window to get dimensions */
  105.     GetWindowAttrs stem winattrs.
  106.        BoardWidth = winattrs.width  - 58
  107.        BoardHeight= winattrs.height - 53 - ToolBarHeight
  108.     end
  109. end
  110. else 
  111. do
  112.     success = DrawTTTBoard()
  113. end        
  114.  
  115. success = UpdateStatus()
  116. do while(1)    
  117.     waitevent stem event. RESIZE MOUSEUP MESSAGE TOOLSELECT DISCONNECT QUIT
  118.     if ((event.type == AMessage.TOOLSELECT)&(event.code1 = AMode.CLEAR)) then do
  119.         SetWindowTitle '"'||"Starting New Game"||'"'
  120.         success = InitTTTArray()
  121.         success = DrawTTTBoard()
  122.         
  123.         /* Tell partner that the board has been cleared */
  124.         if (BConnectMode == 1) then SendMessage 99
  125.         end
  126.         
  127.     if (event.type == AMessage.DISCONNECT) then BConnectMode = 0
  128.     if (event.type == AMessage.QUIT) then exit
  129.     if (event.type == AMessage.RESIZE) then do
  130.         if ((BActive == 1)|(BConnectMode == 0)) then do
  131.            success = DrawTTTBoard()
  132.         end
  133.         else do
  134.            /* Just examine window to get new dimensions */
  135.            GetWindowAttrs stem winattrs.
  136.            BoardWidth = winattrs.width  - 58
  137.            BoardHeight= winattrs.height - 53 - ToolBarHeight
  138.         end
  139.              success = UpdateStatus()
  140.     end
  141.         
  142.     if (event.type == AMessage.MESSAGE) then do
  143.         if (event.message == 99) then do
  144.                 success = InitTTTArray()
  145.                 success = UpdateStatus()
  146.             end
  147.             else do
  148.                 if (turn ~= BActive) then success = ParseMove(event.message)
  149.             end
  150.             end
  151.             
  152.     if ((moves < 9)&((event.type == AMessage.MOUSEUP)&((turn == BActive)|(BConnectMode == 0)))) then 
  153.     do
  154.         xx = 3     /* default */
  155.         if (event.x < (2*(BoardWidth / 3))) then xx = 2
  156.         if (event.x < (BoardWidth / 3)) then xx = 1
  157.         
  158.         yy = 3     /* default */
  159.         if (event.y < (2*(BoardHeight / 3))) then yy = 2
  160.         if (event.y < (BoardHeight / 3)) then yy = 1
  161.         
  162.         if (TTTBoard.xx.yy > 0) then do
  163.             SetWindowTitle '"'||"You can't move there!"||'"'
  164.             end
  165.         else do
  166.             success = DoMove(xx,yy)
  167.             end
  168.     end
  169. end
  170.  
  171. exit
  172.  
  173. /* --------------------------------------------------------------- */
  174. /* procedure DoMove                           */
  175. /* --------------------------------------------------------------- */
  176. DoMove: procedure expose TTTBoard. turn moves BConnectMode BActive BoardWidth BoardHeight ToolBarHeight
  177.     parse arg xx,yy 
  178.     
  179.     TTTBoard.xx.yy = turn 
  180.  
  181.     if ((BConnectMode == 0)|(turn == BActive)) then success = DrawMove(xx,yy)
  182.     if ((BConnectMode == 1)&(turn == BActive)) then do
  183.         message = xx||yy
  184.         SendMessage message
  185.         end    
  186.                 
  187.     if (turn == 1) then do
  188.         turn = 2
  189.         end
  190.         else do
  191.         turn = 1
  192.         end
  193.     moves=moves+1
  194.     success = CheckForWins()
  195.     if ((success > 0)|(moves>=9)) then do
  196.         moves = 9 /* disallow more movement */
  197.         if (success == 1) then SetWindowTitle '"'||"X's won!  Click CLR to play again" ||'"'
  198.         if (success == 2) then SetWindowTitle '"'||"O's won!  Click CLR to play again" ||'"'
  199.         if (success == 0) then SetWindowTitle '"'||"Cat's game!  Click CLR to play again" ||'"'
  200.         end
  201.         else do
  202.         success = UpdateStatus()
  203.         end
  204.     return 1
  205.     
  206.  
  207. /* --------------------------------------------------------------- */
  208. /* procedure CheckForWins                       */
  209. /* --------------------------------------------------------------- */
  210. CheckForWins: procedure expose TTTBoard. BoardWidth BoardHeight ToolBarHeight turn BActive BConnectMode
  211.  
  212.     i=1
  213.     do while (i<4)
  214.         if ((TTTBoard.i.1==1)&(TTTBoard.i.2==1)&(TTTBoard.i.3==1)) then do
  215.             if ((turn == BActive)|(BConnectMode == 0)) then do
  216.                 SetFColor 15 15 15 notbackground
  217.                 BHeight = BoardHeight - ToolBarHeight
  218.                 if (i==1) then barleft = trunc(BoardWidth*.13)
  219.                 if (i==2) then barleft = trunc(BoardWidth*.47)
  220.                 if (i==3) then barleft = trunc(BoardWidth*.82)
  221.                 square barleft ToolBarHeight+trunc(BHeight*.05) (barleft+trunc(BoardWidth*.05)) (ToolBarHeight+trunc(BHeight*.95)) fill
  222.                 SetFColor 0 0 0 notbackground
  223.                 square barleft ToolBarHeight+trunc(BHeight*.05) (barleft+trunc(BoardWidth*.05)) (ToolBarHeight+trunc(BHeight*.95))
  224.                 end
  225.             return 1
  226.             end
  227.             
  228.         if ((TTTBoard.i.1==2)&(TTTBoard.i.2==2)&(TTTBoard.i.3==2)) then do
  229.             if ((turn == BActive)|(BConnectMode == 0)) then do
  230.                 SetFColor 15 15 15 notbackground
  231.                 BHeight = BoardHeight - ToolBarHeight
  232.                 if (i==1) then barleft = trunc(BoardWidth*.13)
  233.                 if (i==2) then barleft = trunc(BoardWidth*.47)
  234.                 if (i==3) then barleft = trunc(BoardWidth*.82)
  235.                 square barleft ToolBarHeight+trunc(BHeight*.05) (barleft+trunc(BoardWidth*.05)) (ToolBarHeight+trunc(BHeight*.95)) fill
  236.                 SetFColor 0 0 0 notbackground
  237.                 square barleft ToolBarHeight+trunc(BHeight*.05) (barleft+trunc(BoardWidth*.05)) (ToolBarHeight+trunc(BHeight*.95))
  238.                 end
  239.             return 2
  240.             end
  241.             
  242.         i = i + 1
  243.         end
  244.     i=1
  245.     do while (i<4)
  246.         if ((TTTBoard.1.i==1)&(TTTBoard.2.i==1)&(TTTBoard.3.i==1)) then do
  247.             if ((turn == BActive)|(BConnectMode == 0)) then do
  248.                 SetFColor 15 15 15 notbackground
  249.                 BHeight = BoardHeight - ToolBarHeight
  250.                 if (i==1) then bartop = trunc((BHeight*.13)+ToolBarHeight)
  251.                 if (i==2) then bartop = trunc((BHeight*.47)+ToolBarHeight)
  252.                 if (i==3) then bartop = trunc((BHeight*.82)+ToolBarHeight)
  253.                 square trunc(BoardWidth*.05) bartop trunc(BoardWidth*.95) (bartop+trunc(BHeight/20)) fill
  254.                 SetFColor 0 0 0 notbackground
  255.                 square trunc(BoardWidth*.05) bartop trunc(BoardWidth*.95) (bartop+trunc(BHeight/20))
  256.                 end
  257.             return 1
  258.             end
  259.  
  260.         if ((TTTBoard.1.i==2)&(TTTBoard.2.i==2)&(TTTBoard.3.i==2)) then do
  261.             if ((turn == BActive)|(BConnectMode == 0)) then do
  262.                 SetFColor 15 15 15 notbackground
  263.                 BHeight = BoardHeight - ToolBarHeight
  264.                 if (i==1) then bartop = trunc((BHeight*.13)+ToolBarHeight)
  265.                 if (i==2) then bartop = trunc((BHeight*.47)+ToolBarHeight)
  266.                 if (i==3) then bartop = trunc((BHeight*.82)+ToolBarHeight)
  267.                 square trunc(BoardWidth*.05) bartop trunc(BoardWidth*.95) (bartop+trunc(BHeight/20)) fill
  268.                 SetFColor 0 0 0 notbackground
  269.                 square trunc(BoardWidth*.05) bartop trunc(BoardWidth*.95) (bartop+trunc(BHeight/20))
  270.                 end
  271.             return 2
  272.             end
  273.             
  274.         i = i + 1
  275.         end
  276.  
  277.     if ((TTTBoard.1.1==1)&(TTTBoard.2.2==1)&(TTTBoard.3.3==1)) then do
  278.         n = DrawDiagWinLine(1)
  279.         return 1
  280.         end
  281.         
  282.     if ((TTTBoard.1.1==2)&(TTTBoard.2.2==2)&(TTTBoard.3.3==2)) then do
  283.         n = DrawDiagWinLine(1)
  284.         return 2
  285.         end
  286.         
  287.     if ((TTTBoard.3.1==1)&(TTTBoard.2.2==1)&(TTTBoard.1.3==1)) then do
  288.         n = DrawDiagWinLine(0)
  289.         return 1
  290.         end
  291.         
  292.     if ((TTTBoard.3.1==2)&(TTTBoard.2.2==2)&(TTTBoard.1.3==2)) then do
  293.         n = DrawDiagWinLine(0)
  294.         return 2
  295.         end
  296.     
  297.     return 0
  298.  
  299.  
  300. /* --------------------------------------------------------------- */
  301. /* procedure DrawDiagWinLine                       */
  302. /*                                   */
  303. /* Draws a diagonal win line.  If TLtoBR is 1, draws it from the   */
  304. /* top left to the bottom right.  Otherwise, draws it from the     */
  305. /* bottom left to the top right.                     */
  306. /*                                   */
  307. /* --------------------------------------------------------------- */
  308. DrawDiagWinLine: procedure expose BActive turn BoardWidth BoardHeight ToolBarHeight TTTBoard.
  309.     parse arg TLtoBR
  310.  
  311.     cur = 0
  312.     maxcur = trunc(BoardWidth * .025)
  313.     BHeight = BoardHeight - ToolBarHeight
  314.     startX = trunc(BoardWidth * 0.05)
  315.     
  316.     if (TLtoBR == 1) then do
  317.         startY = trunc(BHeight * .1) + ToolBarHeight
  318.         dY = -1
  319.         end
  320.     else do    
  321.         startY = trunc(BHeight * .05) + trunc((BHeight*.83)+ToolBarHeight)
  322.         dY = 1
  323.         end
  324.     
  325.     setfcolor 15 15 15 notbackground
  326.     endX = startX + trunc(BoardWidth * .83)
  327.     endY = startY + trunc((-dY)*trunc(BHeight * .83))
  328.  
  329.     osX = startX
  330.     osY = startY
  331.     oeX = endX
  332.     oeY = endY
  333.     
  334.     do while (cur < maxcur)
  335.         line startX startY endX endY
  336.         line (startX+1) startY (endX+1) endY
  337.  
  338.         startX = startX + 1
  339.         startY = startY + dY
  340.         endX = endX + 1
  341.         endY = endY + dY
  342.         cur = cur + 1
  343.         end
  344.         
  345.     setfcolor 0 0 0 notbackground 
  346.     line oeX+1 oeY endx+1 endy
  347.     line osX osY startX startY
  348.  
  349.     line startx starty endX endY
  350.     line osX osY oeX oeY
  351.     
  352.     return 1
  353.  
  354.     
  355.     
  356. /* --------------------------------------------------------------- */
  357. /* procedure DrawMove                           */
  358. /* --------------------------------------------------------------- */
  359. DrawMove: procedure expose BActive turn BoardWidth BoardHeight ToolBarHeight TTTBoard.
  360.     parse arg xx,yy
  361.  
  362.     if (TTTBoard.xx.yy == 0) then return 1
  363.     
  364.     BHeight = BoardHeight - ToolBarHeight
  365.     
  366.     if (yy == 1) then do
  367.         ytop = ToolBarHeight
  368.         ybot = trunc(BHeight*.3)+ToolBarHeight
  369.         end
  370.         
  371.     if (yy == 2) then do
  372.         ytop = trunc(BHeight*.36)+ToolBarHeight
  373.         ybot = trunc(BHeight*.63)+ToolBarHeight
  374.         end
  375.         
  376.     if (yy == 3) then do
  377.         ytop = trunc(BHeight*.69)+ToolBarHeight 
  378.         ybot = trunc(BHeight*.99)+ToolBarHeight
  379.         end
  380.         
  381.     if (xx == 1) then do
  382.         xleft = 1
  383.         xright = trunc(BoardWidth*.3)
  384.         end
  385.         
  386.     if (xx == 2) then do
  387.         xleft = trunc(BoardWidth*.36)
  388.         xright = trunc(BoardWidth*.63)
  389.         end
  390.         
  391.     if (xx == 3) then do
  392.         xleft = trunc(BoardWidth*.69)
  393.         xright = trunc(BoardWidth*.99)
  394.         end
  395.         
  396. /*    square xleft ytop xright ybot fill */
  397.     if (TTTBoard.xx.yy == 1) then do
  398.         penreset
  399.         height = ybot - ytop
  400.         width  = xright - xleft
  401.         th = 3
  402.  
  403.         SetFColor 0 0 0 notbackground
  404.         pen trunc(xleft+(width/th))     ytop
  405.         pen trunc(xleft+(width/2))     trunc(ytop+(height/th))
  406.         pen trunc(xright-(width/th))     ytop
  407.         pen xright             trunc(ytop+(height/th))
  408.         pen trunc(xright-(width/th))     trunc(ytop+(height/2))
  409.         pen xright             trunc(ybot-(height/th))
  410.         pen trunc(xright-(width/th))     ybot
  411.         pen trunc(xright-(width/2))     trunc(ybot-(height/th))
  412.         pen trunc(xleft+(width/th))     ybot
  413.         pen xleft             trunc(ybot-(height/th))
  414.         pen trunc(xleft+(width/th))     trunc(ybot-(height/2))
  415.         pen xleft             trunc(ytop+(height/th))
  416.         pen trunc(xleft+(width/th))    ytop
  417.  
  418.         SetFColor 15 0 0 notbackground
  419.         
  420.         flood trunc((xleft + xright)/2) trunc((ytop + ybot)/2)
  421.         end
  422.     else do
  423.         SetFColor 0 0 0 notbackground
  424.         circle trunc((xleft+xright)/2) trunc((ytop+ybot)/2) trunc((xright - xleft)/2) trunc((ybot - ytop)/2)
  425.         circle trunc((xleft+xright)/2) trunc((ytop+ybot)/2) trunc((xright - xleft)/3) trunc((ybot - ytop)/3)
  426.  
  427.         SetFColor 0 15 0 notbackground
  428.         flood trunc(((xleft+xright)/2)+((xleft-xright)/2.5)) trunc((ytop+ybot)/2)
  429.         end
  430.     return 1
  431.     
  432.         
  433.  
  434.  
  435. /* --------------------------------------------------------------- */
  436. /* procedure UpdateStatus                       */
  437. /* --------------------------------------------------------------- */
  438. UpdateStatus: procedure expose BActive turn BConnectMode moves
  439.  
  440.     if (moves > 8) then do
  441.         SetWindowTitle '"'||"Game Over, click CLR to play again"||'"'
  442.         return 1
  443.         end
  444.         
  445.     if ((BActive == turn)|(BConnectMode == 0)) then do
  446.         if (turn == 1) then SetWindowTitle '"'||"It's Your Turn, Player X"||'"'
  447.         if (turn == 2) then SetWindowTitle '"'||"It's Your Turn, Player O"||'"'
  448.     end
  449.     else
  450.     do
  451.         if (turn == 1) then SetWindowTitle '"'||"It's Their Turn (Player X)"||'"'
  452.         if (turn == 2) then SetWindowTitle '"'||"It's Their Turn (Player O)"||'"'
  453.     end
  454.     
  455.     return 1
  456.  
  457. /* --------------------------------------------------------------- */
  458. /* procedure InitTTTArray                       */
  459. /* --------------------------------------------------------------- */
  460. InitTTTArray: procedure expose TTTBoard. moves turn
  461.     TTTBoard.1.1 = 0
  462.     TTTBoard.1.2 = 0
  463.     TTTBoard.1.3 = 0
  464.     TTTBoard.2.1 = 0
  465.     TTTBoard.2.2 = 0
  466.     TTTBoard.2.3 = 0
  467.     TTTBoard.3.1 = 0
  468.     TTTBoard.3.2 = 0
  469.     TTTBoard.3.3 = 0
  470.     turn  = 1
  471.     moves = 0
  472.     return 1
  473.     
  474.  
  475. /* --------------------------------------------------------------- */
  476. /* procedure DrawTTTBoard                       */
  477. /* --------------------------------------------------------------- */
  478. DrawTTTBoard: procedure expose TTTBoard. BoardWidth BoardHeight ToolBarHeight turn BActive moves BConnectMode
  479.  
  480.    /* Say what we're doing */
  481.    SetWindowTitle '"'||"Drawing TicTacToe board, Please Wait"||'"'
  482.    SetRemoteWindowTitle '"'||"Drawing TicTacToe board, Please Wait"||'"'
  483.    
  484.    /* Examine window to get dimensions */
  485.    GetWindowAttrs stem winattrs.
  486.    BoardWidth = winattrs.width  - 58
  487.    BoardHeight= winattrs.height - 53
  488.  
  489.    /* Clear Screen */
  490.    clear
  491.  
  492.    /* Draw Board */
  493.    SetFColor 0 0 0 notbackground
  494.  
  495.    /* Height of Board */
  496.    BHeight = BoardHeight - ToolBarHeight   
  497.    
  498.    square (trunc(BoardWidth*.31)) ToolBarHeight (trunc(BoardWidth*.35)) BoardHeight fill
  499.    square (trunc(BoardWidth*.64)) ToolBarHeight (trunc(BoardWidth*.68)) BoardHeight fill
  500.    square 0 (trunc(BHeight*.31)+ToolBarHeight) BoardWidth (trunc(BHeight*.35)+ToolBarHeight) fill
  501.    square 0 (trunc(BHeight*.64)+ToolBarHeight) BoardWidth (trunc(BHeight*.68)+ToolBarHeight) fill
  502.    
  503.    success=DrawMove(1,1)
  504.    success=DrawMove(2,1)
  505.    success=DrawMove(3,1)
  506.    success=DrawMove(1,2)
  507.    success=DrawMove(2,2)
  508.    success=DrawMove(3,2)
  509.    success=DrawMove(1,3)
  510.    success=DrawMove(2,3)
  511.    success=DrawMove(3,3)
  512.  
  513.    success=CheckForWins()
  514.    if (success == 0) then success=UpdateStatus()
  515.    return 1
  516.    
  517.  
  518. /* --------------------------------------------------------------- */
  519. /* procedure ParseMove                           */
  520. /* --------------------------------------------------------------- */
  521. ParseMove: procedure expose TTTBoard. BoardWidth BoardHeight ToolBarHeight turn moves BActive BConnectMode
  522.     parse arg message
  523.  
  524.     xx=left(message,1)
  525.     yy=right(message,1)
  526.  
  527.     if ((xx>3)||(xx<0)|(yy>3)||(yy<0)) then do
  528.         SetWindowTitle '"'||"TicTacToe Transmission Trouble :("||'"'
  529.         return 0
  530.         end
  531.         
  532.     if (TTTBoard.xx.yy > 0) then do
  533.         EasyRequest '"'||"TicTacToe Message"||'"' '"'||"Your opponent is cheating (" || xx yy TTTBoard.xx.yy || ") !"||'"' '"'||"What a maroon"||'"'
  534.     end
  535.     else do
  536.         success=DoMove(xx,yy) 
  537.     end
  538.     return 1