-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake_And_Ladder.py
125 lines (101 loc) · 3.44 KB
/
Snake_And_Ladder.py
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
#Snake and Ladder Game
import random
snakes = {
17:7,54:34,62:19,98:79}
ladders = {
3:38,24:33,42:93,72:84}
max_value = 100
dice_face = 6
def welcome_message():
print("###### Welcocme to Snakes & Ladders Game ######")
def get_players_name():
player1_name = input("Enter the name of Player1: ")
player2_name = input("Enter the name of Player2: ")
def get_dice_value():
dice_value= random.randint(1,dice_face)
return dice_value
def snake_ladder(current_value, dice_value):
old_value = current_value
current_value = current_value + dice_value
if current_value == max_value:
return current_value
if current_value > max_value:
print(" Your final position is "+old_value)
return old_value
if current_value in snakes:
final_value = snakes.get(current_value)
elif current_value in ladders:
final_value = ladders.get(current_value)
else:
final_value = current_value
print(" Your final position is {}".format(final_value))
return final_value
def check_win(player_name, position):
if max_value == position:
print(player_name + " won the game")
print("###### Game Succesfully Finished ######")
return 1
else:
return 0
def start():
welcome_message()
get_players_name()
player1_current_position = 0
player2_current_position = 0
print("###### Let us Start ######")
while True:
quits = 0
while True:
input_1 = input("Player 1: ")
if input_1 == "quit":
print("Player 2 won the game")
print("###### Game Succesfully Finished ######")
quits = 1
break
if input_1=="roll":
dice_value = get_dice_value()
break
else:
if input_1.isdecimal():
if int(input_1)>=1 and int(input_1)<=20:
dice_value = int(input_1)
break
else:
print("Invalid Input. Try Again!")
else:
print("Invalid Input. Try Again!")
if quits==1:
break
print("You got a {}".format(dice_value))
player1_current_position = snake_ladder(player1_current_position, dice_value)
check = check_win("Player 1", player1_current_position)
if check==1:
break
while True:
input_2 = input("Player 2: ")
if input_2 == "quit":
print("Player 1 won the game")
print("###### Game Succesfully Finished ######")
quits = 1
break
if input_2=="roll":
dice_value = get_dice_value()
break
else:
if input_2.isdecimal():
if int(input_2)>=1 and int(input_2)<=20:
dice_value = int(input_2)
break
else:
print("Invalid Input. Try Again!")
else:
print("Invalid Input. Try Again!")
if quits==1:
break
print("You got a {}".format(dice_value))
player2_current_position = snake_ladder(player2_current_position, dice_value)
check = check_win("Player 2", player2_current_position)
if check==1:
break
if __name__ == "__main__":
start()