Welcome to Dream.In.Code
Become a VB Expert!

Join 137,229 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,886 people online right now. Registration is fast and FREE... Join Now!




Tic Tac Toe 1 Player

 
Reply to this topicStart new topic

Tic Tac Toe 1 Player, I need help with starting a new game

orangeslide8
21 Feb, 2007 - 06:42 AM
Post #1

D.I.C Head
Group Icon

Joined: 29 Dec, 2006
Posts: 190



Thanked: 1 times
Dream Kudos: 25
My Contributions
ok i finished writing a tic tac toe code for 1 player that is unbeatable but after the games over and it starts again the ai goes then when i click a button it appears but the ai doesn't go i set the turn back to 0 but it still doesn't work heres my code
CODE

Public Class form1
    Dim turn = 0
    Dim firstspot As Integer
    Dim secondspot As Integer
    Dim winner As String
    Dim whichsituation As Double
    Dim whichsituation2 As Integer
    Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
        Close()
    End Sub
    Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = False
        Button7.Enabled = False
        Button8.Enabled = False
        Button9.Enabled = False
        Button10.Visible = True
    End Sub
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        Button6.Enabled = True
        Button7.Enabled = True
        Button8.Enabled = True
        Button9.Enabled = True
        Button1.Text = "X"
        Button1.Enabled = False
        MsgBox("Its your turn")
        turn = turn + 1
        Button10.Visible = False
    End Sub

    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = False
        Button7.Enabled = False
        Button8.Enabled = False
        Button9.Enabled = False
        Label6.Text = 0
        Label7.Text = 0
        Label8.Text = 0
        Label9.Text = 0
        Label10.Text = 0
        Label11.Text = 0
    End Sub
    Private Sub ai1stturn()
        If Button2.Text = "O" Then
            Button7.Text = "X"
            Button7.Enabled = False
            firstspot = 2
        End If
        If Button3.Text = "O" Then
            Button5.Text = "X"
            Button5.Enabled = False
            firstspot = 3
        End If
        If Button4.Text = "O" Then
            Button9.Text = "X"
            Button9.Enabled = False
            firstspot = 4
        End If
        If Button5.Text = "O" Then
            Button9.Text = "X"
            Button9.Enabled = False
            firstspot = 5
        End If
        If Button6.Text = "O" Then
            Button5.Text = "X"
            Button5.Enabled = False
            firstspot = 6
        End If
        If Button7.Text = "O" Then
            Button9.Text = "X"
            Button9.Enabled = False
            firstspot = 7
        End If
        If Button8.Text = "O" Then
            Button5.Text = "X"
            Button5.Enabled = False
            firstspot = 8
        End If
        If Button9.Text = "O" Then
            Button7.Text = "X"
            Button7.Enabled = False
            firstspot = 9
        End If
        MsgBox("it's your turn")
        turn = turn + 1
    End Sub
    Private Sub NewGame()
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = False
        Button7.Enabled = False
        Button8.Enabled = False
        Button9.Enabled = False
        Button10.Visible = True
        If winner = "AI" Then
            Label9.Text = Label9.Text + 1
            Label8.Text = Label8.Text + 1
        End If
        If winner = "Tie" Then
            Label7.Text = Label7.Text + 1
            Label10.Text = Label10.Text + 1
        End If
        turn = 0
    End Sub
    Private Sub ai2()
        If firstspot = 3 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 4 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 5 Then
            Button8.Text = "X"
            Button8.Enabled = False
            whichsituation = 1
        End If
        If firstspot = 6 Then
            Button9.Text = "X"
            winner = "AI"
            MsgBox("The AI Won")
            Button9.Enabled = False
            NewGame()
        End If
        If firstspot = 7 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 8 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 9 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        turn = turn + 1
    End Sub
    Private Sub ai3()
        If firstspot = 2 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 4 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 5 Then
            Button7.Text = "X"
            Button7.Enabled = False
            whichsituation = 3
        End If
        If firstspot = 6 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 7 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 8 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 9 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        turn = turn + 1
    End Sub
    Private Sub ai4()
        If firstspot = 2 Then
            Button5.Text = "X"
            Button5.Enabled = False
            whichsituation = 4
        End If
        If firstspot = 3 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 5 Then
            Button6.Text = "X"
            Button6.Enabled = False
            whichsituation = 5
        End If
        If firstspot = 6 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 7 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 8 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 9 Then
            Button3.Text = "X"
            Button3.Enabled = False
            whichsituation = 6
        End If
        turn = turn + 1
    End Sub
    Private Sub ai5()
        If firstspot = 2 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 4 Then
            Button6.Text = "X"
            Button6.Enabled = False
            whichsituation = 7
        End If
        If firstspot = 7 Then
            Button3.Text = "X"
            Button3.Enabled = False
            whichsituation = 8
        End If
        If firstspot = 9 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        turn = turn + 1
    End Sub
    Private Sub ai6()
        If firstspot = 2 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 3 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 4 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 5 Then
            Button4.Text = "X"
            Button4.Enabled = False
            whichsituation = 8.5
        End If
        If firstspot = 7 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 8 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 9 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        turn = turn + 1
    End Sub
    Private Sub ai7()
        If firstspot = 3 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 4 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 5 Then
            Button3.Text = "X"
            Button3.Enabled = False
            whichsituation = 9
        End If
        If firstspot = 6 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 8 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        turn = turn + 1
    End Sub
    Private Sub ai8()
        If firstspot = 2 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 3 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 4 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 5 Then
            Button2.Text = "X"
            Button2.Enabled = False
            whichsituation = 10
        End If
        If firstspot = 6 Then
            Button9.Text = "X"
            Button9.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 7 Then
            Button5.Text = "X"
            Button5.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 9 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        turn = turn + 1
    End Sub
    Private Sub ai9()
        If firstspot = 2 Then
            Button4.Text = "X"
            Button4.Enabled = False
            winner = "AI"
            MsgBox("The AI Won")
            NewGame()
        End If
        If firstspot = 3 Then
            Button6.Text = "X"
            Button6.Enabled = False
            whichsituation = 11
        End If
        If firstspot = 6 Then
            Button3.Text = "X"
            Button3.Enabled = False
            whichsituation = 12
        End If
        If firstspot = 8 Then
            Button7.Text = "X"
            Button7.Enabled = False
            whichsituation = 13
        End If
        turn = turn + 1
    End Sub
    Private Sub situation23()
        Button8.Text = "X"
        Button8.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation25()
        Button3.Text = "X"
        Button3.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation26()
        Button5.Text = "X"
        Button5.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation27()
        Button3.Text = "X"
        Button3.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation28()
        Button6.Text = "X"
        Button6.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation28andhalf()
        Button7.Text = "X"
        Button7.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation29()
        Button6.Text = "X"
        Button6.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation211()
        Button4.Text = "X"
        Button4.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation212()
        Button9.Text = "X"
        Button9.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation213()
        Button3.Text = "X"
        Button3.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button2.Text = "O"
        Button2.Enabled = False
        If turn = 1 Then
            ai1stturn()
        End If
        If turn = 3 Then
            ai2()
            secondspot = 2
        End If
        If turn = 5 Then
            If whichsituation = 3 Then
                situation23()
            End If
            If whichsituation = 5 Then
                situation25()
            End If
            If whichsituation = 6 Then
                situation26()
            End If
            If whichsituation = 7 Then
                situation27()
            End If
            If whichsituation = 8.5 Then
                situation28andhalf()
            End If
            If whichsituation = 8 Then
                situation28()
            End If
            If whichsituation = 9 Then
                situation29()
            End If
            If whichsituation = 11 Then
                situation211()
            End If
            If whichsituation = 12 Then
                situation212()
            End If
            If whichsituation = 13 Then
                situation213()
            End If
            turn = turn + 1
        End If
        If turn = 7 Then
            If whichsituation2 = 2 Then
                Button8.Text = "X"
                Button8.Enabled = False
                winner = "AI"
                MsgBox("The AI Won")
                NewGame()
            End If
            If whichsituation2 = 3 Then
                Button8.Text = "X"
                Button8.Enabled = False
                winner = "AI"
                MsgBox("The AI Won")
                NewGame()
            End If
            If whichsituation2 = 4 Then
                Button8.Text = "X"
                Button8.Enabled = False
                MsgBox("It was a tie")
                winner = "Tie"
                NewGame()
            End If
            If whichsituation2 = 6 Then
                Button8.Text = "X"
                Button8.Enabled = False
                winner = ("Tie")
                MsgBox("It was a tie")
                NewGame()
            End If
        End If
        turn = turn + 1
    End Sub
    Private Sub situation31()
        Button7.Text = "X"
        Button7.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation34()
        Button9.Text = "X"
        Button9.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation35()
        Button7.Text = "X"
        Button7.Enabled = False
        MsgBox("Its Your Turn")
        whichsituation2 = 2
    End Sub
    Private Sub situation37()
        Button7.Text = "X"
        Button7.Enabled = False
        MsgBox("Its Your Turn")
        whichsituation2 = 3
    End Sub
    Private Sub situation38andhalf()
        Button7.Text = "X"
        Button7.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation310()
        Button7.Text = "X"
        Button7.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation313()
        Button4.Text = "X"
        Button4.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button3.Text = "O"
        Button3.Enabled = False
        If turn = 1 Then
            ai1stturn()
        End If
        If turn = 3 Then
            ai3()
            secondspot = 3
        End If
        If turn = 5 Then
            If whichsituation = 1 Then
                situation31()
            End If
            If whichsituation = 4 Then
                situation34()
            End If
            If whichsituation = 5 Then
                situation35()
            End If
            If whichsituation = 7 Then
                situation37()
            End If
            If whichsituation = 8.5 Then
                situation38andhalf()
            End If
            If whichsituation = 10 Then
                situation310()
            End If
            If whichsituation = 13 Then
                situation313()
            End If
            turn = turn + 1
        End If
        turn = turn + 1
    End Sub
    Private Sub situation41()
        Button7.Text = "X"
        Button7.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation43()
        Button8.Text = "X"
        Button8.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation48()
        Button2.Text = "X"
        Button2.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation49()
        Button2.Text = "X"
        Button2.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation410()
        Button3.Text = "X"
        Button3.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation411()
        Button2.Text = "X"
        Button2.Enabled = False
        MsgBox("Its Your Turn")
        whichsituation2 = 4
    End Sub
    Private Sub situation412()
        Button2.Text = "X"
        Button2.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub situation413()
        Button3.Text = "X"
        Button3.Enabled = False
        winner = "AI"
        MsgBox("The AI Won")
        NewGame()
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Button4.Text = "O"
        Button4.Enabled = False
        If turn = 1 Then
            ai1stturn()
        End If
        If turn = 3 Then
            ai4()
            secondspot = 4
        End If
        If turn = 5 Then
            If whichsituation = 1 Then
                situation41()
            End If
            If whichsituation = 3 Then
   
