Visual Basic - Noughts & Crosses

Gigabyte / H55m-s2v
January 25, 2012 at 06:12:23
Specs: Windows 7 Home Premium, 3.192 GHz / 3959 MB
Hi I have a problem, here is a visual basic x vs o game i wrote but it has a few bugs e.i.
1. It wates until all spaces are filled before deciding who won
2. It Does'nt choose certain spaces for ????? reason
3. It has a few more bugs....(test it yourself)

Please help:
Public Class Form1
Private Sub Reset()
t1.Text = ""
t2.Text = ""
t3.Text = ""
t4.Text = ""
t5.Text = ""
t6.Text = ""
t7.Text = ""
t8.Text = ""
t9.Text = ""
t1.Enabled = True
t2.Enabled = True
t3.Enabled = True
t4.Enabled = True
t5.Enabled = True
t6.Enabled = True
t7.Enabled = True
t8.Enabled = True
t9.Enabled = True
Label1.Text = ""
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Reset()
End Sub
Private Sub t1_Click(sender As System.Object, e As System.EventArgs) Handles t1.Click
If t1.Text <> "" Then
Label1.Text = "Space already takin"
Exit Sub
End If
If t1.Text = "" Then
t1.Text = "X"
t1.Enabled = False
End If
StartIFPCnoMoves()
End Sub
Private Sub t2_Click(sender As System.Object, e As System.EventArgs) Handles t2.Click
If t2.Text <> "" Then
Label1.Text = "Space already takin"
Exit Sub
End If
If t2.Text = "" Then
t2.Text = "X"
t2.Enabled = False
End If
StartIFPCnoMoves()
End Sub
Private Sub t3_Click(sender As System.Object, e As System.EventArgs) Handles t3.Click
If t3.Text <> "" Then
Label1.Text = "Space already takin"
Exit Sub
End If
If t3.Text = "" Then
t3.Text = "X"
t3.Enabled = False
End If
StartIFPCnoMoves()
End Sub
Private Sub t4_Click(sender As System.Object, e As System.EventArgs) Handles t4.Click
If t4.Text <> "" Then
Label1.Text = "Space already takin"
Exit Sub
End If
If t4.Text = "" Then
t4.Text = "X"
t4.Enabled = False
End If
StartIFPCnoMoves()
End Sub
Private Sub t5_Click(sender As System.Object, e As System.EventArgs) Handles t5.Click
If t5.Text <> "" Then
Label1.Text = "Space already takin"
Exit Sub
End If
If t5.Text = "" Then
t5.Text = "X"
t5.Enabled = False
End If
StartIFPCnoMoves()
End Sub
Private Sub t6_Click(sender As System.Object, e As System.EventArgs) Handles t6.Click
If t6.Text <> "" Then
Label1.Text = "Space already takin"
Exit Sub
End If
If t6.Text = "" Then
t6.Text = "X"
t6.Enabled = False
End If
StartIFPCnoMoves()
End Sub
Private Sub t7_Click(sender As System.Object, e As System.EventArgs) Handles t7.Click
If t7.Text <> "" Then
Label1.Text = "Space already takin"
Exit Sub
End If
If t7.Text = "" Then
t7.Text = "X"
t7.Enabled = False
End If
StartIFPCnoMoves()
End Sub
Private Sub t8_Click(sender As System.Object, e As System.EventArgs) Handles t8.Click
If t8.Text <> "" Then
Label1.Text = "Space already takin"
Exit Sub
End If
If t8.Text = "" Then
t8.Text = "X"
t8.Enabled = False
End If
StartIFPCnoMoves()
End Sub
Private Sub t9_Click(sender As System.Object, e As System.EventArgs) Handles t9.Click
If t9.Text <> "" Then
Label1.Text = "Space already takin"
Exit Sub
End If
If t9.Text = "" Then
t9.Text = "X"
t9.Enabled = False
End If
StartIFPCnoMoves()
End Sub
Private Sub StartPC()
If t1.Text = "" Then
t1.Text = "O"
Exit Sub
End If
If t2.Text = "" Then
t2.Text = "O"
Exit Sub
End If
If t3.Text = "" Then
t3.Text = "O"
Exit Sub
End If
If t4.Text = "" Then
t4.Text = "O"
Exit Sub
End If
If t5.Text = "" Then
t5.Text = "O"
Exit Sub
End If
If t6.Text = "" Then
t6.Text = "O"
Exit Sub
End If
If t7.Text = "" Then
t7.Text = "O"
Exit Sub
End If
If t8.Text = "" Then
t8.Text = "O"
Exit Sub
End If
If t9.Text = "" Then
t9.Text = "O"
Exit Sub
End If

