NAME

Game::Cribbage::Round - a full scoring round of cribbage

VERSION

Version 0.12

SYNOPSIS

use Game::Cribbage::Round;

my $round = Game::Cribbage::Round->new(number => 1)->init($game);

# deal cards
$round->add_player_card($player, $card);

# discard to crib
$round->crib_player_cards($player, \@discard);

# play phase
$round->play_player_card($player, 0);

# score
$round->add_starter_card($player, $starter);
$round->end_hands($game);

DESCRIPTION

Orchestrates one complete round of cribbage: dealing, crib discarding, play phase, and hand scoring. Wraps a Game::Cribbage::Hands object (the current hands cycle) and a Game::Cribbage::Round::Score tracker, and maintains a history of all hands cycles played within the round.

PROPERTIES

id

Read/write integer property for a database or external identifier.

$round->id;
$round->id(42);

number

Readonly integer property holding the round number within the game (1-based).

$round->number;

score

Read/write object property holding the Game::Cribbage::Round::Score tracking cumulative per-player scores within this round.

$round->score;

current_hands

Read/write object property holding the active Game::Cribbage::Hands cycle.

$round->current_hands;

complete

Read/write boolean property; set to 1 when the round is finished.

$round->complete;

history

Read/write arrayref of all Game::Cribbage::Hands objects created in this round, in order.

$round->history;

FUNCTIONS

init

Initialises the round: builds the per-player score structure and calls next_hands. Returns $self.

my $round = Game::Cribbage::Round->new(number => 1)->init($game);

reset_hands

Resets scores and history and begins a fresh hands cycle. Used when replaying or restoring game state.

$round->reset_hands($game);

next_hands

Shuffles the deck, creates a new Game::Cribbage::Hands, appends it to history, and sets it as current_hands. Returns $self. Accepts optional named args forwarded to the Hands constructor (e.g. crib_player).

$round->next_hands($game, crib_player => 'player2');

end_hands

Scores all hands for the current cycle, updates score, then starts the next hands cycle with the crib rotating to the next player.

$round->end_hands($game);

add_player_card_by_index

Adds $card to $player's hand at the given array index.

$round->add_player_card_by_index($index, $player, $card);

add_player_card

Appends $card to $player's hand.

$round->add_player_card($player, $card);

add_starter_card

Sets the starter card for the current hands cycle, scores nobs if applicable, and updates round scores. Returns the score object (or 0).

$round->add_starter_card($player, $card);

validate_crib_cards

Delegates to the crib player's hand to validate (and if necessary replace) their crib cards.

$round->validate_crib_cards(\@cards);

next_to_play

Returns the Game::Cribbage::Player object whose turn it is to play next.

my $player = $round->next_to_play($game);

next_to_play_id

Returns the id of the player whose turn it is to play next.

my $id = $round->next_to_play_id($game);

get_crib_player

Returns the Game::Cribbage::Player object who currently holds the crib.

my $player = $round->get_crib_player($game);

next_crib_player

Returns the player key string (e.g. 'player2') for the next crib holder, wrapping from last player back to 'player1'.

my $next = $round->next_crib_player($game);

crib_player_id

Returns the id of the crib player, or undef if not set.

my $id = $round->crib_player_id($game);

crib_player_name

Returns the name of the crib player.

my $name = $round->crib_player_name($game);

crib_player_number

Returns the seat number of the crib player.

my $num = $round->crib_player_number($game);

crib_player_cards

Discards multiple cards from $player's hand into the crib.

$round->crib_player_cards($player, \@cards);

crib_player_card

Discards a single card (by index) from $player's hand into the crib.

$round->crib_player_card($player, 0);

force_play_card

Plays a card bypassing turn-order checks, updating round scores. Returns the Game::Cribbage::Play::Score object.

my $score = $round->force_play_card($card);

play_player_card

Plays $card_index for $player with full turn-order and 31-point validation. Updates round scores on success. Returns the score or an error.

my $score = $round->play_player_card($player, 0);

card_exists

Returns 1 if $card exists in $player's current hand.

$round->card_exists($player, $card);

get_player_card

Returns the card at $card_index in $player's hand.

my $card = $round->get_player_card($player, 0);

current_play_score

Returns the running total of the current play sequence.

my $total = $round->current_play_score();

last_play_score

Returns the running total of the previous completed play sequence.

my $total = $round->last_play_score();

cannot_play_a_card

Declares that $player cannot play. Delegates to the current hands cycle.

$round->cannot_play_a_card($player);

next_play

Advances to the next play sequence (or ends hands if all cards exhausted). Returns the new active play object.

my $play = $round->next_play($game);

end_play

Ends the current play sequence, awarding the go point. Returns 1.

$round->end_play();

identify_worst_cards

Returns the worst two cards to discard from $player's 6-card hand.

my ($cards, @indexes) = $round->identify_worst_cards($player);

best_run_play

Returns the best card for $player to play, delegating to the hands cycle.

my $card = $round->best_run_play($player);

hand_play_history

Returns a hashref summarising play history:

  • used_count - total cards played in all completed play sequences

  • current_play - the active Game::Cribbage::Play object

my $hist = $round->hand_play_history();

AUTHOR

LNATION, <email at lnation.org>

BUGS

Please report any bugs or feature requests to bug-game-cribbage at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Game-Cribbage. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Game::Cribbage

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

This software is Copyright (c) 2024 by LNATION.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)