Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.1 kB
0
Indexable
import random

class TableTennisGame:
    def __init__(self, player1, player2, winning_score=11):
        self.player1 = player1
        self.player2 = player2
        self.winning_score = winning_score
        self.score1 = 0
        self.score2 = 0

    def play_point(self):
        if random.choice([True, False]):
            self.score1 += 1
        else:
            self.score2 += 1

    def is_game_over(self):
        return self.score1 >= self.winning_score or self.score2 >= self.winning_score

    def get_winner(self):
        if self.score1 > self.score2:
            return self.player1
        elif self.score2 > self.score1:
            return self.player2
        else:
            return "It's a tie!"

    def play_game(self):
        while not self.is_game_over():
            self.play_point()
            print(f"{self.player1}: {self.score1} - {self.player2}: {self.score2}")

        print(f"Game over! Winner: {self.get_winner()}")

# Example usage:
player1 = "Player 1"
player2 = "Player 2"

game = TableTennisGame(player1, player2)
game.play_game()
Leave a Comment