0% found this document useful (0 votes)
20 views3 pages

Snakes Ladders Code - Game

Uploaded by

Aviana Lethia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Snakes Ladders Code - Game

Uploaded by

Aviana Lethia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

...ion to Programming)\snakes & ladders (final)\Form1.

vb 1
1 Public Class Form1
2 Dim dice As Integer
3 Dim p1_pos As Integer = 0
4 Dim p2_pos As Integer = 0
5 Dim p1_original As Point = New Point(248, 503)
6 Dim p2_original As Point = New Point(248, 535)
7 Dim myturn As Boolean = True
8 Dim game_end As Boolean = False
9
10 Dim board() As Integer = {
11 100, 99, 98, 97, 96, 95, 94, 93, 92, 91,
12 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
13 80, 79, 78, 77, 76, 75, 74, 73, 72, 71,
14 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
15 60, 59, 58, 57, 56, 55, 54, 53, 52, 51,
16 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
17 40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
18 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
19 20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
20 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
21 }
22
23 Dim ladders() As Integer = {5, 18, 49, 74}
24 Dim ladderEnd() As Integer = {37, 61, 68, 93}
25 Dim snakes() As Integer = {98, 85, 79, 30}
26 Dim snakeEnd() As Integer = {76, 28, 42, 8}
27 Private Sub resetgame()
28 p1_pos = 0
29 p2_pos = 0
30 player1_score.Text = 0
31 player2_score.Text = 0
32 token1.Location = p1_original
33 token2.Location = p2_original
34 myturn = True
35 game_end = False
36 End Sub
37
38 Function get_row(num As Integer) As Integer
39 Dim tile_index As Integer = Array.IndexOf(board, num)
40 Return (tile_index - (tile_index Mod 10)) / 10 + 1 ' tile
index - last digit, divide by 10, +1
41
42 End Function
43 Function get_column(num As Integer) As Integer
44 Dim tile_index As Integer = Array.IndexOf(board, num)
45 Return (tile_index Mod 10) + 1
46 End Function
47
48 Function get_position(num As Integer) As Point
...ion to Programming)\snakes & ladders (final)\Form1.vb 2
49 Dim boardsize As Integer = 535
50
51 Dim tile100X As Integer = 292
52 Dim tile100Y As Integer = 36
53
54 Dim posX As Integer = boardsize / 10 * get_column(num) + tile100X
- boardsize / 10
55 Dim posY As Integer = boardsize / 10 * get_row(num) + tile100Y -
boardsize / 10
56
57 Return New Point(posX, posY)
58 End Function
59
60 Private Sub dice_roll()
61 dice = (Now.Millisecond + Int(Rnd() * 6)) Mod 6 + 1
62 Select Case dice
63 Case 1
64 pbox_dice.BackgroundImage = My.Resources.Resources.dice1
65 Case 2
66 pbox_dice.BackgroundImage = My.Resources.Resources.dice2
67 Case 3
68 pbox_dice.BackgroundImage = My.Resources.Resources.dice3
69 Case 4
70 pbox_dice.BackgroundImage = My.Resources.Resources.dice4
71 Case 5
72 pbox_dice.BackgroundImage = My.Resources.Resources.dice5
73 Case 6
74 pbox_dice.BackgroundImage = My.Resources.Resources.dice6
75 End Select
76 End Sub
77
78 Function process_snakenladders(num As Integer) As Integer
79 Dim ladder_exists As Boolean = Array.Exists(ladders, Function(x) x
= num)
80 Dim snake_exists As Boolean = Array.Exists(snakes, Function(x) x =
num)
81 Dim newpos As Integer = num ' just in case no snakes or ladders
are found
82
83 If ladder_exists Then
84 Dim ladderindex = Array.IndexOf(ladders, num)
85 newpos = ladderEnd(ladderindex)
86 End If
87
88 If snake_exists Then
89 Dim snakeindex = Array.IndexOf(snakes, num)
90 newpos = snakeEnd(snakeindex)
91 End If
92 Return newpos
...ion to Programming)\snakes & ladders (final)\Form1.vb 3
93 End Function
94
95 Private Sub winner(player As Integer)
96 game_end = True
97 MsgBox("Player " & player & " wins!")
98 End Sub
99
100 Private Sub dice_click(sender As Object, e As EventArgs) Handles
pbox_dice.Click
101 If Not game_end Then
102 dice_roll()
103 If myturn Then
104 p1_pos += dice
105 If p1_pos > 99 Then
106 p1_pos = 100
107 winner(1)
108 End If
109 p1_pos = process_snakenladders(p1_pos)
110 player1_score.Text = p1_pos
111 token1.Location = get_position(p1_pos)
112 Else
113 p2_pos += dice
114 If p2_pos > 99 Then
115 p2_pos = 100
116 winner(2)
117 End If
118 p2_pos = process_snakenladders(p2_pos)
119 player2_score.Text = p2_pos
120 token2.Location = get_position(p2_pos) + New Point(24, 24)
121 End If
122 myturn = Not myturn
123 End If
124 End Sub
125
126 Private Sub Form1_FormClosing(sender As Object, e As
FormClosingEventArgs) Handles MyBase.FormClosing
127 If e.CloseReason = CloseReason.UserClosing Then
128 Application.Exit()
129 End If
130 End Sub
131
132 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
133 resetgame()
134 End Sub
135 End Class
136

You might also like