Support Forums

Full Version: Nice TicTacToe Game - Developed by AceInfinity [Source & Release]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I got bored and was fooling around with arrays and whatnot in Visual Studio VB.net and figured i'd make a game Smile

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

:cheers:

Preview:
[Image: 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)
        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

Note: Yes that's all the code I have in my Form1.vb lol, clean isn't it Roflmao

More Information & Download Available here: http://tech.reboot.pro/showthread.php?tid=2224 (Requires .NET Framework 3.5 minimum.)

Any questions shoot me a question in a reply to this thread or at TechLifeForum on the thread I linked to.

~Ace
Thanks for sharing mate.
Looks fun.
This is actually one of the first games i've made too lol, I'm not big on game development, it never interested me, but the concept behind this game seemed fun when I was fooling around with arrays. People should be able to learn a lot from this source. Smile

For the project download anyone that wants it will have to PM me.
Cool game. Big Grin
Thanks for sharing the Source Code.