End Sub
Private Sub ComputerLogic()
DetectDraw()
DetectWinX()
DetectWinO()
If (t1.Text = "O") And (t3.Text = "O") And (t2.Text = "") Then
t2.Text = "O"
Exit Sub
End If
If (t1.Text = "O") And (t9.Text = "O") And (t5.Text = "") Then
t5.Text = "O"
Exit Sub
End If
If (t1.Text = "O") And (t7.Text = "O") And (t4.Text = "") Then
t4.Text = "O"
Exit Sub
End If
If (t3.Text = "O") And (t7.Text = "O") And (t5.Text = "") Then
t5.Text = "O"
Exit Sub
End If
If (t3.Text = "O") And (t9.Text = "O") And (t6.Text = "") Then
t6.Text = "O"
Exit Sub
End If
If (t9.Text = "O") And (t7.Text = "O") And (t8.Text = "") Then
t8.Text = "O"
Exit Sub
End If
If (t2.Text = "O") And (t8.Text = "O") And (t5.Text = "") Then
t5.Text = "O"
Exit Sub
End If
If (t4.Text = "O") And (t6.Text = "O") And (t4.Text = "") Then
t5.Text = "O"
Exit Sub
End If
If (t1.Text = "O") And (t2.Text = "O") And (t3.Text = "") Then
t3.Text = "O"
Exit Sub
End If
If (t1.Text = "O") And (t5.Text = "O") And (t9.Text = "") Then
t9.Text = "O"
Exit Sub
End If
If (t1.Text = "O") And (t4.Text = "O") And (t7.Text = "") Then
t7.Text = "O"
Exit Sub
End If
If (t2.Text = "O") And (t5.Text = "O") And (t9.Text = "") Then
t9.Text = "O"
Exit Sub
End If
If (t3.Text = "O") And (t2.Text = "O") And (t1.Text = "") Then
t1.Text = "O"
Exit Sub
End If
If (t3.Text = "O") And (t5.Text = "O") And (t7.Text = "") Then
t7.Text = "O"
Exit Sub
End If
If (t3.Text = "O") And (t6.Text = "O") And (t3.Text = "") Then
t9.Text = "O"
Exit Sub
End If
If (t4.Text = "O") And (t5.Text = "O") And (t6.Text = "") Then
t6.Text = "O"
Exit Sub
End If
If (t6.Text = "O") And (t5.Text = "O") And (t4.Text = "") Then
t4.Text = "O"
Exit Sub
End If
If (t7.Text = "O") And (t4.Text = "O") And (t1.Text = "") Then
t1.Text = "O"
Exit Sub
End If
If (t7.Text = "O") And (t5.Text = "O") And (t3.Text = "") Then
t3.Text = "O"
Exit Sub
End If
If (t7.Text = "O") And (t8.Text = "O") And (t9.Text = "") Then
t9.Text = "O"
Exit Sub
End If
If (t8.Text = "O") And (t5.Text = "O") And (t2.Text = "") Then
t2.Text = "O"
Exit Sub
End If
If (t9.Text = "O") And (t8.Text = "O") And (t7.Text = "") Then
t7.Text = "O"
Exit Sub
End If
If (t9.Text = "O") And (t5.Text = "O") And (t1.Text = "") Then
t1.Text = "O"
Exit Sub
End If
If (t9.Text = "O") And (t6.Text = "O") And (t3.Text = "") Then
t3.Text = "O"
Exit Sub
End If
If (t1.Text = "X") And (t2.Text = "X") And (t3.Text = "") Then
t3.Text = "O"
Exit Sub
End If
If (t1.Text = "X") And (t5.Text = "X") And (t9.Text = "") Then
t9.Text = "O"
Exit Sub
End If
If (t1.Text = "X") And (t4.Text = "X") And (t7.Text = "") Then
t7.Text = "O"
Exit Sub
End If
If (t2.Text = "X") And (t5.Text = "X") And (t8.Text = "") Then
t8.Text = "O"
Exit Sub
End If
If (t3.Text = "X") And (t2.Text = "X") And (t1.Text = "") Then
t1.Text = "O"
Exit Sub
End If
If (t3.Text = "X") And (t5.Text = "X") And (t7.Text = "") Then
t7.Text = "O"
Exit Sub
End If
If (t3.Text = "X") And (t6.Text = "X") And (t9.Text = "") Then
t9.Text = "O"
Exit Sub
End If
If (t4.Text = "X") And (t5.Text = "X") And (t6.Text = "") Then
t6.Text = "O"
Exit Sub
End If
If (t6.Text = "X") And (t5.Text = "X") And (t4.Text = "") Then
t4.Text = "O"
Exit Sub
End If
If (t7.Text = "X") And (t4.Text = "X") And (t1.Text = "") Then
t1.Text = "O"
Exit Sub
End If
If (t7.Text = "X") And (t5.Text = "X") And (t3.Text = "") Then
t3.Text = "O"
Exit Sub
End If
If (t7.Text = "X") And (t8.Text = "X") And (t9.Text = "") Then
t9.Text = "O"
Exit Sub
End If
If (t8.Text = "X") And (t5.Text = "X") And (t2.Text = "") Then
t2.Text = "O"
Exit Sub
End If
If (t9.Text = "X") And (t8.Text = "X") And (t7.Text = "") Then
t7.Text = "O"
Exit Sub
End If
If (t9.Text = "X") And (t5.Text = "X") And (t1.Text = "") Then
t1.Text = "O"
Exit Sub
End If
If (t9.Text = "X") And (t6.Text = "X") And (t3.Text = "") Then
t3.Text = "O"
Exit Sub
End If
If (t1.Text = "X") And (t3.Text = "X") And (t2.Text = "") Then
t2.Text = "O"
Exit Sub
End If
If (t1.Text = "X") And (t9.Text = "X") And (t5.Text = "") Then
t5.Text = "O"
Exit Sub
End If
If (t1.Text = "X") And (t7.Text = "X") And (t4.Text = "") Then
t4.Text = "O"
Exit Sub
End If
If (t3.Text = "X") And (t7.Text = "X") And (t5.Text = "") Then
t5.Text = "O"
Exit Sub
End If
If (t3.Text = "X") And (t9.Text = "X") And (t6.Text = "") Then
t6.Text = "O"
Exit Sub
End If
If (t9.Text = "X") And (t7.Text = "X") And (t8.Text = "") Then
t8.Text = "O"
Exit Sub
End If
If (t2.Text = "X") And (t4.Text = "X") And (t1.Text = "") Then
t1.Text = "O"
Exit Sub
End If
If (t2.Text = "X") And (t6.Text = "X") And (t3.Text = "") Then
t3.Text = "O"
Exit Sub
End If
If (t8.Text = "X") And (t4.Text = "X") And (t7.Text = "") Then
t7.Text = "O"
Exit Sub
End If
If (t8.Text = "X") And (t6.Text = "X") And (t9.Text = "") Then
t9.Text = "O"
Exit Sub
End If
If (t1.Text = "X") And (t6.Text = "X") And (t8.Text = "") Then
t8.Text = "O"
Exit Sub
End If
If (t1.Text = "X") And (t8.Text = "X") And (t6.Text = "") Then
t6.Text = "O"
Exit Sub
End If
If (t3.Text = "X") And (t8.Text = "X") And (t4.Text = "") Then
t4.Text = "O"
Exit Sub
End If
If (t3.Text = "X") And (t4.Text = "X") And (t8.Text = "") Then
t8.Text = "O"
Exit Sub
End If
If (t9.Text = "X") And (t4.Text = "X") And (t2.Text = "") Then
t2.Text = "O"
Exit Sub
End If
If (t9.Text = "X") And (t2.Text = "X") And (t4.Text = "") Then
t4.Text = "O"
Exit Sub
End If
If (t7.Text = "X") And (t2.Text = "X") And (t6.Text = "") Then
t6.Text = "O"
Exit Sub
End If
If (t7.Text = "X") And (t6.Text = "X") And (t2.Text = "") Then
t2.Text = "O"
Exit Sub
End If
End Sub
Private Sub DetectWinX()
'1,2,3 - 4,5,6 - 7,8,9 - 1,4,7 - 2,5,8 - 3,6,9 - 1,5,9 - 3,5,7
If (t1.Text = "X") And (t2.Text = "X") And (t3.Text = "X") Then
Label1.Text = "Congradulations, You win!!"
Exit Sub
End If
If (t4.Text = "X") And (t5.Text = "X") And (t6.Text = "X") Then
Label1.Text = "Congradulations, You win!!"
Exit Sub
End If
If (t7.Text = "X") And (t8.Text = "X") And (t9.Text = "X") Then
Label1.Text = "Congradulations, You win!!"
Exit Sub
End If
If (t1.Text = "X") And (t4.Text = "X") And (t7.Text = "X") Then
Label1.Text = "Congradulations, You win!!"
Exit Sub
End If
If (t2.Text = "X") And (t5.Text = "X") And (t8.Text = "X") Then
Label1.Text = "Congradulations, You win!!"
Exit Sub
End If
If (t3.Text = "X") And (t6.Text = "X") And (t9.Text = "X") Then
Label1.Text = "Congradulations, You win!!"
Exit Sub
End If
If (t1.Text = "X") And (t5.Text = "X") And (t9.Text = "X") Then
Label1.Text = "Congradulations, You win!!"
Exit Sub
End If
If (t3.Text = "X") And (t5.Text = "X") And (t7.Text = "X") Then
Label1.Text = "Congradulations, You win!!"
Exit Sub
End If
End Sub
Private Sub DetectWinO()
'1,2,3 - 4,5,6 - 7,8,9 - 1,4,7 - 2,5,8 - 3,6,9 - 1,5,9 - 3,5,7
If (t1.Text = "O") And (t2.Text = "O") And (t3.Text = "O") Then
Label1.Text = "Computer Wins!"
Exit Sub
End If
If (t4.Text = "O") And (t5.Text = "O") And (t6.Text = "O") Then
Label1.Text = "Computer Wins!"
Exit Sub
End If
If (t7.Text = "O") And (t8.Text = "O") And (t9.Text = "O") Then
Label1.Text = "Computer Wins!"
Exit Sub
End If
If (t1.Text = "O") And (t4.Text = "O") And (t7.Text = "O") Then
Label1.Text = "Computer Wins!"
Exit Sub
End If
If (t2.Text = "O") And (t5.Text = "O") And (t8.Text = "O") Then
Label1.Text = "Computer Wins!"
Exit Sub
End If
If (t3.Text = "O") And (t6.Text = "O") And (t9.Text = "O") Then
Label1.Text = "Computer Wins!"
Exit Sub
End If
If (t1.Text = "O") And (t5.Text = "O") And (t9.Text = "O") Then
Label1.Text = "Computer Wins!"
Exit Sub
End If
If (t3.Text = "O") And (t5.Text = "O") And (t7.Text = "O") Then
Label1.Text = "Computer Wins!"
Exit Sub
End If
End Sub
Private Sub DetectDraw()
Dim Tilecount As Integer = 0
If (t1.Text <> "O") And (t1.Text <> "") Then
Tilecount = Tilecount + 1
End If
If (t2.Text <> "O") And (t2.Text <> "") Then
Tilecount = Tilecount + 1
End If
If (t3.Text <> "O") And (t3.Text <> "") Then
Tilecount = Tilecount + 1
End If
If (t4.Text <> "O") And (t4.Text <> "") Then
Tilecount = Tilecount + 1
End If
If (t5.Text <> "O") And (t5.Text <> "") Then
Tilecount = Tilecount + 1
End If
If (t6.Text <> "O") And (t6.Text <> "") Then
Tilecount = Tilecount + 1
End If
If (t7.Text <> "O") And (t7.Text <> "") Then
Tilecount = Tilecount + 1
End If
If (t8.Text <> "O") And (t8.Text <> "") Then
Tilecount = Tilecount + 1
End If
If (t9.Text <> "O") And (t9.Text <> "") Then
Tilecount = Tilecount + 1
End If
If Tilecount = 9 Then
Label1.Text = "It's a draw!"
End If
End Sub
Private Sub StartIFPCnoMoves()
Dim NoMove As Integer = 0
If t1.Text <> "O" Then
NoMove = NoMove = NoMove + 1
End If
If t2.Text <> "O" Then
NoMove = NoMove = NoMove + 1
End If
If t3.Text <> "O" Then
NoMove = NoMove = NoMove + 1
End If
If t4.Text <> "O" Then
NoMove = NoMove = NoMove + 1
End If
If t5.Text <> "O" Then
NoMove = NoMove = NoMove + 1
End If
If t6.Text <> "O" Then
NoMove = NoMove = NoMove + 1
End If
If t7.Text <> "O" Then
NoMove = NoMove = NoMove + 1
End If
If t8.Text <> "O" Then
NoMove = NoMove = NoMove + 1
End If
If t9.Text <> "O" Then
NoMove = NoMove = NoMove + 1
End If
If NoMove = 9 Then
'there are 9 spaces without O's'
StartPC()
Exit Sub
ElseIf NoMove <> 9 Then
ComputerLogic()
Exit Sub
End If
End Sub
End Class


