NAME
Game::Cribbage::Play - a single play sequence (cards up to 31)
VERSION
Version 0.12
SYNOPSIS
use Game::Cribbage::Play;
my $play = Game::Cribbage::Play->new(next_to_play => 'player1');
# play a card and get back a Play::Score object
my $score = $play->card('player1', $card);
print $score->score; # points earned
# test without committing
my $points = $play->test_card('player2', $card);
# end the sequence and award the go point
my $go_score = $play->end_play();
DESCRIPTION
Manages one play sequence in cribbage - the consecutive playing of cards until the running total reaches 31 or no player can play. Cards from all players are added in turn-order; scoring (pairs, runs, fifteen, 31) is evaluated after each card.
PROPERTIES
id
Read/write scalar property for a database or external identifier.
$play->id;
$play->id($id);
next_to_play
Read/write string property holding the player key (e.g. 'player1') whose turn it is to play.
$play->next_to_play;
$play->next_to_play('player2');
total
Read/write integer property holding the current running total of pip values. Defaults to 0.
$play->total;
cards
Read/write arrayref of Game::Cribbage::Play::Card objects representing the cards played so far in this sequence.
$play->cards;
scored
Read/write arrayref of Game::Cribbage::Play::Score objects accumulated during this play sequence.
$play->scored;
FUNCTIONS
test_card
Tests whether playing $card for $player would be legal and, if so, returns the numeric score the play would earn. Returns a Game::Cribbage::Error with go set if the card would take the total over 31. Does not commit the card to the play.
my $result = $play->test_card($player, $card);
if (ref $result) {
# Error - cannot play
} else {
# $result is the score (0 or more)
}
card
Plays $card for $player, updating the running total and checking for pairs, runs, fifteen, and 31. Returns a Game::Cribbage::Play::Score object on success, or a Game::Cribbage::Error if the total would exceed 31.
my $score = $play->card($player, $card);
force_card
Plays $card for $player unconditionally unless an identical card is already in the play sequence (duplicate guard). Returns 0 if the card was already played, otherwise delegates to card.
$play->force_card($player, $card);
end_play
Awards the go point to the last player who played a card and records it in scored. Returns the resulting Game::Cribbage::Play::Score, or undef if no cards have been played.
my $score = $play->end_play();
calculate_pair
Internal method. Inspects the tail of cards to detect pairs, three of a kind, or four of a kind, and sets pair on $score.
$play->calculate_pair($score, $card);
calculate_run
Internal method. Inspects the last N cards to detect runs of three or more consecutive run-values, and sets run on $score.
$play->calculate_run($score, $card);
calculate_total
Sums the pip values of $cards and optionally stores the result in total when $set is true. Returns the computed total.
my $total = $play->calculate_total(\@cards, 1);
calculate_hits
Sets fifteen or pegged/go on $score when $total equals 15 or 31 respectively.
$play->calculate_hits($score, $total);
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:
RT: CPAN's request tracker (report bugs here)
Search CPAN
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)