Jump to content

Making a more advanced two player tic tac toe game

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Demon

Demon

    Newbie

  • Members
  • PipPip
  • 18 posts
Introduction

This game was originally made by Arjun I have modified it additionally this tutorial is of my own not written by him. I thought I'd write a useful tut here for once as I usually direct my tuts to other sites but I'll post more here from now.

Form 1 GUI

2 Labels (text player 1 and player 2)
2 text boxes (next to them)
2 combo boxes (next to the textboxes)
2 more textboxes (next to the comboboxes)
2 buttons (next to the textboxes both text add emotion)
2 buttons at the bottom (text Exit and start)

I put them into two columns for the 2 different players but do as you wish ^^

Form1 code:

Double click Form1 and use this code:

Code:

'This game was originally made by Arjun and modified by Demon

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'shows message box with the title "Credits"        

MessageBox.Show("This game was originally made by Arjun and modified by Demon", "Credits")

'adds these items to the combobox        

comboBox1.Items.Add("o")

        comboBox1.Items.Add("->")

        comboBox1.Items.Add("$")

        comboBox2.Items.Add("*")

        comboBox2.Items.Add("<-")

        comboBox2.Items.Add("#")

        comboBox1.Items.Add("=D")

        comboBox2.Items.Add("={")

When the form opens it displays a message box as you can see the second one you may not understand as well it adds those choices to the combo box which means you don't need a boring x and o you can use those but later we'll add something so people can choose any text.

For your exit button use this code:

Code:

End 'exits the program

For your start button:

Code:

If textBox1.Text = "" And textBox2.Text = "" And comboBox1.Text = "" And comboBox2.Text = "" Then

            MsgBox("Enter Required values") 'shows a message box if nothing's selected

        Else 'otherwise

            ply1 = textBox1.Text 'player one's name is textbox1's text

            ply2 = textBox2.Text 'player 2's name is textbox2.text

            ply1_symbl = comboBox1.Text 'Player one's symbol is combobox1.text

            ply2_symbl = comboBox2.Text 'Player two's symbol is combobox2.Text

            Call cal()


            Form2.Show() 'shows form2

            Form2.Refresh() 'refreshes

            Me.Hide() 'hides this screen

        End If 'end if


    End Sub

This code will choose the player's avatar,watch if they typed in nothing and change the name for a more in depth look at the ' in the code.

Private sub Cal:


Code:

Private Sub cal()

'changes the button's text       

Form2.Button1.Text = "Tic"

        Form2.Button2.Text = "Tac"

        Form2.Button3.Text = "Toe"

        Form2.Button4.Text = "Aka"

        Form2.Button5.Text = "Three"

        Form2.Button6.Text = "In"

        Form2.Button7.Text = "A"

        Form2.Button8.Text = "Row"

        Form2.Button9.Text = "=D"

'makes them enabled       

Form2.Button1.Enabled = True

        Form2.Button2.Enabled = True

        Form2.Button3.Enabled = True

        Form2.Button4.Enabled = True

        Form2.Button5.Enabled = True

        Form2.Button6.Enabled = True

        Form2.Button7.Enabled = True

        Form2.Button8.Enabled = True

        Form2.Button9.Enabled = True

        

    End Sub

Enables the button and changes their text.

How to add emotions to the combo box:

For player one:

Code:

comboBox1.Items.Add(TextBox3.Text) 'adds whatever it says in the textbox

For player two:

Code:

comboBox2.Items.Add(TextBox4.Text)

This adds based on what it says in either textbox

if you'd like to be clever for the mouse hover event use the code:

For player one:

Code:

Label1.Text = Textbox1.Text 'meaning they have the same text

For player two:

Code:

Label2.Text = Textbox2.Text

This means they have the same text.I didn't though as I find it fairly useless x]

---End of form1--

Form2 Gui

9 buttons the text will be changed so you can write whatever you like.
A label do a line so you can see it.

Form 2 code

Code:

Public Class Form2

    Dim c As Integer

    Dim turn As String

    Dim sym As String

    Dim count As Integer = 0

All your dim commands which you will need to start.

double click on Form2

Code:

turn = ply1

        Call check()

It's player one's turn then it calls check.

Here's check since it was called:

Code:

Public Sub check()

        If turn = ply1 Then

            Label1.Text = ply1 & "'s Turn"

            turn = ply1

            sym = ply1_symbl

        Else

            Label1.Text = ply2 & "'s Turn"

            turn = ply2

            sym = ply2_symbl

        End If

If it's player 1's turn then the label says ply1 & " 's turn" player one's turn their symbol is their symbol otherwise the label's text is player 2's turn,it's their turn and their symbol is player 2's symbol,ends if.

