| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Game::Durak::Bot; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Game::Durak::Terminal; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | my %opt = (level => 3, seat => 1); | |||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | GetOptions(\%opt, 'level=i', 'seat=i', 'seed=s', 'ascii', 'colour!', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 'paint!', 'pick!', 'width=i', 'height=i', 'help') | ||||||||||||||||||||||||||||||||||||||||||
| 14 | or usage(2); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | usage(0) if $opt{help}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | my $levels = Game::Durak::Bot->ladder; | |||||||||||||||||||||||||||||||||||||||||
| 19 | bad("--seat must be 1 or 2, not '$opt{seat}'") | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | unless $opt{seat} =~ /\A[12]\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | bad("--level must be 1 to $levels, not '$opt{level}'") | ||||||||||||||||||||||||||||||||||||||||||
| 22 | unless $opt{level} =~ /\A[0-9]+\z/ | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | && $opt{level} >= 1 && $opt{level} <= $levels; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$opt{"level"} =~ /\A[0-9]+\z/ and $opt{"level"} >= 1 and $opt{"level"} <= $levels
$opt{"level"} =~ /\A[0-9]+\z/ and $opt{"level"} >= 1
MC/DC66%2/3
$opt{"level"} =~ /\A[0-9]+\z/ and $opt{"level"} >= 1 and $opt{"level"} <= $levels
$opt{"level"} =~ /\A[0-9]+\z/
$opt{"level"} >= 1
$opt{"level"} <= $levels
Truth table75%3/4
A and B and C
A= $opt{"level"} =~ /\A[0-9]+\z/B= $opt{"level"} >= 1C= $opt{"level"} <= $levels
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub bad { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bad: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | my ($why) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | print {\*STDERR} "durak: $why\n"; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | exit 2; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my ($status) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my $fh = $status ? \*STDERR : \*STDOUT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | print {$fh} <<"USAGE"; | |||||||||||||||||||||||||||||||||||||||||
| 35 | usage: $0 [--level 1-5] [--seat 1|2] [--seed STRING] [--ascii] [--no-colour] | ||||||||||||||||||||||||||||||||||||||||||
| 36 | [--no-paint] [--width N] [--height N] | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | A deal of Podkidnoy Durak against the bot. You are the seat given by --seat | ||||||||||||||||||||||||||||||||||||||||||
| 39 | and the bot plays the other one. The seed decides the deal, and the same seed | ||||||||||||||||||||||||||||||||||||||||||
| 40 | deals the same cards every time. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | The table is drawn on one painted screen at a terminal and printed as plain | ||||||||||||||||||||||||||||||||||||||||||
| 43 | lines when the output is a pipe or a file. --paint and --no-paint say so | ||||||||||||||||||||||||||||||||||||||||||
| 44 | either way, and --width and --height fit the screen to a window that is not | ||||||||||||||||||||||||||||||||||||||||||
| 45 | eighty by twenty-four. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | At a terminal the arrow keys walk the cards you may play and the line above | ||||||||||||||||||||||||||||||||||||||||||
| 48 | the table says what the one under the cursor would do: whether they must beat | ||||||||||||||||||||||||||||||||||||||||||
| 49 | it, which attack it beats, whether it is a trump, and how much room the bout | ||||||||||||||||||||||||||||||||||||||||||
| 50 | has left. Enter plays it. The moves that are not cards keep their own keys. | ||||||||||||||||||||||||||||||||||||||||||
| 51 | Needs Term::ReadKey; --no-pick, or no terminal, numbers them at a prompt | ||||||||||||||||||||||||||||||||||||||||||
| 52 | instead. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | At the prompt, a number plays that card. t takes, d is done, x exchanges the | ||||||||||||||||||||||||||||||||||||||||||
| 55 | trump, r resigns and q (or quit) stops. | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | Exit status is 0 if you won or the deal was stopped, 1 if you were the fool, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 for an option the game will not take. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | exit $status; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | my $said = defined $opt{seed} ? $opt{seed} | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | : join q{}, map { chr 97 + int rand 26 } 1 .. 32; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | my $seed = sprintf q{%-32.32s}, $said; | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | my $terminal = Game::Durak::Terminal->new( | ||||||||||||||||||||||||||||||||||||||||||
| 68 | seed => $seed, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | level => $opt{level}, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | seat => $opt{seat}, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ascii => $opt{ascii}, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | colour => $opt{colour}, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | paint => $opt{paint}, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | width => $opt{width}, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | height => $opt{height}, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | (defined $opt{pick} ? (picking => $opt{pick} ? 1 : 0) : ()), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | for my $signal (qw/ INT TERM HUP /) { | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $SIG{$signal} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $terminal->leave_raw; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $SIG{ $_[0] } = 'DEFAULT'; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | kill $_[0], $$; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | my $result = $terminal->run; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | exit 0 unless $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $terminal->say(''); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $terminal->say("the seed was $said"); | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | exit 1 if ref $result eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | && defined $result->{fool} && $result->{fool} == $opt{seat}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
ref $result eq "HASH" and defined $result->{"fool"} and $result->{"fool"} == $opt{"seat"}
ref $result eq "HASH" and defined $result->{"fool"}
MC/DC0%0/3
ref $result eq "HASH" and defined $result->{"fool"} and $result->{"fool"} == $opt{"seat"}
ref $result eq "HASH"
defined $result->{"fool"}
$result->{"fool"} == $opt{"seat"}
Truth table0%0/4
A and B and C
A= ref $result eq "HASH"B= defined $result->{"fool"}C= $result->{"fool"} == $opt{"seat"}
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||||||||||||||||