# Visual Basic > Games and Graphics Programming > Game Demos >  [VB.Net] Dots and Boxes

## dday9

This is the game dots! Currently it's a player vs. player, but I do plan to expand it to player vs. computer.

Screen Shot:


The zipped file without the binaries:
Dots.zip

The source is to much for one post, so I'll break it up.

----------


## dday9

Rect Class Source:


```
Option Strict On
Option Explicit On

Imports System.ComponentModel
Public Class Rect
    Inherits Windows.Forms.Panel

#Region "Properties"

    Private h_color As Color = Color.Gray
    <Description("Gets/Sets the color of a line when the mouse is hovering over a line.")> _
    Public Property HoverColor() As Color
        Get
            Return h_color
        End Get
        Set(ByVal value As Color)
            h_color = value
        End Set
    End Property

    Private s_color As Color = Color.Black
    <Description("Gets/Sets the color of a line when the line is selected.")> _
    Public Property SelectedColor() As Color
        Get
            Return s_color
        End Get
        Set(ByVal value As Color)
            s_color = value
        End Set
    End Property

    Private b_color As Color = Me.BackColor
    <Description("Gets/Sets the color of a line when the line is not selected.")> _
    Public Property BlankColor() As Color
        Get
            Return b_color
        End Get
        Set(ByVal value As Color)
            b_color = value
        End Set
    End Property

    Private f_color As Color = Color.Blue
    <Description("Gets/Sets the color of the rectangle when all the lines are selected.")> _
    Public Property FilledColor() As Color
        Get
            Return f_color
        End Get
        Set(ByVal value As Color)
            f_color = value
        End Set
    End Property

    Private t_line As Rectangle = Nothing
    <Description("Gets the start and end points of the top line.")> _
    Public ReadOnly Property TopLine() As Rectangle
        Get
            Return t_line
        End Get
    End Property

    Private t_hover As Boolean = False
    <Description("Gets/Sets if the mouse is hovering over the top line.")> _
    Public Property TopHovered() As Boolean
        Get
            Return t_hover
        End Get
        Set(ByVal value As Boolean)
            t_hover = value
        End Set
    End Property

    Private t_selected As Boolean = False
    <Description("Gets/Sets if the top line is selected.")> _
    Public Property TopSelected() As Boolean
        Get
            Return t_selected
        End Get
        Set(ByVal value As Boolean)
            t_selected = value
        End Set
    End Property

    Private r_line As Rectangle = Nothing
    <Description("Gets the start and end points of the right line.")> _
    Public ReadOnly Property RightLine() As Rectangle
        Get
            Return r_line
        End Get
    End Property

    Private r_hover As Boolean = False
    <Description("Gets/Sets if the mouse is hovering over the right line.")> _
    Public Property RightHovered() As Boolean
        Get
            Return r_hover
        End Get
        Set(ByVal value As Boolean)
            r_hover = value
        End Set
    End Property

    Private r_selected As Boolean = False
    <Description("Gets/Sets if the right line is selected.")> _
    Public Property RightSelected() As Boolean
        Get
            Return r_selected
        End Get
        Set(ByVal value As Boolean)
            r_selected = value
        End Set
    End Property

    Private b_line As Rectangle = Nothing
    <Description("Gets the start and end points of the bottom line.")> _
    Public ReadOnly Property BottomLine() As Rectangle
        Get
            Return b_line
        End Get
    End Property

    Private b_hover As Boolean = False
    <Description("Gets/Sets if the mouse is hovering over the bottom line.")> _
    Public Property BottomHovered() As Boolean
        Get
            Return b_hover
        End Get
        Set(ByVal value As Boolean)
            b_hover = value
        End Set
    End Property

    Private b_selected As Boolean = False
    <Description("Gets/Sets if the bottom line is selected.")> _
    Public Property BottomSelected() As Boolean
        Get
            Return b_selected
        End Get
        Set(ByVal value As Boolean)
            b_selected = value
        End Set
    End Property

    Private l_line As Rectangle = Nothing
    <Description("Gets the start and end points of the left line.")> _
    Public ReadOnly Property LeftLine() As Rectangle
        Get
            Return l_line
        End Get
    End Property

    Private l_hover As Boolean = False
    <Description("Gets/Sets if the mouse is hovering over the left line.")> _
    Public Property LeftHovered() As Boolean
        Get
            Return l_hover
        End Get
        Set(ByVal value As Boolean)
            l_hover = value
        End Set
    End Property

    Private l_selected As Boolean = False
    <Description("Gets/Sets if the left line is selected.")> _
    Public Property LeftSelected() As Boolean
        Get
            Return l_selected
        End Get
        Set(ByVal value As Boolean)
            l_selected = value
        End Set
    End Property

    Private thickness As Integer = 5
    <Description("Gets/Sets the thickness of the line. If the line is at the top or the bottom, then the thickness represents the height. If the line is at the right or left, then the thickness represents it's width.")> _
    Public Property LineThickness() As Integer
        Get
            Return thickness
        End Get
        Set(ByVal value As Integer)
            thickness = value
        End Set
    End Property

#End Region

#Region "Methods"

    Private Sub SetRectangles()
        t_line = New Rectangle(New Point(0, 0), New Size(Me.Width, thickness))
        r_line = New Rectangle(New Point(Me.ClientSize.Width - thickness, 0), New Size(thickness, Me.Height))
        b_line = New Rectangle(New Point(0, Me.ClientSize.Height - thickness), New Size(Me.Width, thickness))
        l_line = New Rectangle(New Point(0, 0), New Size(thickness, Me.Height))
    End Sub

    Private Function GetLine(ByVal location As Point) As Rectangle
        Dim r As New Rectangle(location, New Size(1, 1))

        If r.IntersectsWith(t_line) Then
            Return t_line
        ElseIf r.IntersectsWith(r_line) Then
            Return r_line
        ElseIf r.IntersectsWith(b_line) Then
            Return b_line
        ElseIf r.IntersectsWith(l_line) Then
            Return l_line
        Else
            Return Nothing
        End If

    End Function

    Public Function IsFilled() As Boolean
        If t_selected AndAlso r_selected AndAlso b_selected AndAlso l_selected Then
            Return True
        Else
            Return False
        End If
    End Function

#End Region

#Region "Events"

    Public Event LineClicked As Action(Of Rect, Rectangle)

#End Region

#Region "Event Handlers"

    Private Sub Rect_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        Dim r As Rectangle = GetLine(e.Location)

        If IsNothing(r) = False Then
            Select Case r
                Case t_line
                    If t_selected = False Then t_selected = True : RaiseEvent LineClicked(Me, t_line)
                Case r_line
                    If r_selected = False Then r_selected = True : RaiseEvent LineClicked(Me, r_line)
                Case b_line
                    If b_selected = False Then b_selected = True : RaiseEvent LineClicked(Me, b_line)
                Case l_line
                    If l_selected = False Then l_selected = True : RaiseEvent LineClicked(Me, l_line)
            End Select

            Me.Invalidate()
        End If
    End Sub

    Private Sub Rect_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
        t_hover = False
        r_hover = False
        b_hover = False
        l_hover = False

        Me.Invalidate()
    End Sub

    Private Sub Rect_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Dim r As Rectangle = GetLine(e.Location)

        If IsNothing(r) = False Then
            Select Case r
                Case t_line
                    t_hover = True
                    r_hover = False
                    b_hover = False
                    l_hover = False
                Case r_line
                    r_hover = True
                    t_hover = False
                    b_hover = False
                    l_hover = False
                Case b_line
                    b_hover = True
                    t_hover = False
                    r_hover = False
                    l_hover = False
                Case l_line
                    l_hover = True
                    t_hover = False
                    r_hover = False
                    b_hover = False
                Case Else
                    t_hover = False
                    r_hover = False
                    b_hover = False
                    l_hover = False
            End Select

            Me.Invalidate()
        End If
    End Sub

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        Call SetRectangles()

        'Fill in the rectangle if all the lines are selected
        If IsFilled() = True Then e.Graphics.FillRectangle(New SolidBrush(f_color), Me.DisplayRectangle)

        'Top Line
        If t_selected Then
            e.Graphics.FillRectangle(New SolidBrush(s_color), t_line)
        ElseIf t_hover Then
            e.Graphics.FillRectangle(New SolidBrush(h_color), t_line)
        End If

        'Right Line
        If r_selected Then
            e.Graphics.FillRectangle(New SolidBrush(s_color), r_line)
        ElseIf r_hover Then
            e.Graphics.FillRectangle(New SolidBrush(h_color), r_line)
        End If

        'Bottom Line
        If b_selected Then
            e.Graphics.FillRectangle(New SolidBrush(s_color), b_line)
        ElseIf b_hover Then
            e.Graphics.FillRectangle(New SolidBrush(h_color), b_line)
        End If

        'Left Line
        If l_selected Then
            e.Graphics.FillRectangle(New SolidBrush(s_color), l_line)
        ElseIf l_hover Then
            e.Graphics.FillRectangle(New SolidBrush(h_color), l_line)
        End If

    End Sub

#End Region

    Public Sub New()
        Me.DoubleBuffered = True
    End Sub

End Class
```

