Simple TicTacToe Game [Basic] - Source

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
So I got bored and was fooling around with arrays and whatnot in Visual Studio VB.net and figured i'd make a game :) Time to have some programming content in this forum. I'll also be writing some batch stuff as well, and even C++, perl, and powershell. Possibly a bit of VBscript as well, as these are all languages that I know, but my favorites are definitely C++, batch and powershell.

There's lots of good content in here for people just learning out, so i'm releasing the source with this as well for anybody that wants to take a look and learn from it. This source is packed with lots of good code that can help a beginner out. Arrays, lists, basic LINQ, and classes.

Have fun playing and learning from the source

:thumbsup2:

Preview:
iITkz.png


Source:
Form1.vb
Code:
Option Strict On

Public Class Form1

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

    Private Sub Button_Clicks(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, _
                                                                                    Button4.Click, Button5.Click, Button6.Click, _
                                                                                    Button7.Click, Button8.Click, Button9.Click

        Dim Btn As Button = DirectCast(sender, Button)
        If Not String.IsNullOrEmpty(Btn.Text) Then Exit Sub
        Btn.Text = Label1.Text.ElementAt(13)

        Dim CurrentMove As New PlayerMove With {.XO_Player = Label1.Text.ElementAt(13), .ClickNum = Btn.Tag.ToString}
        XO.PMove = CurrentMove

        Select Case Label1.Text.ElementAt(13)
            Case "X"c
                Label1.Text = "Player Turn: O"
            Case "O"c
                Label1.Text = "Player Turn: X"
        End Select
    End Sub
End Class

EventHandles.vb (Module):
Code:
Option Strict On

Module EventHandles
    Public X_Plays As List(Of String)
    Public O_Plays As List(Of String)

    Public WithEvents XO As New NumValCheck
    Private Sub NumberValInput(ByVal ButtonNum As String, PlayerXO As Char) Handles XO.CheckPlay
        Select Case PlayerXO
            Case "X"c
                X_Plays.Add(ButtonNum)
            Case "O"c
                O_Plays.Add(ButtonNum)
        End Select

        'we know a player can't win without having made 3 plays yet
        If X_Plays.Count >= 3 OrElse O_Plays.Count >= 3 Then CheckForWinner()
    End Sub

    Private Sub CheckForWinner()
        'If not winner don't do anything, if there's a winner, then reset the List's
        'display winner
        Dim Lists As List(Of String)() = {X_Plays, O_Plays}
        For plist As Integer = 0 To 1
            With Lists(plist)
                Dim pl As String
                If plist = 0 Then pl = "X" Else pl = "O"
                If (.Contains("02") And .Contains("11") And .Contains("20")) Or _
                   (.Contains("00") And .Contains("11") And .Contains("22")) Then FoundWinner(pl)
                For c As Integer = 0 To 2
                    Dim c_val As String = CStr(c)
                    If (Array.FindAll(.ToArray, Function(i) i.ToString.StartsWith(c_val)).Count = 3) Or _
                       (Array.FindAll(.ToArray, Function(i) i.ToString.EndsWith(c_val)).Count = 3) Then FoundWinner(pl)
                Next
            End With
        Next
    End Sub

    Public Sub RollTheDice()
        Dim x As New Random(32)
        Select Case CInt(x.Next(New Random().Next())) Mod 2
            Case 0
                Form1.Label1.Text = "Player Turn: X"
            Case Else
                Form1.Label1.Text = "Player Turn: O"
        End Select
    End Sub

    Private Sub FoundWinner(Winner As String)
        Select Case Winner
            Case "X"
                MessageBox.Show("Player [X] has won this game!")
            Case "O"
                MessageBox.Show("Player [O] has won this game!")
        End Select
        ReloadGame()
        RollTheDice()
    End Sub

    Public Sub ReloadGame()
        X_Plays = New List(Of String)(9)
        O_Plays = New List(Of String)(9)
        For Each Ctrl As Control In Form1.Controls
            If TryCast(Ctrl, Button) IsNot Nothing Then
                Ctrl.Text = String.Empty
            End If
        Next
    End Sub
End Module

Public Class PlayerMove
    Public Property XO_Player As Char
    Public Property ClickNum As String
End Class

Public Class NumValCheck
    Private CheckPlayVal As PlayerMove
    Public Event CheckPlay(ByVal num As String, ByVal player As Char)

    Public Property PMove() As PlayerMove
        Get
            PMove = CheckPlayVal
        End Get
        Set(ByVal value As PlayerMove)
            CheckPlayVal = value
            RaiseEvent CheckPlay(value.ClickNum, value.XO_Player)
        End Set
    End Property
End Class

Download from attachment Requires .NET Framework 3.5 minimum.

*Edit: Fixed source bug.
 

Attachments

Last edited:
That's pretty cool Ace!
I have never been much of a GUI programmer...

This is simplistic and wonderful learning example.
 
That's pretty cool Ace!
I have never been much of a GUI programmer...

This is simplistic and wonderful learning example.

Haha, it's my favorite kind of programming :) Console application programming doesn't bother me either, and neither does scripting languages that run through the cmd console. And although this is minimal, as you say it's a good learning example!

Much, much, less convoluted than what i'm currently working on lol.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top