create a snake and ladder game
- User
create a snake and ladder game
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
create a snake and ladder game
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll help you create a Snake and Ladders game in Python. I'll provide a complete implementation with classes and game logic:
import random
class SnakeAndLadders:
def __init__(self):
# Game board setup
self.board_size = 100
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
}
self.players = []
def add_players(self, num_players):
"""Initialize players with starting position 0"""
self.players = [0] * num_players
def roll_dice(self):
"""Simulate dice roll"""
return random.randint(1, 6)
def move_player(self, player_index, steps):
"""Move player and handle snakes and ladders"""
current_pos = self.players[player_index]
new_pos = current_pos + steps
# Check if player exceeds board size
if new_pos > 100:
return current_pos
# Check for snakes
if new_pos in self.snakes:
new_pos = self.snakes[new_pos]
print(f"Oops! Snake at {new_pos}")
# Check for ladders
if new_pos in self.ladders:
new_pos = self.ladders[new_pos]
print(f"Yay! Ladder to {new_pos}")
self.players[player_index] = new_pos
return new_pos
def play_game(self):
"""Main game loop"""
self.add_players(2) # Two players
current_player = 0
while True:
input(f"Player {current_player + 1}'s turn. Press Enter to roll dice.")
dice_roll = self.roll_dice()
print(f"Dice roll: {dice_roll}")
new_position = self.move_player(current_player, dice_roll)
print(f"Player {current_player + 1} moves to {new_position}")
# Check for winning condition
if new_position == 100:
print(f"Player {current_player + 1} wins!")
break
# Switch players
current_player = (current_player + 1) % len(self.players)
# Run the game
if __name__ == "__main__":
game = SnakeAndLadders()
game.play_game()
This implementation includes several key features:
Board Setup:
Game Mechanics:
Gameplay:
How to Play:
Customization Options:
snakes and ladders dictionaries to change board layoutadd_players() to support more playersWould you like me to explain any part of the code or make any specific modifications?