Button 1 through 9's code:

Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Button1.Enabled = False

        Call check()

        If turn = ply1 Then

            Button1.Text = sym

            turn = ply2

        Else

            Button1.Text = sym

            turn = ply1

        End If

        count = count + 1

        If Button1.Text = sym And Button2.Text = sym And Button3.Text = sym Then

            Call com()

        ElseIf Button1.Text = sym And Button4.Text = sym And Button7.Text = sym Then

            Call com()


        ElseIf Button1.Text = sym And Button5.Text = sym And Button9.Text = sym Then

            Call com()


        End If

        Call com1()

        Call check()

    End Sub


    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

        Button2.Enabled = False

        Call check()

        If turn = ply1 Then

            Button2.Text = sym

            turn = ply2

        Else

            Button2.Text = sym

            turn = ply1

        End If

        count = count + 1

        If Button2.Text = sym And Button1.Text = sym And Button3.Text = sym Then

            Call com()


        ElseIf Button2.Text = sym And Button5.Text = sym And Button8.Text = sym Then

            Call com()

        End If

        Call com1()

        Call check()

    End Sub


    Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

        Button3.Enabled = False

        Call check()

        If turn = ply1 Then

            Button3.Text = sym

            turn = ply2

        Else

            Button3.Text = sym

            turn = ply1

        End If

        count = count + 1

        If Button3.Text = sym And Button1.Text = sym And Button2.Text = sym Then

            Call com()

        ElseIf Button3.Text = sym And Button6.Text = sym And Button9.Text = sym Then

            Call com()

        ElseIf Button3.Text = sym And Button5.Text = sym And Button7.Text = sym Then

            Call com()

        End If

        Call com1()

        Call check()

    End Sub


    Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click

        Button4.Enabled = False

        Call check()

        If turn = ply1 Then

            Button4.Text = sym

            turn = ply2

        Else

            Button4.Text = sym

            turn = ply1

        End If

        count = count + 1

        If Button4.Text = sym And Button1.Text = sym And Button7.Text = sym Then

            Call com()

        ElseIf Button4.Text = sym And Button5.Text = sym And Button6.Text = sym Then

            Call com()

            

        End If

        Call com1()

        Call check()

    End Sub


    Private Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click

        Button5.Enabled = False

        Call check()

        If turn = ply1 Then

            Button5.Text = sym

            turn = ply2

        Else

            Button5.Text = sym

            turn = ply1

        End If

        count = count + 1

        If Button5.Text = sym And Button4.Text = sym And Button6.Text = sym Then

            Call com()

        ElseIf Button5.Text = sym And Button2.Text = sym And Button8.Text = sym Then

            Call com()

        ElseIf Button5.Text = sym And Button1.Text = sym And Button9.Text = sym Then

            Call com()

        ElseIf Button5.Text = sym And Button3.Text = sym And Button7.Text = sym Then

            Call com()


        End If

        Call com1()

        Call check()

    End Sub


    Private Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click

        Button6.Enabled = False

        Call check()

        If turn = ply1 Then

            Button6.Text = sym

            turn = ply2

        Else

            Button6.Text = sym

            turn = ply1

        End If

        count = count + 1

        If Button6.Text = sym And Button4.Text = sym And Button5.Text = sym Then

            Call com()

        ElseIf Button6.Text = sym And Button3.Text = sym And Button9.Text = sym Then

            Call com()

            

        End If

        Call com1()

        Call check()

    End Sub


    Private Sub Button7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button7.Click

        Button7.Enabled = False

        Call check()

        If turn = ply1 Then

            Button7.Text = sym

            turn = ply2

        Else

            Button7.Text = sym

            turn = ply1

        End If

        count = count + 1

        If Button7.Text = sym And Button1.Text = sym And Button4.Text = sym Then

            Call com()

        ElseIf Button7.Text = sym And Button8.Text = sym And Button9.Text = sym Then

            Call com()

        ElseIf Button7.Text = sym And Button5.Text = sym And Button3.Text = sym Then

            Call com()

        End If

        Call com1()

        Call check()

    End Sub


    Private Sub Button8_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button8.Click

        Button8.Enabled = False

        Call check()

        If turn = ply1 Then

            Button8.Text = sym

            turn = ply2

        Else

            Button8.Text = sym

            turn = ply1

        End If

        count = count + 1

        If Button8.Text = sym And Button7.Text = sym And Button9.Text = sym Then

            Call com()

        ElseIf Button8.Text = sym And Button2.Text = sym And Button5.Text = sym Then

            Call com()

           

        End If

        Call com1()

        Call check()

    End Sub


    Private Sub Button9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button9.Click

        Button9.Enabled = False

        Call check()

        If turn = ply1 Then

            Button9.Text = sym

            turn = ply2

        Else

            Button9.Text = sym

            turn = ply1

        End If

        count = count + 1

        If Button9.Text = sym And Button6.Text = sym And Button3.Text = sym Then

            Call com()

        ElseIf Button9.Text = sym And Button8.Text = sym And Button7.Text = sym Then

            Call com()


        ElseIf Button9.Text = sym And Button5.Text = sym And Button1.Text = sym Then

            Call com()

        End If

        Call com1()

        Call check()

    End Sub

You can't click them again if it's player one's turn the button's text is their symbol then if it's player 2's turn the text is their symbol and it's player 1's turn the count is the original count + 1 if three buttons by the means of winning (example diagonally etc have the same symbol then call endif calls com1 and calls check.

Com aka check winner:

Code:

Public Sub com()

        If sym = ply1_symbl Then

            MsgBox(ply1 & " Wins")

            c = 1

            Me.Hide()

            Form1.Show()


        ElseIf sym = ply2_symbl Then

            MsgBox(ply2 & " Wins")

            c = 1

            Me.Hide()

            Form1.Show()

        End If

        

    End Sub

if it's player 1's symbol show a message box saying player 1 one wins, hide me and show form1 (where we started)

com1 aka if no one wins

Code:

Public Sub com1()

        If count = 9 And c <> 1 Then

            MsgBox("Game over")

            Me.Hide()

            Form1.Show()


            Exit Sub


        End If


    End Sub

If the turn count is 9 meaning every square's been selected and no one's won show a message box saying game over and show form1 (where we started)

Enjoy = D

-Demon

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Good job on this Demon. This is a great way to associate logic to form controls, some people have trouble in that.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.