Click here to Skip to main content
15,896,118 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: How to customize date format in DTPicker created through CreateWindowEx Pin
genie1321-Apr-09 23:33
genie1321-Apr-09 23:33 
GeneralRe: How to customize date format in DTPicker created through CreateWindowEx Pin
Johan Hakkesteegt22-Apr-09 2:59
Johan Hakkesteegt22-Apr-09 2:59 
GeneralRe: How to customize date format in DTPicker created through CreateWindowEx Pin
genie1322-Apr-09 21:00
genie1322-Apr-09 21:00 
Questionchecklistbox record select without loop Pin
Pankaj-codeproject21-Apr-09 20:26
Pankaj-codeproject21-Apr-09 20:26 
AnswerRe: checklistbox record select without loop Pin
Henry Minute22-Apr-09 4:06
Henry Minute22-Apr-09 4:06 
QuestionGenerating a log of call stack Pin
nishkarsh_k21-Apr-09 18:33
nishkarsh_k21-Apr-09 18:33 
AnswerRe: Generating a log of call stack Pin
Thomas Krojer22-Apr-09 20:19
Thomas Krojer22-Apr-09 20:19 
QuestionNeed Help with a TIC Tac Toe game Pin
elindel21-Apr-09 18:15
elindel21-Apr-09 18:15 
I have most of the code done but I don't know how to make it so the computer tries to win. Here is my code, please HELP!!!!!




Private strPlayer As String = "" 'Used to track whose turn it is

'Declare variable representing game board cells
Private strpbxA1 As String = "Open"
Private strpbxA2 As String = "Open"
Private strpbxA3 As String = "Open"
Private strpbxB1 As String = "Open"
Private strpbxB2 As String = "Open"
Private strpbxB3 As String = "Open"
Private strpbxC1 As String = "Open"
Private strpbxC2 As String = "Open"
Private strpbxC3 As String = "Open"

'This procedure executes procedures required to set up the game
Private Sub frmMain_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Set_Game_Defaults() 'Call procedure that sets default assignments

Clear_Board() 'Call procedure that clears the game board


End Sub

'This procedure sets default assignments
Private Sub Set_Game_Defaults()

txt_One.Text = "Click on Play to begin" 'Display opening message
strPlayer = "Player X" 'Set player X to go first


End Sub

'This procedure clears out the game board
Private Sub Clear_Board()

'Load a blank image into each game board cell
pbx_A1.Image = iml_One.Images(2)
pbx_A2.Image = iml_One.Images(2)
pbx_A3.Image = iml_One.Images(2)
pbx_B1.Image = iml_One.Images(2)
pbx_B2.Image = iml_One.Images(2)
pbx_B3.Image = iml_One.Images(2)
pbx_C1.Image = iml_One.Images(2)
pbx_C2.Image = iml_One.Images(2)
pbx_C3.Image = iml_One.Images(2)


'Mark each game board cell as open and available for selection
strpbxA1 = "Open"
strpbxA2 = "Open"
strpbxA3 = "Open"
strpbxB1 = "Open"
strpbxB2 = "Open"
strpbxB3 = "Open"
strpbxC1 = "Open"
strpbxC2 = "Open"
strpbxC3 = "Open"

End Sub

'This procedure executes when the button labeled Play is clicked
Private Sub btn_One_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btn_One.Click

Clear_Board() 'Call the procedure that clears out the game board

Play_Game() 'Call the procedure that begins game play


End Sub

'This procedure begins game play
Private Sub Play_Game()

'Post message that identifies whose turn it is
txt_One.Text = strPlayer & "'s turn."

'Enable all game board cells
pbx_A1.Enabled = True
pbx_A2.Enabled = True
pbx_A3.Enabled = True
pbx_B1.Enabled = True
pbx_B2.Enabled = True
pbx_B3.Enabled = True
pbx_C1.Enabled = True
pbx_C2.Enabled = True
pbx_C3.Enabled = True
btn_One.Enabled = False 'Disable access to the button labeled Play

End Sub

'This procedure executes when the button labeled exit is clicked
Private Sub btn_Three_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btn_Two.Click

Application.Exit()

End Sub


'This proceudre executes when a player clicks on the first cell in the first row
Private Sub pbx_A1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles pbx_A1.Click

Dim strGameOver As String = "" 'Used to track game status

'Notify the player if the cell has already been taken.
If strpbxA1 <> "Open" Then
txt_One.Text = "The square has already been taken." & _
ControlChars.CrLf & strPlayer & "'s turn."
Return ''Leave the Sub procedure
End If

If strPlayer = "Player X" Then
pbx_A1.Image = iml_One.Images(0)
strpbxA1 = "Player X"
Else
pbx_A1.Image = iml_One.Images(1)
strpbxA1 = "Player O"
End If

