Results 1 to 5 of 5

Thread: [VB.Net] TicTacToe

Hybrid View

  1. #1
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,205

    [VB.Net] TicTacToe

    This is the source code and the .exe.

    Features:
    -Player V Player
    -Player V CPU
    -Reset Score
    -Give up

    Notes:
    -I accenditally deleted all of my stuff, so I'm trying to rebuild everything I deleted. This was the first thing.

    Full Project:
    tic-tac-toe.zip

    Source:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private grid(2, 2) As Panel
        Private p1turn As Boolean
        Private r As New Random
    
    #Region "New/End game"
    
        Private Sub newgame()
            For i As Integer = pnl_Grid.Controls.Count - 1 To 0 Step -1
                pnl_Grid.Controls.RemoveAt(i)
            Next
    
            For x As Integer = 0 To 2
                For y As Integer = 0 To 2
                    Dim pnl As New Panel
                    With pnl
                        .BackgroundImage = Nothing
                        .BackgroundImageLayout = ImageLayout.Zoom
                        .BorderStyle = BorderStyle.FixedSingle
                        .Enabled = True
                        .Location = New Point(x * 100, y * 100)
                        .Size = New Size(100, 100)
                        .Tag = ""
                    End With
    
                    pnl_Grid.Controls.Add(pnl)
                    grid(x, y) = pnl
    
                    AddHandler pnl.Click, AddressOf pnl_Click
                Next
            Next
    
            p1turn = True
        End Sub
    
        Private Function checkend() As Boolean
    
            'Horizontal
            If grid(0, 0).Tag.ToString = "p1" AndAlso grid(1, 0).Tag.ToString = "p1" AndAlso grid(2, 0).Tag.ToString = "p1" Then
                MessageBox.Show("Player1 wins")
                Call newgame()
                Return True
            ElseIf grid(0, 0).Tag.ToString = "p2" AndAlso grid(1, 0).Tag.ToString = "p2" AndAlso grid(2, 0).Tag.ToString = "p2" Then
                MessageBox.Show("Player2 wins")
                Call newgame()
                Return True
            ElseIf grid(0, 1).Tag.ToString = "p1" AndAlso grid(1, 1).Tag.ToString = "p1" AndAlso grid(2, 1).Tag.ToString = "p1" Then
                MessageBox.Show("Player1 wins")
                Call newgame()
                Return True
            ElseIf grid(0, 1).Tag.ToString = "p2" AndAlso grid(1, 1).Tag.ToString = "p2" AndAlso grid(2, 1).Tag.ToString = "p2" Then
                MessageBox.Show("Player2 wins")
                Call newgame()
                Return True
            ElseIf grid(0, 2).Tag.ToString = "p1" AndAlso grid(1, 2).Tag.ToString = "p1" AndAlso grid(2, 2).Tag.ToString = "p1" Then
                MessageBox.Show("Player1 wins")
                Call newgame()
                Return True
            ElseIf grid(0, 2).Tag.ToString = "p2" AndAlso grid(1, 2).Tag.ToString = "p2" AndAlso grid(2, 2).Tag.ToString = "p2" Then
                MessageBox.Show("Player2 wins")
                Call newgame()
                Return True
    
                'Horizontal
            ElseIf grid(0, 0).Tag.ToString = "p1" AndAlso grid(0, 1).Tag.ToString = "p1" AndAlso grid(0, 2).Tag.ToString = "p1" Then
                MessageBox.Show("Player1 wins")
                Call newgame()
                Return True
            ElseIf grid(0, 0).Tag.ToString = "p2" AndAlso grid(0, 1).Tag.ToString = "p2" AndAlso grid(0, 2).Tag.ToString = "p2" Then
                MessageBox.Show("Player2 wins")
                Call newgame()
                Return True
            ElseIf grid(1, 0).Tag.ToString = "p1" AndAlso grid(1, 1).Tag.ToString = "p1" AndAlso grid(1, 2).Tag.ToString = "p1" Then
                MessageBox.Show("Player1 wins")
                Call newgame()
                Return True
            ElseIf grid(1, 0).Tag.ToString = "p2" AndAlso grid(1, 1).Tag.ToString = "p2" AndAlso grid(1, 2).Tag.ToString = "p2" Then
                MessageBox.Show("Player2 wins")
                Call newgame()
                Return True
            ElseIf grid(2, 0).Tag.ToString = "p1" AndAlso grid(2, 1).Tag.ToString = "p1" AndAlso grid(2, 2).Tag.ToString = "p1" Then
                MessageBox.Show("Player1 wins")
                Call newgame()
                Return True
            ElseIf grid(2, 0).Tag.ToString = "p2" AndAlso grid(2, 1).Tag.ToString = "p2" AndAlso grid(2, 2).Tag.ToString = "p2" Then
                MessageBox.Show("Player2 wins")
                Call newgame()
                Return True
    
                'Diagonal
            ElseIf grid(0, 0).Tag.ToString = "p1" AndAlso grid(1, 1).Tag.ToString = "p1" AndAlso grid(2, 2).Tag.ToString = "p1" Then
                MessageBox.Show("Player1 wins")
                Call newgame()
                Return True
            ElseIf grid(0, 0).Tag.ToString = "p2" AndAlso grid(1, 1).Tag.ToString = "p2" AndAlso grid(2, 2).Tag.ToString = "p2" Then
                MessageBox.Show("Player2 wins")
                Call newgame()
                Return True
            ElseIf grid(0, 2).Tag.ToString = "p1" AndAlso grid(1, 1).Tag.ToString = "p1" AndAlso grid(2, 0).Tag.ToString = "p1" Then
                MessageBox.Show("Player1 wins")
                Call newgame()
                Return True
            ElseIf grid(0, 2).Tag.ToString = "p2" AndAlso grid(1, 1).Tag.ToString = "p2" AndAlso grid(2, 0).Tag.ToString = "p2" Then
                MessageBox.Show("Player2 wins")
                Call newgame()
                Return True
            Else
    
                Return False
            End If
    
        End Function
    
    #End Region
    
    #Region "Draw Bitmap and AI"
    
        Private Function drawpiece() As Bitmap
            drawpiece = New Bitmap(50, 50)
            Dim g As Graphics = Graphics.FromImage(drawpiece)
    
            'p1 is x
            'cpu is o
            If p1turn Then
                Dim pt1, pt2, pt3, pt4 As Point
                pt1 = New Point(1, 1) 'Upper-Left
                pt2 = New Point(49, 49) 'Lower-Right
                pt3 = New Point(1, 49) 'Lower-Left
                pt4 = New Point(49, 1) 'Upper-Right
    
                g.DrawLine(Pens.Red, pt1, pt2)
                g.DrawLine(Pens.Red, pt3, pt4)
    
                g.Save()
            Else
                Dim rect As New Rectangle(1, 1, 48, 48)
                g.DrawEllipse(Pens.Blue, rect)
                g.Save()
            End If
    
            Return drawpiece
        End Function
    
        Private Function rndPanel() As Panel
            Dim i As Integer = 0
            Dim templist As New List(Of Panel)
    
            For Each pnl As Panel In pnl_Grid.Controls.OfType(Of Panel)()
                If pnl.Enabled = True Then
                    i += 1
                    templist.Add(pnl)
                End If
            Next
    
            rndPanel = templist.Item(r.Next(0, i))
    
            Return rndPanel
        End Function
    
    #End Region
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Call newgame()
        End Sub
    
        Private Sub pnl_Click(sender As Object, e As EventArgs)
            Dim pnl As Panel = DirectCast(sender, Panel)
            pnl.BackgroundImage = drawpiece() : pnl.Tag = "p1" : pnl.Enabled = False
            p1turn = False
    
            If checkend() = True Then
                Exit Sub
            End If
    
            Dim randpnl As Panel = rndPanel()
            randpnl.BackgroundImage = drawpiece() : randpnl.Tag = "p2" : randpnl.Enabled = False
            p1turn = True
    
            If checkend() = True Then
                Exit Sub
            End If
        End Sub
    
    End Class

  2. #2
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,205

    Re: [VB.Net] TicTacToe

    One thing that I forgot to do when I updated the code was in the resetGame sub I would change typing out all that button.whatever to:
    Code:
    For Each Button In Panel1.Controls.OfType(Of Button)()
                Button.Enabled = True
                Button.Text = String.Empty
            Next
    I was more focused on working with ai rather than coding basics. Just a little tit tat from me.


    Edit - Also, take a look at this function if you want the moves to be much more random:
    Code:
    Private Function randomBtn() As Button
            Dim list As New List(Of Button)
            For Each btn As Button In Panel1.Controls.OfType(Of Button)()
                If btn.Enabled = True Then
                    list.Add(btn)
                End If
            Next
            Dim r As New Random
            Dim i As Integer
            i = r.Next(0, list.Count - 1)
            randomBtn = list.Item(i)
    
        End Function
    then in the ai sub, simply comment(or delete) everything in it and replace it with:
    Code:
    If player1Turn = False Then
                randomBtn.PerformClick()
                player1Turn = True
            End If

  3. #3
    Lively Member HunterTTP's Avatar
    Join Date
    Jul 12
    Posts
    115

    Lightbulb Re: [VB.Net] TicTacToe

    Maybe for the AI, have the program detect which buttons have already been used; then just have it pick one of the buttons that haven't been used. The only hard part with this is having it "pick". It could randomly generate a number between 1 and 9, (lets say it picks 7) then it checks if button 7 is already occupied, if it is, it generates a new random number (over and over again).

  4. #4
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,205

    Re: [VB.Net] TicTacToe

    Basically what the edit does in my edit in post #2, is it puts all buttons in the panel that aren't enabled into a list and then choose a random button from there. The problem with that is that if there is an obvious win for player1, the ai may not block it.

  5. #5
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,205

    Re: [VB.Net] TicTacToe

    UPDATE
    I've made a pretty major update. I've replaced the buttons with panels and draw the x's and o's on the panels. Also there is a difficulty setting and statistics dialog now:

    Game:
    Attachment 95043

    Difficulty:
    Attachment 95039

    Stats:
    Attachment 95041

    Game Zip:
    Attachment 95045

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •