Creating a Video Slot game with VB.NET Part 2 – Gameplay

Introduction

Welcome back! With this installment we will mainly concentrate in determining whether or not three pictures form part of a winning line. We will also enable the facility to enter an amount of credits, and track them during gameplay.

How Does it Work?

A line is basically the row in which the matching pictures were found. It doesn’t have to be a straight line, it can be any variant. If you take 5 columns with 4 pictures each, you can have many possibilities, as explained in the first installment of this series. We need to determine which pictures are next to each other. Let us start with the ‘complicated’ stuff first… for a change!

Finding Matches

Create the following variables inside General Declarations :

    Private LineCounter As Integer 'Count winning lines ( rows )
    Private SelLines As Integer 'How many lines were selected
    Private MainLineCounter As Integer 'Count winning MAIN Lines - which can be skew, straight, zig-zag etc.
    Private Credits As Integer 'How many credits
    Private LineBet As Integer 'How much to bet per line
    Private Bet As Integer 'Total bet
    Private strBetSummary As String 'summarise lines + bet per line
    Private LineBetCounter As Integer = 0 'Limit bet per line

Now add the following sub procedure :

    Private Sub CheckMainLines() 'Determine winning sequence for MAIN lines

        For i As Integer = 0 To 9 'loop through all images in arrImages array
            'Line 1
            '0	0	0	0	0
            If Column1(0).Image Is arrImages(i) And Column2(0).Image Is arrImages(i) And _
                Column3(0).Image Is arrImages(i) And Column4(0).Image Is arrImages(i) And _
                Column5(0).Image Is arrImages(i) Then 'Check if neighbouring pictures are the same
                MessageBox.Show("Line 1 Match") 'Match - More matches are possible, but that will be done in Part 3
                MainLineCounter += 1 'Increment MainLine counter - to indicate how many main lines have been struck
                'Line 2
                '1	1	1	1	1
            ElseIf Column1(1).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) And _
                Column3(1).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) And _
                Column5(1).Image Is arrImages(i) Then
                MessageBox.Show("Line 2 Match")
                MainLineCounter += 1
                'Line 3
                '2	2	2	2	2
            ElseIf Column1(2).Image Is arrImages(i) And Column2(2).Image Is arrImages(i) And _
                Column3(2).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) _
                And Column5(2).Image Is arrImages(i) Then
                MessageBox.Show("Line 3 Match")
                MainLineCounter += 1
                'Line 4
                '3	3	3	3	3
            ElseIf Column1(3).Image Is arrImages(i) And Column2(3).Image Is arrImages(i) And _
                Column3(3).Image Is arrImages(i) And Column4(3).Image Is arrImages(i) And _
                Column5(3).Image Is arrImages(i) Then
                MessageBox.Show("Line 4 Match")
                MainLineCounter += 1
                'Line 5
                '3	2	1	2	3
            ElseIf Column1(3).Image Is arrImages(i) And Column2(2).Image Is arrImages(i) And _
                Column3(1).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) And _
                Column5(3).Image Is arrImages(i) Then
                MessageBox.Show("Line 5 Match")
                MainLineCounter += 1
                'Line 6
                '0	1	2	1	0
            ElseIf Column1(0).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) And _
                Column3(2).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) And _
                Column5(0).Image Is arrImages(i) Then
                MessageBox.Show("Line 6 Match")
                MainLineCounter += 1
                'Line 7
                '1	0	1	0	1
            ElseIf Column1(1).Image Is arrImages(i) And Column2(0).Image Is arrImages(i) And _
                Column3(1).Image Is arrImages(i) And Column4(0).Image Is arrImages(i) And _
                Column5(1).Image Is arrImages(i) Then
                MessageBox.Show("Line 7 Match")
                MainLineCounter += 1
                'Line 8
                '2	3	2	3	2
            ElseIf Column1(2).Image Is arrImages(i) And Column2(3).Image Is arrImages(i) And _
                Column3(2).Image Is arrImages(i) And Column4(3).Image Is arrImages(i) And _
                Column5(2).Image Is arrImages(i) Then
                MessageBox.Show("Line 8 Match")
                MainLineCounter += 1
                'Line 9
                '2	1	1	1	2
            ElseIf Column1(2).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) And _
                Column3(1).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) And _
                Column5(2).Image Is arrImages(i) Then
                MessageBox.Show("Line 9 Match")
                MainLineCounter += 1

            End If
        Next

    End Sub

