Excel VBA Game Ladders and Snakes

Summary

Ladders and Snakes Game Board

This fun little project was part of a contest to program the game Ladders and Snakes using Excel. The game is also known as Chutes and Ladders (I guess the more child friendly version for those afraid of snakes). The object was to implement a multi-player game that could have children and/or BOTs players. The game is totally self-contained in Excel with external image files.

Implementation

The game was programmed using Visual Basic for Application (VBA). Images were collected for the board, game pieces, and die sides (6). The board image was sized such that each game square match the worksheet cell size. So each game piece location was a cell.

There are a number of board images available, so I also included an up/down worksheet that identified the starting and ending board position for squares with a ladder or snake. Board positions were 1 (beginning) to 100 (end). Each board position (1-100) was converted into an Excel row and column location using a function. A defined type was used to manage the data. The following code translated the board location into an Excel row and column. Note the use of the offsetcell variable. This allowed the board square (upper left) to be placed in any symmetrical row/column (e.g. B2/2) to allow for game image boarders.

' type for player's piece row and column
Private Type rowcol
    row As Integer
    column As Integer
End Type

' convert board square to Excel row/col
Private Function getrowcol(space As Integer) As rowcol  ' for position return cell location
    offsetcell = 1  ' row and column offset from A1 based on board picture and location
    Select Case space
        Case 1 To 10
            getrowcol.row = 10 + offsetcell
            getrowcol.column = (space - 1) Mod 10 + 1 + offsetcell
        
        Case 11 To 20
            getrowcol.row = 9 + offsetcell
            getrowcol.column = 10 - ((space - 1) Mod 10) + offsetcell

        Case 21 To 30
            getrowcol.row = 8 + offsetcell
            getrowcol.column = (space - 1) Mod 10 + 1 + offsetcell
        
        Case 31 To 40
            getrowcol.row = 7 + offsetcell
            getrowcol.column = 10 - ((space - 1) Mod 10) + offsetcell
        
        Case 41 To 50
            getrowcol.row = 6 + offsetcell
            getrowcol.column = (space - 1) Mod 10 + 1 + offsetcell
        
        Case 51 To 60
            getrowcol.row = 5 + offsetcell
            getrowcol.column = 10 - ((space - 1) Mod 10) + offsetcell
        
        Case 61 To 70
            getrowcol.row = 4 + offsetcell
            getrowcol.column = (space - 1) Mod 10 + 1 + offsetcell
        
        Case 71 To 80
            getrowcol.row = 3 + offsetcell
            getrowcol.column = 10 - ((space - 1) Mod 10) + offsetcell
        
        Case 81 To 90
            getrowcol.row = 2 + offsetcell
            getrowcol.column = (space - 1) Mod 10 + 1 + offsetcell
        
        Case 91 To 100
            getrowcol.row = 1 + offsetcell
            getrowcol.column = 10 - ((space - 1) Mod 10) + offsetcell
        End Select

End Function

Each time a player finished their turn a check was performed to see if board square they occupied was a ladder (up) or a snake (down). If the board position changed the player’s position, a new board square was returned. The up/down worksheet list was in sorted, ascending order.

Private Function checkladder(space As Integer) As Integer   ' check postion for change
    checkladder = space                                     ' due to ladder or snake
    ' use worksheet from-to
    ' supports changing the board easily
    Set ws = ThisWorkbook.Sheets("UpDown")
    ' search from for this position
    i = 2
    Do While (ws.Cells(i, 1) <> "")         ' end of list
        If ws.Cells(i, 1) = space Then      ' match
            checkladder = ws.Cells(i, 2)    ' return to
            Exit Do ' found exit
        End If
        If ws.Cells(i, 1) > space Then      ' values are greater
            Exit Do                         ' so exit early
        End If
        i = i + 1
    Loop
End Function

A defined type was also implemented for the player. Items to track included their name, where they are on the board, their piece image, if the player is a BOT, and where in the cell to place their piece image (pixel offset).

' player type
Private Type player
    name As String          ' name
    position As Integer     ' position 1 - 100
    color As String         ' player piece image file
    BOT As Boolean          ' is BOT
    cellxoffset As Integer  ' x position in cell
    cellyoffset As Integer  ' y position in cell
End Type

Die rolling was done using random numbers and the die image rotated during the roll. Game piece movement also showed moving the player’s game piece through each board square including any ladder or snake movement.

Play continue sequentially between players until some reached the end and won the game. When this happen a winner’s banner was displayed.

Overall this was a fun project. I hadn’t manipulated images on an Excel worksheet before, so it was worth the effort.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s