' Main Activity
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.Show
InitializeGame
End Sub
Dim Board(2, 2) As String
Dim CurrentPlayer As String
Dim GameOver As Boolean
Sub InitializeGame
For i = 0 To 2
For j = 0 To 2
Board(i, j) = ""
Next
Next
CurrentPlayer = "X"
GameOver = False
DrawBoard
End Sub
Sub DrawBoard
MainForm.Controls.Clear
Dim ButtonWidth As Int = 100
Dim ButtonHeight As Int = 100
For i = 0 To 2
For j = 0 To 2
Dim btn As Button
btn.Initialize("btn_" & i & "_" & j)
btn.Width = ButtonWidth
btn.Height = ButtonHeight
btn.Text = Board(i, j)
btn.Top = i * ButtonHeight
btn.Left = j * ButtonWidth
MainForm.Controls.Add(btn)
Next
Next
End Sub
Sub btn_Click (Tag As String)
If GameOver Then Return
Dim coords() As String = Regex.Split("_", Tag)
Dim x As Int = coords(1)
Dim y As Int = coords(2)
If Board(x, y) = "" Then
Board(x, y) = CurrentPlayer
If CheckWin(CurrentPlayer) Then
Msgbox(CurrentPlayer & " wins!", "Game Over")
GameOver = True
Else If IsDraw Then
Msgbox("It's a draw!", "Game Over")
GameOver = True
Else
CurrentPlayer = If(CurrentPlayer = "X", "O", "X")
End If
DrawBoard
End If
End Sub
Sub CheckWin(Player As String) As Boolean
' Check Rows and Columns
For i = 0 To 2
If (Board(i, 0) = Player And Board(i, 1) = Player And Board(i, 2) = Player) Or _
(Board(0, i) = Player And Board(1, i) = Player And Board(2, i) = Player) Then
Return True
End If
Next
' Check Diagonals
If (Board(0, 0) = Player And Board(1, 1) = Player And Board(2, 2) = Player) Or _
(Board(0, 2) = Player And Board(1, 1) = Player And Board(2, 0) = Player) Then
Return True
End If
Return False
End Sub
Sub IsDraw As Boolean
For i = 0 To 2
For j = 0 To 2
If Board(i, j) = "" Then Return False
Next
Next
Return True
End Sub