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.

deck: Deck

The draw pile.

discard_pile: List[Card]

Cards that have been played.

current_player: int

Index of the current player.

direction: int

Turn direction (1 or -1).

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:

Card

get_winner()

Check if there’s a winner.

Returns:

Winning player index, or None if game ongoing

Return type:

Optional[int]

get_valid_plays(player_idx)

Get all valid cards a player can play.

Parameters:

player_idx – Index of the player

Returns:

List of playable cards

Return type:

List[Card]

is_valid_play(card)

Check if a card can be played on the current discard.

Parameters:

card – Card to check

Returns:

True if valid

Return type:

bool

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!")