Skip to content

Commit a2e0c71

Browse files
Merge pull request #1931 from shaz12t/master
Create python.py
2 parents c6b3738 + f896c53 commit a2e0c71

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

BoardGame-CLI/python.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import random
2+
3+
# Define the game board with snakes and ladders
4+
snakes_and_ladders = {
5+
2: 38, 7: 14, 8: 31, 15: 26, 16: 6, 21: 42,
6+
28: 84, 36: 44, 46: 25, 49: 11, 51: 67, 62: 19,
7+
64: 60, 71: 91, 74: 53, 78: 98, 87: 94, 89: 68,
8+
92: 88, 95: 75, 99: 80
9+
}
10+
11+
# Function to roll a six-sided die
12+
def roll_die():
13+
return random.randint(1, 6)
14+
15+
# Function to simulate a single turn
16+
def take_turn(current_position, player_name):
17+
# Roll the die
18+
roll_result = roll_die()
19+
print(f"{player_name} rolled a {roll_result}!")
20+
21+
# Calculate the new position after the roll
22+
new_position = current_position + roll_result
23+
24+
# Check if the new position is a ladder or a snake
25+
if new_position in snakes_and_ladders:
26+
new_position = snakes_and_ladders[new_position]
27+
if new_position > current_position:
28+
print("Ladder! Climb up!")
29+
else:
30+
print("Snake! Slide down!")
31+
32+
# Check if the new position exceeds the board size
33+
if new_position >= 100:
34+
new_position = 100
35+
print(f"Congratulations, {player_name} reached the final square!")
36+
37+
return new_position
38+
39+
# Main game loop
40+
def play_snakes_and_ladders():
41+
player1_position = 1
42+
player2_position = 1
43+
44+
player1_name = input("Enter the name of Player 1: ")
45+
player2_name = input("Enter the name of Player 2: ")
46+
47+
current_player = player1_name
48+
49+
while player1_position < 100 and player2_position < 100:
50+
print(f"\n{current_player}'s turn:")
51+
input("Press Enter to roll the die.")
52+
53+
if current_player == player1_name:
54+
player1_position = take_turn(player1_position, player1_name)
55+
current_player = player2_name
56+
else:
57+
player2_position = take_turn(player2_position, player2_name)
58+
current_player = player1_name
59+
60+
print("\nGame Over!")
61+
print(f"{player1_name} ended at square {player1_position}.")
62+
print(f"{player2_name} ended at square {player2_position}.")
63+
if player1_position == 100:
64+
print(f"{player1_name} won!")
65+
elif player2_position == 100:
66+
print(f"{player2_name} won!")
67+
68+
# Start the game
69+
play_snakes_and_ladders()

0 commit comments

Comments
 (0)