'Call the procedure that checks to see if the game has been won
strGameOver = Check_For_Winner()

'Call the procedure that switched player turns or displays a message declaring a winner
Determine_Game_Status(strGameOver)

End Sub

'This proceudre executes when a player clicks on the second cell in the first row
Private Sub pbx_A2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles pbx_A2.Click

Dim strGameOver As String = "" 'Used to track game status


'Notify the player if the cell has already been taken.
If strpbxA2 <> "Open" Then
txt_One.Text = "The square has already been taken." & _
ControlChars.CrLf & strPlayer & "'s turn."
Return 'Leave the Sub procedure
End If

If strPlayer = "Player X" Then
pbx_A2.Image = iml_One.Images(0)
strpbxA2 = "Player X"
Else
pbx_A2.Image = iml_One.Images(1)
strpbxA2 = "Player O"
End If

'Call the procedure that checks to see if the game has been won
strGameOver = Check_For_Winner()

'Call the procedure that switched player turns or displays a message declaring a winner
Determine_Game_Status(strGameOver)

End Sub

'This proceudre executes when a player clicks on the third cell in the first row
Private Sub pbx_A3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles pbx_A3.Click

Dim strGameOver As String = "" 'Used to track game status


'Notify the player if the cell has already been taken.
If strpbxA3 <> "Open" Then
txt_One.Text = "The square has already been taken." & _
ControlChars.CrLf & strPlayer & "'s turn."
Return 'Leave the Sub procedure
End If

If strPlayer = "Player X" Then
pbx_A3.Image = iml_One.Images(0)
strpbxA3 = "Player X"
Else
pbx_A3.Image = iml_One.Images(1)
strpbxA3 = "Player O"
End If

'Call the procedure that checks to see if the game has been won
strGameOver = Check_For_Winner()

'Call the procedure that switched player turns or displays a message declaring a winner
Determine_Game_Status(strGameOver)


End Sub

'This proceudre executes when a player clicks on the first cell in the second row
Private Sub pbx_B1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles pbx_B1.Click

Dim strGameOver As String = "" 'Used to track game status


'Notify the player if the cell has already been taken.
If strpbxB1 <> "Open" Then
txt_One.Text = "The square has already been taken." & _
ControlChars.CrLf & strPlayer & "'s turn."
Return 'Leave the Sub procedure
End If

If strPlayer = "Player X" Then
pbx_B1.Image = iml_One.Images(0)
strpbxB1 = "Player X"
Else
pbx_B1.Image = iml_One.Images(1)
strpbxB1 = "Player O"
End If

'Call the procedure that checks to see if the game has been won
strGameOver = Check_For_Winner()

'Call the procedure that switched player turns or displays a message declaring a winner
Determine_Game_Status(strGameOver)

End Sub

'This proceudre executes when a player clicks on the second cell in the second row
Private Sub pbx_B2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles pbx_B2.Click

Dim strGameOver As String = "" 'Used to track game status


'Notify the player if the cell has already been taken.
If strpbxB2 <> "Open" Then
txt_One.Text = "The square has already been taken." & _
ControlChars.CrLf & strPlayer & "'s turn."
Return 'Leave the Sub procedure
End If

If strPlayer = "Player X" Then
pbx_B2.Image = iml_One.Images(0)
strpbxB2 = "Player X"
Else
pbx_B2.Image = iml_One.Images(1)
strpbxB2 = "Player O"
End If

'Call the procedure that checks to see if the game has been won
strGameOver = Check_For_Winner()

'Call the procedure that switched player turns or displays a message declaring a winner
Determine_Game_Status(strGameOver)

End Sub

'This proceudre executes when a player clicks on the third cell in the second row
Private Sub pbx_B3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles pbx_B3.Click

Dim strGameOver As String = "" 'Used to track game status


'Notify the player if the cell has already been taken.
If strpbxB3 <> "Open" Then
txt_One.Text = "The square has already been taken." & _
ControlChars.CrLf & strPlayer & "'s turn."
Return 'Leave the Sub procedure
End If

If strPlayer = "Player X" Then
pbx_B3.Image = iml_One.Images(0)
strpbxB3 = "Player X"
Else
pbx_B3.Image = iml_One.Images(1)
strpbxB3 = "Player O"
End If

'Call the procedure that checks to see if the game has been won
strGameOver = Check_For_Winner()

'Call the procedure that switched player turns or displays a message declaring a winner
Determine_Game_Status(strGameOver)

End Sub

'This proceudre executes when a player clicks on the first cell in the third row
Private Sub pbx_C1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles pbx_C1.Click

