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