Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nice TicTacToe Game - Developed by AceInfinity [Source & Release]
#1
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
Reply


Messages In This Thread
Nice TicTacToe Game - Developed by AceInfinity [Source & Release] - by AceInfinity - 03-30-2012, 03:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Release] Password Enhancer v1.0 [/Release] Resistance 11 5,881 09-29-2013, 08:58 PM
Last Post: Resistance
  [SOURCE] VTSkid-Control [Release] FizzyMentos 14 4,146 09-11-2012, 08:18 PM
Last Post: malis2007
  [RELEASE] Up2Minus [MINUS UPLOADER [RELEASE] Kenneth 2 1,480 09-08-2012, 04:17 PM
Last Post: spesificrelax
  [Release] Web-Camera Security - My First Release Gerira 1 1,386 08-31-2012, 04:56 PM
Last Post: AceInfinity
  [RELEASE]SF-TutPad "Tutorial Editor" [RELEASE] Digital-Punk 28 10,167 08-11-2012, 11:25 PM
Last Post: Kenneth

Forum Jump:


Users browsing this thread: 2 Guest(s)