Skip to content

Commit 08e358f

Browse files
authored
Create snake and ladder game using python
CREATING THE SNAKE AND LADDER GAME USING PYTHON.IF TWO PLAYERS WILL PLAY MANUALLY SO ANYONE CAN CHEAT OTHERS BUT IF WE DO IT THROUGH PROGRAM NO ONE CAN CHEAT WITH THIS GAME.
1 parent 84199c6 commit 08e358f

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

snake and ladder game using python

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

0 commit comments

Comments
 (0)