Game Module
The game module provides the core UNO game logic.
UnoGame Class
- class src.game.UnoGame(num_players=2)
Main UNO game controller.
- Parameters:
num_players (int) – Number of players (2-4)
- players: List[Player]
List of player objects.
- deal_cards()
Deal 7 cards to each player.
- play_card(player_idx, card)
Execute a card play.
- Parameters:
player_idx – Index of the player
card – Card to play
- Raises:
InvalidPlayError – If the card cannot be played
- draw_card(player_idx)
Player draws a card from the deck.
- Parameters:
player_idx – Index of the player
- Returns:
The drawn card
- Return type:
- get_winner()
Check if there’s a winner.
- Returns:
Winning player index, or None if game ongoing
- Return type:
Optional[int]
Example Usage
from src.game import UnoGame
# Create a 2-player game
game = UnoGame(num_players=2)
# Deal cards
game.deal_cards()
# Get valid plays for current player
valid_cards = game.get_valid_plays(game.current_player)
# Play a card
if valid_cards:
game.play_card(game.current_player, valid_cards[0])
else:
game.draw_card(game.current_player)
# Check for winner
winner = game.get_winner()
if winner is not None:
print(f"Player {winner} wins!")