Skip to content

Commit

Permalink
new __init__; new add_new_player
Browse files Browse the repository at this point in the history
  • Loading branch information
siemieniuk committed Dec 9, 2023
1 parent 534d5aa commit e033713
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions backend/src/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ def to_dict(self):


class Game:
def __init__(self, num_players: int, name: str) -> None:
def __init__(
self, max_players: int, name: str, password: str | None = None
) -> None:
assert max_players <= 6 and max_players >= 2
self.players: list[Player] = []
self.max_players: int = num_players
self.max_players: int = max_players
self.player_counter: int = 0
self.status: GameStatus = GameStatus.NOT_STARTED
self.name: str = name
self.password: str | None = password
self._remaining_cards: PriorityQueue = PriorityQueue()

def move(self, card: Card, action: list[str]) -> bool:
Expand Down Expand Up @@ -240,11 +244,15 @@ def switch_current_player(self) -> None:
)
break

def add_new_player(self, name: str) -> None:
def add_new_player(self, name: str, password: str) -> None:
if password != self.password:
raise ValueError("Password does not match")
if self.status in [GameStatus.STARTED, GameStatus.TERMINATED]:
raise ValueError("Game has been started or was terminated")
if self.does_player_exist(name):
raise ValueError(f"Player with identifier={name} already exists")
if len(self.players) == self.max_players:
raise ValueError("Tried to exceed maximum number of players")

player = Player(name)
self.players.append(player)
Expand Down

0 comments on commit e033713

Please sign in to comment.