-
Notifications
You must be signed in to change notification settings - Fork 25
/
snake and ladder game using python
146 lines (142 loc) · 3.1 KB
/
snake and ladder game using python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#snake and ladder game
end=100
import random
def check_ladder(points):
if points==8:
print("ladder")
return 26
elif points==21:
print("ladder")
return 82
elif points==43:
print("ladder")
return 77
elif points==50:
print("ladder")
return 91
elif points==54:
print("ladder")
return 93
elif points==62:
print("ladder")
return 96
elif points==66:
print("ladder")
return 87
elif points==80:
print("ladder")
return 100
else:
#not a ladder
return points
def check_snake(points):
if points==44:
print("snake")
return 22
elif points==46:
print("snake")
return 5
elif points==48:
print("snake")
return 9
elif points==52:
print("snake")
return 11
elif points==55:
print("snake")
return 7
elif points==59:
print("snake")
return 17
elif points==64:
print("snake")
return 36
elif points==69:
print("snake")
return 33
elif points==73:
print("snake")
return 1
elif points==83:
print("snake")
return 19
elif points==92:
print("snake")
return 51
elif points==95:
print("snake")
return 24
elif points==98:
print("snake")
return 28
else:
#not a snake
return points
def reached_end(points):
if points ==end:
return True
else:
return False
def play():
# input player 1 name
p1_name=input("player 1,please enter your name:")
# input player 2 name
p2_name=input("player 2,please enter your name:")
#initial points of player 1
pp1=0
#initial points of player 2
pp2=0
turn=0
while(1):
if turn%2==0:
#player 1 turn
print(p1_name,"your turn")
#ask player's choice to continue
c=int(input("press 1 to continue, 0 to quit:"))
if c==0:
print(p1_name,"scored",pp1)
print(p2_name,"scored",pp2)
print("quitting the game !thanks for playing")
break
else:
#generate a random number from 1,2,3,4,5,6
dice=random.randint(1,6)
print("Dice showed:",dice)
#add points
pp1=pp1+dice
pp1=check_ladder(pp1)
pp1=check_snake(pp1)
#check if the player goes beyond the board
if pp1>end:
pp1=end
print(p1_name,"your score:",pp1)
if reached_end(pp1):
print(p1_name,"won")
break
else:
#player 2 turn
print(p2_name,"your turn")
#ask player's choice to continue
c=int(input("press 1 to continue, 0 to quit:"))
if c==0:
print(p1_name,"scored",pp1)
print(p2_name,"scored",pp2)
print("quitting the game !thanks for playing")
break
else:
#generate a random number from 1,2,3,4,5,6
dice=random.randint(1,6)
print("Dice showed:",dice)
#add points
pp2=pp2+dice
pp2=check_ladder(pp2)
pp2=check_snake(pp2)
#check if the player goes beyond the board
if pp2>end:
pp2=end
print(p2_name,"your score:",pp2)
if reached_end(pp2):
print(p2_name,"won")
break
turn=turn+1
play()