Dim strGameOver As String = "" 'Used to track game status


'Notify the player if the cell has already been taken.
If strpbxC1 <> "Open" Then
txt_One.Text = "The square has already been taken." & _
ControlChars.CrLf & strPlayer & "'s turn."
Return 'Leave the Sub procedure
End If

If strPlayer = "Player X" Then
pbx_C1.Image = iml_One.Images(0)
strpbxC1 = "Player X"
Else
pbx_C1.Image = iml_One.Images(1)
strpbxC1 = "Player O"
End If

'Call the procedure that checks to see if the game has been won
strGameOver = Check_For_Winner()

'Call the procedure that switched player turns or displays a message declaring a winner
Determine_Game_Status(strGameOver)

End Sub
'This proceudre executes when a player clicks on the second cell in the third row

Private Sub pbx_C2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles pbx_C2.Click

Dim strGameOver As String = "" 'Used to track game status


'Notify the player if the cell has already been taken.
If strpbxC2 <> "Open" Then
txt_One.Text = "The square has already been taken." & _
vbCrLf & strPlayer & "'s turn."
Return 'Leave the Sub procedure
End If

If strPlayer = "Player X" Then
pbx_C2.Image = iml_One.Images(0)
strpbxC2 = "Player X"
Else
pbx_C2.Image = iml_One.Images(1)
strpbxC2 = "Player O"
End If

'Call the procedure that checks to see if the game has been won
strGameOver = Check_For_Winner()

'Call the procedure that switched player turns or displays a message declaring a winner
Determine_Game_Status(strGameOver)

End Sub

'This proceudre executes when a player clicks on the third cell in the third row
Private Sub pbx_C3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles pbx_C3.Click

Dim strGameOver As String = "" 'Used to track game status


'Notify the player if the cell has already been taken.
If strpbxC3 <> "Open" Then
txt_One.Text = "The square has already been taken." & _
ControlChars.CrLf & strPlayer & "'s turn."
Return 'Leave the Sub procedure
End If

If strPlayer = "Player X" Then
pbx_C3.Image = iml_One.Images(0)
strpbxC3 = "Player X"
Else
pbx_C3.Image = iml_One.Images(1)
strpbxC3 = "Player O"
End If

'Call the procedure that checks to see if the game has been won
strGameOver = Check_For_Winner()

'Call the procedure that switched player turns or displays a message declaring a winner
Determine_Game_Status(strGameOver)


End Sub

'This procedure determines whether the game has been won and by whom
Function Check_For_Winner() As String

'Check the first row for a winner
If strpbxA1 = strPlayer Then
If strpbxA2 = strPlayer Then
If strpbxA3 = strPlayer Then
Return strPlayer
End If
End If
End If

'Check the second row for a winner
If strpbxB1 = strPlayer Then
If strpbxB2 = strPlayer Then
If strpbxB3 = strPlayer Then
Return strPlayer
End If
End If
End If

'Check the third row for a winner
If strpbxC1 = strPlayer Then
If strpbxC2 = strPlayer Then
If strpbxC3 = strPlayer Then
Return strPlayer
End If
End If
End If

'Check the first colum for a winner
If strpbxA1 = strPlayer Then
If strpbxB1 = strPlayer Then
If strpbxC1 = strPlayer Then
Return strPlayer
End If
End If
End If

'Check the second colum for a winner
If strpbxA2 = strPlayer Then
If strpbxB2 = strPlayer Then
If strpbxC2 = strPlayer Then
Return strPlayer
End If
End If
End If

'Check the third colum for a winner
If strpbxA3 = strPlayer Then
If strpbxB3 = strPlayer Then
If strpbxC3 = strPlayer Then
Return strPlayer
End If
End If
End If

'Check diagonally from top=left to bottom_right for a winner
If strpbxA1 = strPlayer Then
If strpbxB2 = strPlayer Then
If strpbxC3 = strPlayer Then
Return strPlayer
End If
End If
End If


'Check diagonally from top-right to bottom-left for a winner
If strpbxA3 = strPlayer Then
If strpbxB2 = strPlayer Then
If strpbxC1 = strPlayer Then
Return strPlayer
End If
End If
End If



'Check to see if the game has resulted in a tie

If strpbxA1 = "Player X" Or _
strpbxA1 = "Player O" Then
If strpbxA2 = "Player X" Or _
strpbxA2 = "Player O" Then
If strpbxA3 = "Player X" Or _
strpbxA3 = "Player O" Then
If strpbxB1 = "Player X" Or _
strpbxB1 = "Player O" Then
If strpbxB2 = "Player X" Or _
strpbxB2 = "Player O" Then
If strpbxB3 = "Player X" Or _
strpbxB3 = "Player O" Then
If strpbxC1 = "Player X" Or _
strpbxC1 = "Player O" Then
If strpbxC2 = "Player X" Or _
strpbxC2 = "Player O" Then
If strpbxC3 = "Player X" Or _
strpbxC3 = "Player O" Then
Return "Tie"
End If
End If
End If
End If
End If
End If
End If
End If
End If



