Visual Basic .NET - Monopoly Board

A concept on how a monopoly board may work in code.

Getting Started

This is going to get lengthy.

Value Description
0 N/A
1 Property
2 Utility
3 Railroad
4 Community Chest
5 Chance
6 Free Parking

The list of variables and arrays can go on and on. An example would be using constants of widths and heights of squares, how many houses/hotels are currently in play, or even dice variables/logs. The purpose of this post is to give you an idea on how you can create just a monopoly board.

Module

Rather than creating huge arrays, we can program a module and refer to landing when we need to in the future code.

Module landing
    Public propid As Integer = -1
    Class Prop
        Public Property id As Integer = propid
        Public ReadOnly Property getID() As Integer
            Get
                Return id
            End Get
        End Property
        Public WriteOnly Property setID() As Integer
            Set(ByVal value As Integer)
                id = value
            End Set
        End Property
        Public name As String
        Public type As Integer
        Public owned As Integer
        Public description As String
        Public price As Integer
        Public rent(0 To 5) As Integer
        Public houses As Integer
        Public housecost As Decimal
        Public mortgage As Integer
    End Class
End Module

Setting up Properties

Since this was a project that I was doing for fun, Let's just setup the arrays anyways. This will give us an idea on what they look like. We'll still use the module above and load the data into it.

Here are some array sample codes

    Dim cities() As String = {"Go","Mediterranean Avenue","Community Chest","Baltic Avenue","Income Tax","Reading Railroad","Oriental Avenue","Chance","Vermont Avenue","Connecticut",
                                 "In Jail / Just Visiting","St. Charles Place","Electric Company","States Ave.","Virginia Ave.","Pennsylvania R.R.","St. James Place","Community Chest","Tennessee Ave.","New York Ave.","Free Parking",
                                 "Kentucky Ave.","Chance","Indiana Avenue","Illinois","B. & O. Railroad","Atlantic Avenue","Ventnor Avenue","Water Works","Marvin Gardens","Go To Jail",
                                 "Pacific Avenue","North Carolina Avenue","Community Chest","Pennsylvania Avenue","Short Line","Chance","Park Place","Luxury Tax","Boardwalk"}
    Dim cityColors() As Color = {Color.White,Color.Purple,Color.White,Color.Purple,Color.White,Color.Black,Color.AliceBlue,Color.White,Color.AliceBlue,Color.AliceBlue,
                                 Color.White,Color.Pink,Color.White,Color.Pink,Color.Pink,Color.Black,Color.Orange,Color.White,Color.Orange,Color.Orange,Color.White,
                                 Color.Red,Color.White,Color.Red,Color.Red,Color.Black,Color.Yellow,Color.Yellow,Color.White,Color.Yellow,Color.White,
                                 Color.Green,Color.Green,Color.White,Color.Green,Color.Black,Color.White,Color.Blue,Color.White,Color.Blue}
    Dim type() As Integer = {0, 1, 4, 1, 2, 3, 1, 5, 1, 1,
                             8, 1, 2, 1, 1, 3, 1, 4, 1, 1,
                             6, 1, 5, 1, 1, 3, 1, 1, 2, 1,
                             7, 1, 1, 4, 1, 3, 5, 1, 2, 1}

The big loop

The code below is the majority of the project. It isn't structured the best but it's a nice breakdown. Theres lots of comments to explain what's happening.

        For ctr As Integer = 0 To cities.Length() - 1
            landing.propid += 1

            'Create new object of Monopoly Property
            Dim prop As Prop = New Prop()

            'Name of the Monopoly Property
            prop.name = cities(ctr)

            'Description of the MP
            If (prop.name = "Go") Then
                prop.description = "Collect $200"
            ElseIf prop.name = "Income Tax" Then
                prop.description = "Pay 10% or $200"
            Else
                prop.description = ""
            End If

            'Types of the MP
            prop.type = type(ctr)

            'Owned By Player (Initialized)
            prop.owned = ownedBy(ctr)

            'Prices of the MP
            prop.price = prices(ctr)

            'Rents of the MP
            prop.rent(0) = rent0(ctr)
            prop.rent(1) = rent1(ctr)
            prop.rent(2) = rent2(ctr)
            prop.rent(3) = rent3(ctr)
            prop.rent(4) = rent4(ctr)
            prop.rent(5) = rent5(ctr)

            'Amount of houses that a player owns on this property
            prop.houses = 0 'Default Zero

            'Cost of each green house for the property
            prop.housecost = housecost(ctr)

            'Mortgage price of a property when bankrupted
            prop.mortgage = (prices(ctr) / 2)


            'Add Labels
            Dim lbl As New Label()
            Dim price As New Label()
            Dim pcolor As New Label()

            lbl.Size = New System.Drawing.Size(70, 70)
            price.Size = New System.Drawing.Size(68, 20)
            pcolor.Size = New System.Drawing.Size(68, 10)
            'Turn Board
            ''Change the tiles directions to:
            ''1. Up
            ''2. Left
            ''3. Down
            ''4. Right
            ''Each tile will be drawn in the appropriate location based on
            ''the real monopoly board and the x and y coordinates.
            If (prop.id > 30) Then
                tilesY -= 70
            ElseIf (prop.id > 20) Then
                tilesX -= 70
            ElseIf (prop.id > 10) Then
                tilesY += 70
            Else
                tilesX += 70
            End If

            lbl.Location = New System.Drawing.Point(tilesX, tilesY)
            price.Location = New System.Drawing.Point(tilesX + 1, tilesY + 49)
            pcolor.Location = New System.Drawing.Point(tilesX + 1, tilesY + 1)

            lbl.BackColor = Color.White
            lbl.ForeColor = Color.Black
            lbl.BorderStyle = BorderStyle.FixedSingle
            lbl.TextAlign = ContentAlignment.MiddleCenter

            price.BackColor = Color.White
            price.ForeColor = Color.DarkGray
            price.TextAlign = ContentAlignment.MiddleCenter

            pcolor.BackColor = cityColors(ctr)
            pcolor.BorderStyle = BorderStyle.None

            lbl.Text = "" & prop.name
            If (prop.type = 1) Then
                price.Text = "Price $" & prop.price
            Else
                price.Text += prop.description
                price.BackColor = Color.White
                price.BorderStyle = BorderStyle.None
            End If

            Me.Controls.Add(price)
            Me.Controls.Add(pcolor)
            Me.Controls.Add(lbl)

            Dim Properties As New ListViewItem(prop.name)
            loadDataInListTable(Properties, prop)
        Next

Picture of the board

MonopolyBoard