1. #1
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,725

    Simple TicTacToe Game [Basic] - Source

    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



    Preview:


    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.
    Attached Files Attached Files
    Last edited by AceInfinity; 03-30-2012 at 05:33 PM.
    Automation Programmer
    Microsoft MVP [2012 - 2018]


    • Ad Bot

      advertising
      Beep.

        
       

  2. #2
    Laxer's Avatar
    Join Date
    Feb 2012
    Location
    Portland, OR
    Posts
    3,857
    • specs System Specs
      • Motherboard:
        GIGABYTE GA-Z97MX
      • CPU:
        Intel 4690K @ 4.6Ghz
      • Memory:
        Corsair Vengeance Pro 16GB @ 2666Mhz
      • Graphics:
        2x Sapphire 7970s
      • Hard Drives:
        2x Corsair Force 3GT 120GB (RAID 0) + 2x Western Digital Red 3TB (RAID 1)
      • Disk Drives:
        LG Black 10X Blu-ray Burner
      • Power Supply:
        CORSAIR 950HX
      • Case:
        Corsair 350D
      • Cooling:
        Corsair H100i
      • Display:
        3 x 22" Samsung 1080p Displays
      • Operating System:
        Windows 8 Pro x64

    Re: Simple TicTacToe Game [Basic] - Source

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

    This is simplistic and wonderful learning example.

  3. #3
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,725

    Re: Simple TicTacToe Game [Basic] - Source

    Quote Originally Posted by Laxer View Post
    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.
    Automation Programmer
    Microsoft MVP [2012 - 2018]

Similar Threads

  1. [C#] Console Based Game - TicTacToe
    By AceInfinity in forum Programming
    Replies: 5
    Last Post: 03-30-2013, 11:42 AM
  2. Windows 7 x64 Home Basic BSODs
    By apoorvmathur93 in forum BSOD, Crashes, Kernel Debugging
    Replies: 4
    Last Post: 02-13-2013, 06:02 PM
  3. Basic dump information - small app
    By mgrzeg in forum BSOD Processing Apps Download | Information | Discussions
    Replies: 7
    Last Post: 09-01-2012, 07:26 PM
  4. Basic Programming
    By 2xg in forum Programming
    Replies: 8
    Last Post: 08-05-2012, 01:43 PM
  5. A possible, basic, explanation (for a user) of why Video drivers crash systems
    By Shintaro in forum BSOD, Crashes, Kernel Debugging
    Replies: 6
    Last Post: 07-30-2012, 06:38 PM

Log in

Log in