Hi guys, i read an article from <http://www.scratchprojects.com/2006/02/sample_article_tic_tac_toe_p01.php> and got this really cool approach for development of Tic Tac Toe (am sure u all know it) in VB. However i am using sharp develop 2.2.1 to edit anf compile my code and it's working fine.
I combined all the code and worked out as explained in the article but i get alot of errors when it comes to compiling and i have stated in the code below where the errors come in which mostly say "Statement cannot appear outside of a method body" and "Declaration Expected and so on". Could someone please tell me what is wrong with my code. And i know this code is not sufficient enough but please do visit the site i meantioned and see how the article explained the code. I will be grateful
Happy programming!!!!!!!!
' Created by SharpDevelop.
' User: babasa
' Date: 3/26/2008
' Time: 1:20 PM
'
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Public Partial Class frmTicTacToe
Public Shared arrGrid(3, 3) As Integer
Public Shared intAIDifficulty As Integer
Public Shared intWhosTurn As Integer
Public Shared intWhosWinner As Integer
Private Enum Difficulty
Easy = 0
Medium = 1
Hard = 2
End Enum
Private Sub frmTicTacToe_Load(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Call InitGrid()
Call SetDifficulty(Difficulty.Medium)
intWhosTurn = 1
End Sub ' frmTicTacToe_Load
Private Sub InitGrid()
' Sets all values in the grid array to 0
For i As Integer = 0 To 2
For j As Integer = 0 To 2
arrGrid(i, j) = 0
Next
Next
Call DrawGrid()
End Sub ' InitGrid
Private Sub DrawGrid()
If arrGrid(0, 0) = 1 Then
Me.lblGrid00.Text = "O"
ElseIf arrGrid(0, 0) = 2 Then
Me.lblGrid00.Text = "X"
Else
Me.lblGrid00.Text = ""
End If
End Sub ' DrawGrid
Private Sub SetDifficulty(ByVal intDifficulty As Integer)
intAIDifficulty = intDifficulty
Select Case intDifficulty
Case 0
Me.menuEasy.Checked = True
Me.menuMedium.Checked = False
Me.menuHard.Checked = False
Case 1
Me.menuEasy.Checked = False
Me.menuMedium.Checked = True
Me.menuHard.Checked = False
Case 2
Me.menuEasy.Checked = False
Me.menuMedium.Checked = False
Me.menuHard.Checked = True
End Select
End Sub ' SetDifficulty
Private Sub menuNewGame_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles menuNewGame.Click
Call InitGrid()
intWhosWinner = 0
If intWhosTurn = 2 Then
Call AIMove()
End If
End Sub ' menuNewGame_Click
Private Sub menuEasy_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles menuEasy.Click
Call SetDifficulty(Difficulty.Easy)
End Sub ' menuEasy_Click
Private Sub menuQuit_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles menuQuit.Click
Application.Exit()
End Sub ' menuQuit_Click
Private Sub menuAboutTTT_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles menuAboutTTT.Click
MessageBox.Show("Tic-Tac-Toe v1.0" & vbCrLf & _
"See how this and other programs work at" & _
vbCrLf & "http://www.scratchprojects.com")
End Sub ' menuAboutTTT_Click
Private Sub lblGrid00_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid00.Click
Call GridClick(0, 0)
End Sub ' lblGrid00_Click
Private Sub lblGrid01_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid01.Click
Call GridClick(0, 0)
End Sub ' lblGrid00_Click
Private Sub lblGrid02_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid02.Click
Call GridClick(0, 0)
End Sub ' lblGrid00_Click
Private Sub lblGrid10_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid10.Click
Call GridClick(0, 0)
End Sub ' lblGrid00_Click
Private Sub lblGrid11_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid11.Click
Call GridClick(0, 4)
End Sub ' lblGrid00_Click
Private Sub lblGrid12_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid12.Click
Call GridClick(0, 5)
End Sub ' lblGrid00_Click
Private Sub lblGrid20_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid20.Click
Call GridClick(0, 0)
End Sub ' lblGrid00_Click
Private Sub lblGrid21_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid21.Click
Call GridClick(0, 0)
End Sub ' lblGrid00_Click
Private Sub lblGrid22_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles lblGrid22.Click
Call GridClick(0, 0)
End Sub ' lblGrid00_Click
/* Errors start here */
If (arrGrid(intRow, intCol) = 0) And (intWhosWinner = 0) Then
End If
If intWhosTurn = 1 Then
arrGrid(intRow, intCol) = 1
intWhosTurn = 2
ElseIf intWhosTurn = 2 Then
arrGrid(intRow, intCol) = 2
intWhosTurn = 1
End If
Call DrawGrid()
Call CheckWinLose()
If (intWhosTurn = 2) And (intWhosWinner = 0) Then
Call AIMove()
End If
' Declare the winner
If intWhosWinner = 3 Then
MessageBox.Show("It's a draw")
ElseIf intWhosWinner = 2 Then
MessageBox.Show("You Lose")
ElseIf intWhosWinner = 1 Then
MessageBox.Show("You Win")
End If
Dim intOCount As Integer = 0
Dim intXCount As Integer = 0
Dim intTotalCount As Integer = 0
' Check horizontal for win or lose
For i As Integer = 0 To 2
For j As Integer = 0 To 2
If arrGrid(i, j) = 1 Then
intOCount += 1
intTotalCount += 1
End If
If arrGrid(i, j) = 2 Then
intXCount += 1
intTotalCount += 1
End If
Next
If intOCount = 3 Then
intWhosWinner = 1
ElseIf intXCount = 3 Then
intWhosWinner = 2
End If
intOCount = 0
intXCount = 0
Next
' Check for tie
If (intTotalCount = 9) And (intWhosWinner = 0) Then
intWhosWinner = 3
Else
intTotalCount = 0
End If
' Check diagonal for win or lose
If arrGrid(0, 0) = 1 And arrGrid(1, 1) = 1 And arrGrid(2, 2) = 1 Then
intWhosWinner = 1
ElseIf arrGrid(0, 0) = 2 And arrGrid(1, 1) = 2 And arrGrid(2, 2) = 2 Then
intWhosWinner = 2
ElseIf arrGrid(0, 2) = 1 And arrGrid(1, 1) = 1 And arrGrid(2, 0) = 1 Then
intWhosWinner = 1
ElseIf arrGrid(0, 2) = 2 And arrGrid(1, 1) = 2 And arrGrid(2, 0) = 2 Then
intWhosWinner = 2
End If
Private Sub AIMove()
Dim intRandomX As Integer = RandomNumber(3, 0)
Dim intRandomY As Integer = RandomNumber(3, 0)
Select Case intAIDifficulty
Case Difficulty.Easy
Case Difficulty.Medium
Case Difficulty.Hard
End Select
End Sub ' AIMove
Public Function RandomNumber(ByVal MaxNumber _
As Integer, ByVal MinNumber As Integer)
Dim r As New Random(System.DateTime.Now.Millisecond)
Return r.Next(MinNumber, MaxNumber)
End Function ' RandomNumber
While arrGrid(intRandomX, intRandomY) <> 0
intRandomX = RandomNumber(3, 0)
intRandomY = RandomNumber(3, 0)
End While
Call GridClick(intRandomX, intRandomY)
' Check to see if it can win
For i As Integer = 0 To 2
For j As Integer = 0 To 2
If arrGrid(i, j) = 0 Then
arrGrid(i, j) = 2
Call CheckWinLose()
If intWhosWinner = 2 Then
intWhosWinner = 0
arrGrid(i, j) = 0
GridClick(i, j)
Return
Else
arrGrid(i, j) = 0
End If
End If
Next
Next
' Check to see if it will lose
For i As Integer = 0 To 2
For j As Integer = 0 To 2
If arrGrid(i, j) = 0 Then
arrGrid(i, j) = 1
Call CheckWinLose()
If intWhosWinner = 1 Then
intWhosWinner = 0
arrGrid(i, j) = 0
GridClick(i, j)
Return
Else
intWhosWinner = 0
arrGrid(i, j) = 0
End If
End If
Next
Next
Dim intSquaresUsed As Integer = 0
For i As Integer = 0 To 2
For j As Integer = 0 To 2
If arrGrid(i, j) <> 0 Then
intSquaresUsed += 1
End If
Next
Next
Select Case intSquaresUsed
Case 0
Case 1
Case 2
Case 3
Case 4
Case Is > 4
End Select
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
'
' TODO : Add constructor code after InitializeComponents
'
End Sub
End Class