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


Name
Password
School


..
Class TicTacToeNoArrays

Import JJGui

Class TicTacToeNoArrays
--Name: ??
Boxes b1,b2,b3,b4,b5,b6,b7,b8,b9 ofClass JJButton are 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 TicTacToeNoArrays(none) is public
 New b1 ofClass JJButton     with ("")
 New b2 ofClass JJButton     with ("")
 New b3 ofClass JJButton     with ("")
 New b4 ofClass JJButton     with ("")
 New b5 ofClass JJButton     with ("")
 New b6 ofClass JJButton     with ("")
 New b7 ofClass JJButton     with ("")
 New b8 ofClass JJButton     with ("")
 New b9 ofClass JJButton     with ("")
 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 TicTacToeNoArrays

 Routine ActsAsMain(none) is public
   Start -- tells JJ that this is the "main"

   Call gp.add with (b1)
   Call gp.add with (b2)
   Call gp.add with (b3)
   Call gp.add with (b4)
   Call gp.add with (b5)
   Call gp.add with (b6)
   Call gp.add with (b7)
   Call gp.add with (b8)
   Call gp.add with (b9)

   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 buttonLabel ofClass Str
 Set ok = false

 If (b == b1) then
    Set buttonLabel = b1.getLabel()
    If (buttonLabel.length() == 0) then
       Set ok = true
       Call b1.setLabel with (whoseTurn)
    Else
       Call ta.setText with ("*** ERROR *** Button already pressed")
    EndIf
 ElseIf (b == b2) then
    Set buttonLabel = b2.getLabel()
    If (buttonLabel.length() == 0) then
       Set ok = true
       Call b2.setLabel with (whoseTurn)
    Else
       Call ta.setText with ("*** ERROR *** Button already pressed")
    EndIf
 ElseIf (b == b3) then
    Set buttonLabel = b3.getLabel()
    If (buttonLabel.length() == 0) then
       Set ok = true
       Call b3.setLabel with (whoseTurn)
    Else
       Call ta.setText with ("*** ERROR *** Button already pressed")
    EndIf
 ElseIf (b == b4) then
    Set buttonLabel = b4.getLabel()
    If (buttonLabel.length() == 0) then
       Set ok = true
       Call b4.setLabel with (whoseTurn)
    Else
       Call ta.setText with ("*** ERROR *** Button already pressed")
    EndIf
 ElseIf (b == b5) then
    Set buttonLabel = b5.getLabel()
    If (buttonLabel.length() == 0) then
       Set ok = true
       Call b5.setLabel with (whoseTurn)
    Else
       Call ta.setText with ("*** ERROR *** Button already pressed")
    EndIf
 ElseIf (b == b6) then
    Set buttonLabel = b6.getLabel()
    If (buttonLabel.length() == 0) then
       Set ok = true
       Call b6.setLabel with (whoseTurn)
    Else
       Call ta.setText with ("*** ERROR *** Button already pressed")
    EndIf
 ElseIf (b == b7) then
    Set buttonLabel = b7.getLabel()
    If (buttonLabel.length() == 0) then
       Set ok = true
       Call b7.setLabel with (whoseTurn)
    Else
       Call ta.setText with ("*** ERROR *** Button already pressed")
    EndIf
 ElseIf (b == b8) then
    Set buttonLabel = b8.getLabel()
    If (buttonLabel.length() == 0) then
       Set ok = true
       Call b8.setLabel with (whoseTurn)
    Else
       Call ta.setText with ("*** ERROR *** Button already pressed")
    EndIf
 ElseIf (b == b9) then
    Set buttonLabel = b9.getLabel()
    If (buttonLabel.length() == 0) then
       Set ok = true
       Call b9.setLabel with (whoseTurn)
    Else
       Call ta.setText with ("*** ERROR *** Button already pressed")
    EndIf
 Else
    Call ta.setText with ("Internal ERROR")
 EndIf

 If (ok) then
  If whoseTurn.equals("X") then
    Set whoseTurn = "O"
    Call ta.setText with ("O's next: Click a button")
  Else
    Set whoseTurn = "X"
    Call ta.setText with ("X's next: Click a button")
  EndIf
 EndIf
EndRoutine jjHandleButtonPress

EndClass TicTacToeNoArrays
Return to
    JJ
home page
..