Snake and Ladder Game Code


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 chess 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 Folder\VB program\audio\applause.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 Folder\VB program\audio\applause.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

Back to Snake and Ladder Game

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net

[Privacy Policy]