Need to write a vba program to make the game Chutes and Ladders in excel Snakes
ID: 3605838 • Letter: N
Question
Need to write a vba program to make the game Chutes and Ladders in excel
Snakes and ladders is the original version of the game “Chutes and Ladders”. This game consists of a square board made up of 100 squares (ten squares by ten squares). To play, players roll one or more dice and advance their token the rolled number of spaces. Arrival at the end of the board – the hundredth square is the ultimate goal. In order to make the game interesting, there are snakes and ladders along the way. If a player’s token lands on the lower-numbered end of a “ladder”, the player moves the token up to the ladder’s higher-numbered square. If the player lands on the higher-numbered square of a “snake” (or chute), the token is moved down to the snake’s lower-number square. Players take alternate turns to roll the die.
Write a VBA Sub Program to create the snakes and ladders game for two players using one die. The player who reaches the 100th square first wins the game. Below are the rules that you need to keep in mind while building this game.
a. Create a 10 X 10 board and place the snakes and ladders as shown in the next page.
b. Player 1 will start the game by rolling one die, which is a randomly generated number between 1 and 6 when the button “Player 1” is clicked. He/She should advance his/her token the rolled number of spaces. Similarly, player 2 takes his/her turn when the button “Player 2” is clicked. Players should alternate their turns.
c. If a player’s token lands on the lower-numbered end of a ladder, the player moves the token up to the ladder’s higher-numbered square. If the player’s token lands on the highernumbered square of a snake, the token is moved down to the snake’s lower-numbered square.
d. The player who first reaches the 100th square is declared the winner.
f. Create a sub program for Player 1 and Player 2 separately.
g. Create a sub program to restart the game anytime when the button named “Restart” is clicked.
The blue arrow represents the ladders, and the orange arrow represents the snakes. If a player’s token lands on 18, the player should move up his/her token to 44. If a player’s token lands on 32, the player should move his/her token down to 11.
12 13 93 100 | 99| 98 | 97 Player 1 93 78 63 58 92 79 62 59 73 68 53 player 2 Restart 23 13 9 20 10Explanation / Answer
Option Base 1
Dim c(10) As Variant
Dim r(10) As Variant
Dim x As Integer
Dim m As Integer
Dim n As Integer
Dim num As Integer
Dim totalnum As Single
Dim totalnum1 As Single
Dim player As Integer
Dim t As Integer
Private Sub Command2_Click()
'To move the pieces to the original position
Image1(0).Move 10200, 5520
Image1(1).Move 10200, 6480
totalnum = 0
totalnum1 = 0
Label2.Caption = ""
MMControl1.Command = "close"
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
' To assign the column and row coordinates to all the boxes
c(1) = 600
r(1) = 8200
For i = 1 To 9
c(i + 1) = c(i) + 800
Next
For j = 1 To 9
r(j + 1) = r(j) - 800
Next
End Sub
'To initiate the rolling of dice
Private Sub roll()
x = x + 10
Randomize Timer
n = Int(1 + Rnd * 6)
For i = 0 To 6
Shape1(i).Visible = False
Next
If n = 1 Then
Shape1(3).Visible = True
Shape2.FillColor = &HC0C0C0
End If
If n = 2 Then
Shape1(2).Visible = True
Shape1(4).Visible = True
Shape2.FillColor = &H8080FF
End If
If n = 3 Then
Shape1(2).Visible = True
Shape1(3).Visible = True
Shape1(4).Visible = True
Shape2.FillColor = &H80FF&
End If
If n = 4 Then
Shape1(0).Visible = True
Shape1(2).Visible = True
Shape1(4).Visible = True
Shape1(6).Visible = True
Shape2.FillColor = &HFFFF00
End If
If n = 5 Then
Shape1(0).Visible = True
Shape1(2).Visible = True
Shape1(3).Visible = True
Shape1(4).Visible = True
Shape1(6).Visible = True
Shape2.FillColor = &HFFFF&
End If
If n = 6 Then
Shape1(0).Visible = True
Shape1(1).Visible = True
Shape1(2).Visible = True
Shape1(4).Visible = True
Shape1(5).Visible = True
Shape1(6).Visible = True
Shape2.FillColor = &HFF00FF
End If
End Sub
Private Sub Command1_Click(Index As Integer)
'To indentify which player click the roll dice command
If Index = 0 Then
player = 1
End If
If Index = 1 Then
player = 2
End If
Timer1.Enabled = True
x = 0
End Sub
Private Sub Timer1_Timer()
If x < 100 Then
Call roll
Else
Timer1.Enabled = False
'To check the number on the dice
If Shape1(3).Visible = True Then
num = 1
End If
If (Shape1(2).Visible = True) And (Shape1(4).Visible = True) Then
num = 2
End If
If (Shape1(2).Visible = True) And (Shape1(3).Visible = True) And (Shape1(4).Visible = True) Then
num = 3
End If
If (Shape1(0).Visible = True) And (Shape1(2).Visible = True) And (Shape1(4).Visible = True) And (Shape1(6).Visible = True) Then
num = 4
End If
If (Shape1(0).Visible = True) And (Shape1(2).Visible = True) And (Shape1(3).Visible = True) And (Shape1(4).Visible = True) And (Shape1(6).Visible = True) Then
num = 5
End If
If (Shape1(0).Visible = True) And (Shape1(1).Visible = True) And (Shape1(2).Visible = True) And (Shape1(4).Visible = True) And (Shape1(5).Visible = True) Then
num = 6
End If
'To move player 1 according to the total score of the dice
'Movement across colum1 to column 10 and row 1 to row 10
If player = 1 Then
totalnum = totalnum + num
If totalnum < 11 Then
Image1(0).Move c(totalnum), r(1)
If totalnum = 10 Then
Image1(0).Move c(8), r(3)
totalnum = 28
End If
End If
If totalnum > 10 And totalnum < 21 Then
Image1(0).Move c(21 - totalnum), r(2)
If totalnum = 17 Then
Image1(0).Move c(4), r(4)
totalnum = 37
End If
End If
If totalnum > 20 And totalnum < 31 Then
Image1(0).Move c(totalnum - 20), r(3)
End If
If totalnum > 30 And totalnum < 41 Then
Image1(0).Move c(41 - totalnum), r(4)
If totalnum = 34 Then
Image1(0).Move c(5), r(2)
totalnum = 16
End If
If totalnum = 31 Then
Image1(0).Move c(10), r(7)
totalnum = 70
End If
End If
If totalnum > 40 And totalnum < 51 Then
Image1(0).Move c(totalnum - 40), r(5)
If totalnum = 45 Then
Image1(0).Move c(4), r(9)
totalnum = 84
End If
If totalnum = 44 Then
Image1(0).Move c(1), r(3)
totalnum = 21
End If
End If
If totalnum > 50 And totalnum < 61 Then
Image1(0).Move c(61 - totalnum), r(6)
End If
If totalnum > 60 And totalnum < 71 Then
Image1(0).Move c(totalnum - 60), r(7)
If totalnum = 68 Then
Image1(0).Move c(8), r(5)
totalnum = 48
End If
End If
If totalnum > 70 And totalnum < 81 Then
Image1(0).Move c(81 - totalnum), r(8)
If totalnum = 79 Then
Image1(0).Move c(2), r(6)
totalnum = 59
End If
If totalnum = 78 Then
Image1(0).Move c(4), r(10)
totalnum = 97
End If
End If
If totalnum > 80 And totalnum < 91 Then
Image1(0).Move c(totalnum - 80), r(9)
End If
If totalnum > 90 And totalnum < 101 Then
Image1(0).Move c(101 - totalnum), r(10)
If totalnum = 95 Then
Image1(0).Move c(8), r(8)
totalnum = 73
End If
End If
If totalnum > 100 Or totalnum = 100 Then
Image1(0).Move c(1), r(10)
End If
End If
'To move player 2 according to the total score of the dice
If player = 2 Then
totalnum1 = totalnum1 + num
If totalnum1 < 11 Then
Image1(1).Move c(totalnum1), r(1)
If totalnum1 = 10 Then
Image1(1).Move c(8), r(3)
totalnum1 = 28
End If
End If
If totalnum1 > 10 And totalnum1 < 21 Then
Image1(1).Move c(21 - totalnum1), r(2)
If totalnum1 = 17 Then
Image1(1).Move c(4), r(4)
totalnum1 = 37
End If
End If
If totalnum1 > 20 And totalnum1 < 31 Then
Image1(1).Move c(totalnum1 - 20), r(3)
End If
If totalnum1 > 30 And totalnum1 < 41 Then
Image1(1).Move c(41 - totalnum1), r(4)
If totalnum1 = 34 Then
Image1(1).Move c(5), r(2)
totalnum1 = 16
End If
If totalnum1 = 31 Then
Image1(1).Move c(10), r(7)
totalnum1 = 70
End If
End If
If totalnum1 > 40 And totalnum1 < 51 Then
Image1(1).Move c(totalnum1 - 40), r(5)
If totalnum1 = 45 Then
Image1(1).Move c(4), r(9)
totalnum1 = 84
End If
If totalnum1 = 44 Then
Image1(1).Move c(1), r(3)
totalnum1 = 21
End If
End If
If totalnum1 > 50 And totalnum1 < 61 Then
Image1(1).Move c(61 - totalnum1), r(6)
End If
If totalnum1 > 60 And totalnum1 < 71 Then
Image1(1).Move c(totalnum1 - 60), r(7)
If totalnum1 = 68 Then
Image1(1).Move c(8), r(5)
totalnum1 = 48
End If
End If
If totalnum1 > 70 And totalnum1 < 81 Then
Image1(1).Move c(81 - totalnum1), r(8)
If totalnum1 = 79 Then
Image1(1).Move c(2), r(6)
totalnum1 = 59
End If
If totalnum1 = 78 Then
Image1(1).Move c(4), r(10)
totalnum1 = 97
End If
End If
If totalnum1 > 80 And totalnum1 < 91 Then
Image1(1).Move c(totalnum1 - 80), r(9)
End If
If totalnum1 > 90 And totalnum1 < 101 Then
Image1(1).Move c(101 - totalnum1), r(10)
If totalnum1 = 95 Then
Image1(1).Move c(8), r(8)
totalnum1 = 73
End If
End If
If totalnum1 > 100 Or totalnum1 = 100 Then
Image1(1).Move c(1), r(10)
End If
End If
'To play the applause sound when any one player reach 100
If (totalnum > 100 Or totalnum = 100) And totalnum1 < 100 Then
Label2.Caption = "Player 1 Wins"
MMControl1.Notify = False
MMControl1.Wait = True
MMControl1.Shareable = False
MMControl1.DeviceType = "WaveAudio"
MMControl1.FileName = "D:Liew FolderVB programudiopplause.wav"
MMControl1.Command = "Open"
MMControl1.Command = "Play"
End If
If (totalnum1 > 100 Or totalnum1 = 100) And totalnum < 100 Then
Label2.Caption = "Player 2 Wins"
MMControl1.Notify = False
MMControl1.Wait = True
MMControl1.Shareable = False
MMControl1.DeviceType = "WaveAudio"
MMControl1.FileName = "D:Liew FolderVB programudiopplause.wav"
MMControl1.Command = "Open"
MMControl1.Command = "Play"
End If
End If
End Sub
Private Sub Timer2_Timer()
delay
If t < 1000 Then
Else
Timer2.Enabled = False
End If
End Sub
Sub delay()
t = t + 1
End Sub
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.