----------


## dday9

Form1 Source:


```
Option Strict On
Option Explicit On
Public Class Form1

    Private p1turn As Boolean
    Private p1score As Int16
    Private p2score As Int16

    Private Sub NewGame()
        'Reset globals
        p1turn = True
        p1score = 0
        p2score = 0

        'Remove any handlers
        For Each r As Rect In Panel1.Controls.OfType(Of Rect)()
            RemoveHandler r.LineClicked, AddressOf rect_lineclicked
        Next

        'Clear out any rects
        Panel1.Controls.Clear()

        'Add back a 12x12 grid of rects
        For x As Integer = 0 To 11
            For y As Integer = 0 To 11
                Dim r As New Rect
                With r
                    .BorderStyle = BorderStyle.FixedSingle
                    .LineThickness = 3
                    .Location = New Point(x * 25, y * 25)
                    .Name = x.ToString & y.ToString
                    .Size = New Size(25, 25)
                End With
                AddHandler r.LineClicked, AddressOf rect_lineclicked

                Panel1.Controls.Add(r)
            Next
        Next

    End Sub

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

    Private Enum Line
        Top
        Right
        Bottom
        Left
    End Enum

    Private Sub rect_lineclicked(ByVal box As Rect, ByVal line As Rectangle)
        If p1turn Then
            Call FillBox(box, Color.CornflowerBlue)
        Else
            Call FillBox(box, Color.Firebrick)
        End If

        'Get the line that was selected
        Select Case line
            Case box.TopLine
                Call SetVisibleLine(box, Form1.Line.Top)
            Case box.RightLine
                Call SetVisibleLine(box, Form1.Line.Right)
            Case box.BottomLine
                Call SetVisibleLine(box, Form1.Line.Bottom)
            Case box.LeftLine
                Call SetVisibleLine(box, Form1.Line.Left)
        End Select

        'Check for a win, is so then reset
        If CheckWin() = True Then
            If p1score > p2score Then
                MessageBox.Show("Player1 won!", "Dots")
            ElseIf p1score < p2score Then
                MessageBox.Show("Player2 won!", "Dots")
            Else
                MessageBox.Show("Draw.", "Dots")
            End If
            Call NewGame()
        End If

    End Sub

    Private Sub SetVisibleLine(ByVal box As Rect, ByVal line As Line)
        'Get the location of the box in relation to the grid
        Dim location As String = box.Name

        'Check to see if there is an adjacent line and set it to selected

        Select Case line
            Case Form1.Line.Top
                Dim new_location As String = location.Substring(0, 1) & (CInt(location.Substring(1)) - 1).ToString
                Dim possible_rect As Rect = DirectCast(Panel1.Controls(new_location), Rect)

                If possible_rect IsNot Nothing Then
                    possible_rect.BottomSelected = True
                    possible_rect.Invalidate()
                End If
            Case Form1.Line.Right
                Dim new_location As String = (CInt(location.Substring(0, 1)) + 1).ToString & location.Substring(1, 1)
                Dim possible_rect As Rect = DirectCast(Panel1.Controls(new_location), Rect)

                If possible_rect IsNot Nothing Then
                    possible_rect.LeftSelected = True
                    possible_rect.Invalidate()
                End If
            Case Form1.Line.Bottom
                Dim new_location As String = location.Substring(0, 1) & (CInt(location.Substring(1)) + 1).ToString
                Dim possible_rect As Rect = DirectCast(Panel1.Controls(new_location), Rect)

                If possible_rect IsNot Nothing Then
                    possible_rect.TopSelected = True
                    possible_rect.Invalidate()
                End If
            Case Form1.Line.Left
                Dim new_location As String = (CInt(location.Substring(0, 1)) - 1).ToString & location.Substring(1, 1)
                Dim possible_rect As Rect = DirectCast(Panel1.Controls(new_location), Rect)

                If possible_rect IsNot Nothing Then
                    possible_rect.RightSelected = True
                    possible_rect.Invalidate()
                End If
        End Select
    End Sub

    Private Sub FillBox(ByVal box As Rect, ByVal col As Color)
        If box.IsFilled Then
            box.FilledColor = col

            If p1turn Then
                p1score = CShort(p1score + 1)
            Else
                p2score = CShort(p2score + 1)
            End If

            PlayerScoreLabel.Text = p1score.ToString
            ComputerScoreLabel.Text = p2score.ToString
        Else
            p1turn = Not (p1turn)
        End If

    End Sub

    Private Function CheckWin() As Boolean
        Dim win As Boolean = False
        For Each b As Rect In Panel1.Controls.OfType(Of Rect)()
            If b.IsFilled Then
                win = True
            Else
                win = False
                Exit For
            End If
        Next

        Return win
    End Function

    Private Sub RestartButton_Click(sender As System.Object, e As System.EventArgs) Handles RestartButton.Click
        Call NewGame()
    End Sub
End Class
```

----------

