forked from Py-Contributors/awesomeScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_and_ladder.py
194 lines (159 loc) · 4.88 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import time
import random
import sys
# Just for effects. Add a delay of 1 second before performing any action
SLEEP_BETWEEN_ACTIONS = 1
MAX_VAL = 100
DICE_FACE = 6
# snake takes you down from 'key' to 'value'
snakes = {
8: 4,
18: 1,
26: 10,
39: 5,
51: 6,
54: 36,
56: 1,
60: 23,
75: 28,
83: 45,
85: 59,
90: 48,
92: 25,
97: 87,
99: 63,
}
# ladder takes you up from 'key' to 'value'
ladders = {
3: 20,
6: 14,
11: 28,
15: 34,
17: 74,
22: 37,
38: 59,
49: 67,
57: 76,
61: 78,
73: 86,
81: 98,
88: 91,
}
player_turn_text = [
"Your turn.",
"Go.",
"Please proceed.",
"Lets win this.",
"Are you ready?",
"",
]
snake_bite = ["boohoo", "bummer", "snake bite", "oh no", "dang"]
ladder_jump = ["woohoo", "woww", "nailed it", "oh my God...", "yaayyy"]
def welcome_msg():
msg = """
Rules:
1. Initally both the players are at starting position i.e. 0.
Take it in turns to roll the dice.
Move forward the number of spaces shown on the dice.
2. If you lands at the bottom of a ladder, you can move up to the top of the ladder.
3. If you lands on the head of a snake, you must slide down to the bottom of the snake.
4. The first player to get to the FINAL position is the winner.
5. Hit enter to roll the dice.
"""
print(msg)
def get_player_names():
player1_name = None
while not player1_name:
player1_name = input("Please enter a valid name for first player: ").strip()
player2_name = None
while not player2_name:
player2_name = input("Please enter a valid name for second player: ").strip()
print()
print("Match will be played between")
print(player1_name)
print("AND")
print(player2_name)
return player1_name, player2_name
def get_dice_value():
time.sleep(SLEEP_BETWEEN_ACTIONS)
dice_value = random.randint(1, DICE_FACE)
print("Its a " + str(dice_value))
return dice_value
def got_snake_bite(old_value, current_value, player_name):
print("\n" + random.choice(snake_bite).upper() + " ~~~~~~~~>")
print()
print(player_name + " got a snake bite")
print("Down from")
print(old_value)
print("to")
print(current_value)
def got_ladder_jump(old_value, current_value, player_name):
print("\n" + random.choice(ladder_jump).upper() + " ########")
print()
print(player_name)
print(" climbed the ladder from ")
print(old_value)
print("to")
print(current_value)
def snake_ladder(player_name, current_value, dice_value):
time.sleep(SLEEP_BETWEEN_ACTIONS)
old_value = current_value
current_value = current_value + dice_value
if current_value > MAX_VAL:
print(
"You need " + str(MAX_VAL - old_value) + " to win this game. Keep trying."
)
return old_value
print()
print(player_name)
print("moved from")
print(old_value)
print("to")
print(current_value)
if current_value in snakes:
final_value = snakes.get(current_value)
got_snake_bite(current_value, final_value, player_name)
elif current_value in ladders:
final_value = ladders.get(current_value)
got_ladder_jump(current_value, final_value, player_name)
else:
final_value = current_value
return final_value
def check_win(player_name, position):
time.sleep(SLEEP_BETWEEN_ACTIONS)
if MAX_VAL == position:
print("\n\n\nThats it.\n\n" + player_name + " won the game.")
print("Congratulations " + player_name)
print("Thank you for playing the game.")
sys.exit(1)
def start():
welcome_msg()
time.sleep(SLEEP_BETWEEN_ACTIONS)
player1_name, player2_name = get_player_names()
time.sleep(SLEEP_BETWEEN_ACTIONS)
player1_current_position = 0
player2_current_position = 0
while True:
time.sleep(SLEEP_BETWEEN_ACTIONS)
inp_str = "\n" + player1_name + ": " + random.choice(player_turn_text)
_ = input(inp_str + " Hit the enter to roll dice: ")
print("\nRolling dice...")
dice_value = get_dice_value()
time.sleep(SLEEP_BETWEEN_ACTIONS)
print(player1_name + " moving....")
player1_current_position = snake_ladder(
player1_name, player1_current_position, dice_value
)
check_win(player1_name, player1_current_position)
inp_prompt = "\n" + player2_name + ": " + random.choice(player_turn_text)
_ = input(inp_prompt + " Hit the enter to roll dice: ")
print("\nRolling dice...")
dice_value = get_dice_value()
time.sleep(SLEEP_BETWEEN_ACTIONS)
print(player2_name + " moving....")
player2_current_position = snake_ladder(
player2_name, player2_current_position, dice_value
)
check_win(player2_name, player2_current_position)
if __name__ == "__main__":
start()