write a whole code for creating the snake and ladder game
- User
write a whole code for creating the snake and ladder game
- Assistant
write a whole code for creating the snake and ladder game
Sure! Below is a complete Python implementation of the classic Snake and Ladder game. This version is a text-based, two-player game that you can run in your terminal or command prompt.
import random
class SnakeAndLadder:
def __init__(self, players_names):
self.players = {name: 0 for name in players_names}
self.snakes = {
16: 6,
47: 26,
49: 11,
56: 53,
62: 19,
64: 60,
87: 24,
93: 73,
95: 75,
98: 78
}
self.ladders = {
1: 38,
4: 14,
9: 31,
21: 42,
28: 84,
36: 44,
51: 67,
71: 91,
80: 100
}
def roll_dice(self):
return random.randint(1, 6)
def play_turn(self, player):
input(f"{player}'s turn. Press Enter to roll the dice...")
dice = self.roll_dice()
print(f"{player} rolled a {dice}")
prev_position = self.players[player]
new_position = prev_position + dice
if new_position > 100:
print(f"{player} needs to land exactly on 100 to win. Current position: {prev_position}")
return False # No move this turn
self.players[player] = new_position
print(f"{player} moved from {prev_position} to {new_position}")
# Check for snakes or ladders
if new_position in self.snakes:
final_position = self.snakes[new_position]
print(f"Oh no! {player} landed on a snake. Sliding down from {new_position} to {final_position}")
self.players[player] = final_position
elif new_position in self.ladders:
final_position = self.ladders[new_position]
print(f"Great! {player} climbed a ladder from {new_position} to {final_position}")
self.players[player] = final_position
print(f"{player} is now on square {self.players[player]}\n")
return self.players[player] == 100
def start_game(self):
print("Welcome to Snakes and Ladders!\n")
winner = None
while not winner:
for player in self.players:
if self.play_turn(player):
winner = player
break
print(f"Congratulations {winner}! You have won the game!")
if __name__ == "__main__":
# You can customize player names here
players = ["Player 1", "Player 2"]
game = SnakeAndLadder(players)
game.start_game()
Prerequisites:
Save the Code:
snake_and_ladder.py.Run the Game:
snake_and_ladder.py file.python snake_and_ladder.py
Gameplay:
Adding More Players:
You can add more players by modifying the players list in the __main__ section. For example:
players = ["Alice", "Bob", "Charlie"]
Changing Snakes and Ladders Positions:
You can modify the self.snakes and self.ladders dictionaries in the __init__ method to change where the snakes and ladders are located on the board.
Board Size: The current implementation uses a standard 100-square board. To change the board size, you'll need to adjust the winning condition and ensure that all snake and ladder positions are within the new range.
Welcome to Snakes and Ladders!
Player 1's turn. Press Enter to roll the dice...
Player 1 rolled a 4
Player 1 moved from 0 to 4
Great! Player 1 climbed a ladder from 4 to 14
Player 1 is now on square 14
Player 2's turn. Press Enter to roll the dice...
Player 2 rolled a 3
Player 2 moved from 0 to 3
Player 2 is now on square 3
Player 1's turn. Press Enter to roll the dice...
Player 1 rolled a 6
Player 1 moved from 14 to 20
Player 1 is now on square 20
...
This implementation provides a straightforward and interactive way to play Snake and Ladder in the console. Feel free to enhance it further by adding features like handling more players, adding graphics with libraries such as pygame, or implementing additional game rules.
Not yet included in a published release.