jj logo
Take a quick tour of JJ
Teacher Registration
Student Registration
Industry Registration
Guest Registration
Some Humor
Friends
Quotes


Name
Password
School


..
Class TicTacToeUsingArrays

Import JJGui

Class TicTacToeUsingArrays
--Name: ??  --the name (replace '??') has to match the login name
 Box   buttons                    ofClass JJButton[] is private
 Box   ta                         ofClass JJTextArea is private
 Box   gp                         ofClass JJGridPanel is private
 Box   bp                         ofClass JJBorderPanel is private
 Box   whoseTurn                  ofClass Str is private

 Constructor TicTacToeUsingArrays(none) is public
  Box i ofType int

   --allocate an array and loop thru New buttons
   NewArray buttons ofClass JJButton[9]
   Set i = 0
   Repeat
    New buttons[i] ofClass JJButton     with ("")
    Inc i by 1
   ExitOn (i == 9)
   EndRepeat
   New ta ofClass JJTextArea   with ("X's turn: To Start, click a button",2,34)
   New gp ofClass JJGridPanel with (3,3)
   New bp ofClass JJBorderPanel
   Set whoseTurn = "X"
 EndConstructor TicTacToeUsingArrays

 Routine ActsAsMain(none) is public
  Box i ofType int

   Start -- tells JJ that this is the "main"

   --loop thru adding buttons to panel
   Set i = 0
   Repeat
    Call gp.add with (buttons[i])
    Inc i by 1
   ExitOn (i == 9)
   EndRepeat

   Call bp.add with ("Center", gp)
   Call bp.add with ("South", ta)
   Call bp.jjShowAsMainPanel
 EndRoutine ActsAsMain

 Routine jjHandleButtonPress(b) is public --Always needed
  Slot b ofClass JJButton --Always the same ...
  --  ...although, the name (b) could be different
 
  Box ok ofType bool
  Box i ofType int
  Box buttonLabel ofClass Str
  Box bCurr ofClass JJButton
   Set ok = false

   --loop thru buttons until finding the pressed one
   Set i = 0
   Repeat
    Set bCurr = buttons[i]
    If (b == bCurr) then
       Set buttonLabel = bCurr.getLabel()
       If (buttonLabel.length() == 0) then
          Set ok = true
          Call bCurr.setLabel with (whoseTurn)
       Else
          Call ta.setText with ("*** ERROR *** Button already pressed")
       EndIf
    EndIf
    Inc i by 1
   ExitOn (i == 9)
   EndRepeat

   If (ok) then
    If whoseTurn.equals("X") then
     Set whoseTurn = "O"
     Call ta.setText with ("O's next: Please click a button")
    Else
     Set whoseTurn = "X"
     Call ta.setText with ("X's next: Please click a button")
    EndIf
   EndIf
 EndRoutine jjHandleButtonPress
EndClass TicTacToeUsingArrays
Return to
    JJ
home page
..