Return ""

End Function

'This procedure determines whether or not the game is over
Private Sub Determine_Game_Status(ByVal strGameOver As String)

If strGameOver = "" Then 'The game is not over yet
Switch_Players() 'Call procedure that switches player turns
'Post message stating that it is time for players to switch turns
txt_One.Text = strPlayer & "'s turn."
Else
If strGameOver <> "Tie" Then 'There is a winner
btn_One.Enabled = True ''Enable the button labeled Play
'Call procedure that disables game board cells
Disable_Squares()
'Display game over message
txt_One.Text = "Game over. " & strGameOver & " has won."
Else 'The game has resulted in a tie
btn_One.Enabled = True 'Enable the button Play
'Call procedure that disables game board cells
Disable_Squares()
'Display game over message
txt_One.Text = "Game over. There was no winner."
End If
End If

End Sub

'This procedure is responsible for toggling between players turns
Private Sub Switch_Players()

If strPlayer = "Player X" Then
strPlayer = "Player O"
Else
strPlayer = "Player X"
End If

End Sub

'This procedure disables all game board cells
Private Sub Disable_Squares()

pbx_A1.Enabled = False
pbx_A2.Enabled = False
pbx_A3.Enabled = False
pbx_B1.Enabled = False
pbx_B2.Enabled = False
pbx_B3.Enabled = False
pbx_C1.Enabled = False
pbx_C2.Enabled = False
pbx_C3.Enabled = False

End Sub


End Class
AnswerRe: Need Help with a TIC Tac Toe game Pin
Christian Graus21-Apr-09 18:38
protectorChristian Graus21-Apr-09 18:38 
GeneralRe: Need Help with a TIC Tac Toe game Pin
Dalek Dave21-Apr-09 21:33
professionalDalek Dave21-Apr-09 21:33 
AnswerRe: Need Help with a TIC Tac Toe game Pin
Eddy Vluggen21-Apr-09 22:14
professionalEddy Vluggen21-Apr-09 22:14 
Questionhow do i use my window base application remotly? Pin
r_mohd21-Apr-09 15:46
r_mohd21-Apr-09 15:46 
AnswerRe: how do i use my window base application remotly? Pin
Christian Graus21-Apr-09 15:56
protectorChristian Graus21-Apr-09 15:56 
AnswerRe: how do i use my window base application remotly? Pin
Yusuf21-Apr-09 16:03
Yusuf21-Apr-09 16:03 
QuestionNeed Help with Visual Basic Drag and Drop Pin
ymilan21-Apr-09 13:15
ymilan21-Apr-09 13:15 
AnswerRe: Need Help with Visual Basic Drag and Drop Pin
Eddy Vluggen21-Apr-09 13:36
professionalEddy Vluggen21-Apr-09 13:36 
GeneralRe: Need Help with Visual Basic Drag and Drop Pin
ymilan22-Apr-09 17:38
ymilan22-Apr-09 17:38 
GeneralRe: Need Help with Visual Basic Drag and Drop Pin
Eddy Vluggen22-Apr-09 20:53
professionalEddy Vluggen22-Apr-09 20:53 
GeneralRe: Need Help with Visual Basic Drag and Drop Pin
ymilan23-Apr-09 10:45
ymilan23-Apr-09 10:45 
GeneralRe: Need Help with Visual Basic Drag and Drop Pin
Eddy Vluggen23-Apr-09 20:49
professionalEddy Vluggen23-Apr-09 20:49 
GeneralRe: Need Help with Visual Basic Drag and Drop Pin
ymilan24-Apr-09 5:41
ymilan24-Apr-09 5:41 
GeneralRe: Need Help with Visual Basic Drag and Drop Pin
ymilan24-Apr-09 6:06
ymilan24-Apr-09 6:06 
GeneralRe: Need Help with Visual Basic Drag and Drop Pin
Eddy Vluggen24-Apr-09 8:45
professionalEddy Vluggen24-Apr-09 8:45 
Questionhow to:create a SIMPLE windows explorer like app. Pin
trujade21-Apr-09 6:54
trujade21-Apr-09 6:54 
AnswerRe: how to:create a SIMPLE windows explorer like app. Pin
Luc Pattyn21-Apr-09 7:08
sitebuilderLuc Pattyn21-Apr-09 7:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.