See More: Visual Basic - Noughts & Crosses

Report •

#1
January 29, 2012 at 13:21:43
Posting tip: When you paste code, it helps if what you paste compiles. In this instance, we're left guessing what t1, t2, t3, t4, t5, t6, t7, t8, t9, Label1, and Button1 are and what they do. It might not be difficult, but those 5 minutes I'm spending on the setup aren't spent on your problem.

General Observation: This program appears to be the result of coding without design. Before you open your editor of choice (Visual Studio), you should have outlined all of the steps involved with the appropriate level of logic breakdown.

On Copy/Paste: When coding, copy/paste is generally the wrong answer. You should at a minimum combine the t?_Click subs into a single sub:

Private Sub PlayerMove(ByVal sender As System.Object, ByVal e As System.EventArgs)
  If sender.Text <> "" Then
    Label1.Text = "Space already takin"
    Exit Sub
  End If
  'NOTE: Removed a redundant If statement
  sender.Text = "X"
  sender.Enabled = False
  StartIFPCnoMoves()
End Sub
Then, in the New . . .
Public Sub New()
  ' This call is required by the designer.
  InitializeComponent()

  ' Add any initialization after the InitializeComponent() call.
  AddHandler t1.Click, AddressOf PlayerMove
  AddHandler t2.Click, AddressOf PlayerMove
  AddHandler t3.Click, AddressOf PlayerMove
  AddHandler t4.Click, AddressOf PlayerMove
  AddHandler t5.Click, AddressOf PlayerMove
  AddHandler t6.Click, AddressOf PlayerMove
  AddHandler t7.Click, AddressOf PlayerMove
  AddHandler t8.Click, AddressOf PlayerMove
  AddHandler t9.Click, AddressOf PlayerMove
  Reset()
