Untitled
unknown
plain_text
9 months ago
2.4 kB
5
Indexable
import random
class Player:
def __init__(self, name):
self.name = name
self.position = 0 # Position on the board (0 means not started)
self.finished = False
class LudoGame:
WINNING_POSITION = 25 # Simple board with 25 positions
def __init__(self, players):
self.players = [Player(name) for name in players]
self.current_player_index = 0
def roll_dice(self):
return random.randint(1, 6)
def move_player(self, player):
if player.finished:
print(f"{player.name} has already finished!")
return
dice_value = self.roll_dice()
print(f"{player.name} rolled a {dice_value}")
# Start moving only if player is out of the starting position
if player.position == 0 and dice_value != 6:
print(f"{player.name} needs a 6 to start. Stay at the start.")
return
if player.position == 0 and dice_value == 6:
player.position = 1
print(f"{player.name} starts moving!")
return
# Move player and check for the win condition
new_position = player.position + dice_value
if new_position > LudoGame.WINNING_POSITION:
print(f"{player.name} rolled too high and stays at {player.position}.")
else:
player.position = new_position
print(f"{player.name} moves to {player.position}.")
# Check for win
if player.position == LudoGame.WINNING_POSITION:
player.finished = True
print(f"Congratulations! {player.name} has won the game!")
def play_turn(self):
player = self.players[self.current_player_index]
print(f"\n{player.name}'s turn!")
self.move_player(player)
# Advance to the next player
self.current_player_index = (self.current_player_index + 1) % len(self.players)
def is_game_over(self):
# Game ends when at least one player wins
return any(player.finished for player in self.players)
def start_game(self):
print("Starting Ludo Game!")
while not self.is_game_over():
self.play_turn()
print("Game Over!")
if __name__ == "__main__":
player_names = input("Enter player names separated by commas: ").split(',')
game = LudoGame(player_names)
game.start_game()
Editor is loading...
Leave a Comment