There are hundreds of possible matches with our screen of twenty pictures, remember that. Now, with this sub, I am checking if all the matches for one full line have been found. In other words, I check to see if the pictures displayed correspond precisely with my built in lines. How? Let us take a look at how the columns and rows are formed. The pictures are organized according to Figure 1

How Pictures are organized
Figure 1How Pictures are organized

Obviously arrays start counting at 0, so technically our pictures start at 0 and go on to 19. The lines look as follows :

How our lines are organized
Figure 2How our lines are organized

As you can see, the first four Main lines are straightforward in the fact that all of them have the same index in each different PictureBox array ( Column1 – Column 5 ). From line 5 onwards, I tried to create different patterns for the lines to follow. In real Video slot games, there are many more combinations, but this is just an example of how the logic works.

This sub deals with the Main lines. I call them Main lines, because they are worth the most. A full line needs to be identified in order for the line to win. In the sub I compare the pictures with a little keyword called Is. Remember in Part 1 I declared arrImages as Image? Well, it is because of this. It makes it easier to compare pictures this way, and you get the correct result each time; instead of using pixel detection and so forth, which may give unexpected results. We will work more with the Main Lines in Part 3 again. When a match is found, I increment the MainLineCounter variable to keep track of how many lines were struck.

The next two subs use the same principle. I just take it a step further to detect all possible matches. Add the CheckCols and CheckRows subs :

    Private Sub CheckCols() 'Determine winning sequence for columns

        For i As Integer = 0 To 9 'Loop through arrImages
            If Column1(0).Image Is arrImages(i) And Column1(1).Image Is arrImages(i) _
                And Column1(2).Image Is arrImages(i) Then 'Determine three matching neighbouring images
                MessageBox.Show("column 1 Match 1") 'Match - Two matches are possible
                'No Column counter yet, just concetrating on the lines Now - Completed with Part 3
            ElseIf Column1(1).Image Is arrImages(i) And Column1(2).Image Is arrImages(i) _
                And Column1(3).Image Is arrImages(i) Then
                MessageBox.Show("column 1 Match 2")

            ElseIf Column2(0).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) _
                And Column2(2).Image Is arrImages(i) Then
                MessageBox.Show("column 2 Match 1")

            ElseIf Column2(1).Image Is arrImages(i) And Column2(2).Image Is arrImages(i) _
                And Column2(3).Image Is arrImages(i) Then
                MessageBox.Show("column 2 Match 2")

            ElseIf Column3(0).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) _
                And Column3(2).Image Is arrImages(i) Then
                MessageBox.Show("column 3 Match 1")

            ElseIf Column3(1).Image Is arrImages(i) And Column3(2).Image Is arrImages(i) _
                And Column3(3).Image Is arrImages(i) Then
                MessageBox.Show("column 3 Match 2")

            ElseIf Column4(0).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) _
                And Column4(2).Image Is arrImages(i) Then
                MessageBox.Show("column 4 Match 1")

            ElseIf Column4(1).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) _
                And Column4(3).Image Is arrImages(i) Then
                MessageBox.Show("column 4 Match 2")

            ElseIf Column5(0).Image Is arrImages(i) And Column5(1).Image Is arrImages(i) _
                And Column5(2).Image Is arrImages(i) Then
                MessageBox.Show("column 5 Match 1")

            ElseIf Column5(1).Image Is arrImages(i) And Column5(2).Image Is arrImages(i) _
                And Column5(3).Image Is arrImages(i) Then
                MessageBox.Show("column 5 Match 2")

            End If
        Next

    End Sub

    Private Sub CheckRows() 'Determine winning sequence for lines ( straight rows )
        For i As Integer = 0 To 9 'Loop arrImages

            If Column1(0).Image Is arrImages(i) And Column2(0).Image Is arrImages(i) And _
                Column3(0).Image Is arrImages(i) Then 'Determine similarity between neighbouring pictures
                MessageBox.Show("row 1 Match 1") 'Three matches are possible
                LineCounter += 1 ' Count each match distinctly
            ElseIf Column2(0).Image Is arrImages(i) And Column3(0).Image Is arrImages(i) _
                And Column4(0).Image Is arrImages(i) Then
                MessageBox.Show("row 1 Match 2")
                LineCounter += 1
            ElseIf Column3(0).Image Is arrImages(i) And Column4(0).Image Is arrImages(i) _
                And Column5(0).Image Is arrImages(i) Then
                MessageBox.Show("row 1 Match 3")
                LineCounter += 1
            ElseIf Column1(1).Image Is arrImages(i) And Column2(1).Image Is arrImages(i) _
                And Column3(1).Image Is arrImages(i) Then
                MessageBox.Show("row 2 Match 1")
                LineCounter += 1
            ElseIf Column2(1).Image Is arrImages(i) And Column3(1).Image Is arrImages(i) _
                And Column4(1).Image Is arrImages(i) Then
                MessageBox.Show("row 2 Match 2")
                LineCounter += 1
            ElseIf Column3(1).Image Is arrImages(i) And Column4(1).Image Is arrImages(i) _
                And Column5(1).Image Is arrImages(i) Then
                MessageBox.Show("row 2 Match 3")
                LineCounter += 1
            ElseIf Column1(2).Image Is arrImages(i) And Column2(2).Image Is arrImages(i) _
                And Column3(2).Image Is arrImages(i) Then
                MessageBox.Show("row 3 Match 1")
                LineCounter += 1
            ElseIf Column2(2).Image Is arrImages(i) And Column3(2).Image Is arrImages(i) _
                And Column4(2).Image Is arrImages(i) Then
                MessageBox.Show("row 3 Match 2")
                LineCounter += 1
            ElseIf Column3(2).Image Is arrImages(i) And Column4(2).Image Is arrImages(i) _
                And Column5(2).Image Is arrImages(i) Then
                MessageBox.Show("row 3 Match 3")
                LineCounter += 1
            ElseIf Column1(3).Image Is arrImages(i) And Column2(3).Image Is arrImages(i) _
                And Column3(3).Image Is arrImages(i) Then
                MessageBox.Show("row 4 Match 1")
                LineCounter += 1
            ElseIf Column2(3).Image Is arrImages(i) And Column3(3).Image Is arrImages(i) _
                And Column4(3).Image Is arrImages(i) Then
                MessageBox.Show("row 4 Match 2")
                LineCounter += 1
            ElseIf Column3(3).Image Is arrImages(i) And Column4(3).Image Is arrImages(i) _
                And Column5(3).Image Is arrImages(i) Then
                MessageBox.Show("row 4 Match 3")
                LineCounter += 1
            End If
        Next

    End Sub

We are now able to detect all matches. When a macth is found, a MessageBox will show, and the associated counter will increment. In Part 3, the MessageBoxes will be removed. Add the call to these three subs to tmrPause_Tick, which will now look like :

    Private Sub tmrPause_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrPause.Tick
        ''''''''''''''''''''''
        ''Part 1
        If Col <= 5 Then
            Col += 1
            TimeCounter = 0
            tmrPause.Enabled = False
            tmrSlots.Enabled = True
        Else
            Col = 1
            TimeCounter = 0
            tmrPause.Enabled = False
            tmrSlots.Enabled = False
            picSpin.Enabled = True

            Panel5.BackColor = Color.White
            picSpin.Image = My.Resources.SPIN
            '''''''''''''''''''''''''''''

            CheckMainLines() 'Determine all matches
            CheckCols()
            CheckRows()

        End If

    End Sub
Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read