End Sub


Bugs at a glance by function:
StartPC: Technically not a bug, but the contents of this Sub should be at the end of ComputerLogic, if just to keep all of the AI in one place.

ComputerLogic: End of game checks should be done both before and after this function, not in it. As is, the computer doesn't check to see if it won until after the player's next turn. The AI should disable the button it picks. This would allow you to remove the If checks in PlayerMove / t?_Click

DetectDraw: Function tests to see if each button is not "O" and not blank. The function is checking to see all buttons are "X", which is impossible. (Five's the max.)

StartIFPCnoMoves: Entirely irrelevant if you combine both AI functions. And this line is broken:

NoMove = NoMove = NoMove + 1
(NoMove) = (NoMove + 1) evaluates to False. False is implicitly converted to 0, so NoMove will always be 0.

DetectWinX / DetectWinO / DetectDraw: The functions check for an end game, but don't interrupt a completed game. The only thing stopping the AI and player is a lack of blank spots after 9 moves.

Reset: I suggest the following header for this Sub. Congratulations, you just got rid of a one line wrapper function and you can still call Reset():

Private Sub Reset(Optional ByVal sender As System.Object = Nothing, _
  Optional ByVal e As System.EventArgs = Nothing) Handles Button1.Click

How To Ask Questions The Smart Way


Report •

#2
January 30, 2012 at 04:49:54
Thanks for your Reply, I will start implementing your ideas and proposals.

Report •
Related Solutions


Ask Question