Board Game Simulation

Write a Python class Game that simulates a simple board game with players, scores, and the ability to move players on the board. The class should provide methods to add players, move players, and display the current state of the game.

Example 1:

Input: add_player("Alice"), move_player("Alice", 5), display_board()
Output: {"Alice": 5}

Example 2:

Input: add_player("Alice"), add_player("Bob"), move_player("Alice", 5), move_player("Bob", 3), display_board()
Output: {"Alice": 5, "Bob": 3}

Use a dictionary to keep track of players and their positions on the board.

class Game:
    def __init__(self):
        self.board = {}

    def add_player(self, name):
        self.board[name] = 0

    def move_player(self, name, positions):
        self.board[name] += positions

    def display_board(self):
        return self.board

# Example usage
game = Game()
game.add_player("Alice")
game.move_player("Alice", 5)
print(game.display_board())  # Output: {"Alice": 5}
game.add_player("Bob")
game.move_player("Bob", 3)
print(game.display_board())  # Output: {"Alice": 5, "Bob": 3}

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!