User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Tic Tac Toe 1 Player
21 Feb, 2007 - 02:18 PM
Post #2

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
When restarting the game, you need to change all indicators to the start position, ie winner = ? , etc.
User is offlineProfile CardPM
+Quote Post

orangeslide8
RE: Tic Tac Toe 1 Player
21 Feb, 2007 - 05:31 PM
Post #3

D.I.C Head
Group Icon

Joined: 29 Dec, 2006
Posts: 190



Thanked: 1 times
Dream Kudos: 25
My Contributions
QUOTE(KeyWiz @ 21 Feb, 2007 - 03:18 PM) *

When restarting the game, you need to change all indicators to the start position, ie winner = ? , etc.

ya i set the turn back to 0 but the others don't really matter do they?
thnx
john

User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Tic Tac Toe 1 Player
21 Feb, 2007 - 07:23 PM
Post #4

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
of course they do. You design the program to go from a dead start value.
You assume a null value until assigned so your code will let a prior
games history affect the new games descion tree.
Get used to Initiating or Clearing your variables when
restarting any program to a certain spot.

There used to be a CLEAR command which when used would reset ALL variables to initial
values, but this is OOOOLLLLDDDDD knowlage. I am unsure of the CLEAR command
in VB6 and up, as I haven't used a program in years where I could or would clear all values.

This post has been edited by KeyWiz: 21 Feb, 2007 - 07:25 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 03:11PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month