| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Getopt::Long (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Game::Terminal; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | exit main(@ARGV) unless caller; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | my $rows = join '', map { | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | sprintf " %-11s %-6s %-4s %s\n", $_->{key}, $_->{family}, $_->{players}, $_->{blurb} | |||||||||||||||||||||||||||||||||||||||||
| 16 | 48 | } Game::Terminal::games(); | |||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | return <<"USAGE"; | |||||||||||||||||||||||||||||||||||||||||
| 18 | game - one command for every terminal game | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | usage: game the menu: choose a game, set its options, play | ||||||||||||||||||||||||||||||||||||||||||
| 21 | game <name> [options] start that game; the options are the game's own | ||||||||||||||||||||||||||||||||||||||||||
| 22 | game list every game, with its version or "not installed" | ||||||||||||||||||||||||||||||||||||||||||
| 23 | game help <name> that game's own --help | ||||||||||||||||||||||||||||||||||||||||||
| 24 | game --help | --version | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | options, before the name: | ||||||||||||||||||||||||||||||||||||||||||
| 27 | --ascii plain characters, in whatever words the game uses for that | ||||||||||||||||||||||||||||||||||||||||||
| 28 | --no-colour no colour, by setting NO_COLOR for the game | ||||||||||||||||||||||||||||||||||||||||||
| 29 | --no-pick type moves instead of choosing them with the arrow keys | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | the games: | ||||||||||||||||||||||||||||||||||||||||||
| 32 | $rows | ||||||||||||||||||||||||||||||||||||||||||
| 33 | exit status: | ||||||||||||||||||||||||||||||||||||||||||
| 34 | the game's own: 0 won, drew or quit, 1 lost, 2 a bad option | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 a bad option to game itself, or a name that is no game | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 the game died | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | A name is matched after lowercasing and dropping punctuation, and the | ||||||||||||||||||||||||||||||||||||||||||
| 39 | aliases work too: draughts, othello, ur, morris, tafl, weiqi, baduk, | ||||||||||||||||||||||||||||||||||||||||||
| 40 | chinesechess, awale, rummy, crib, 66. NO_COLOR in the environment turns | ||||||||||||||||||||||||||||||||||||||||||
| 41 | colour off everywhere. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | for my $row (Game::Terminal::games()) { | |||||||||||||||||||||||||||||||||||||||||
| 47 | 24 | my $version = Game::Terminal::version_of($row); | |||||||||||||||||||||||||||||||||||||||||
| 48 | printf "%-11s %-6s %-4s %-46s %s\n", $row->{key}, $row->{family}, $row->{players}, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 24 | ▶ | $row->{blurb}, defined $version ? "$row->{class} $version" : 'not installed'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 9 | ▶ | sub main { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
main: called CC=20 SCAR=32.8
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 9 | my @argv = @_; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | my %opt; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 9 | my $saved = Getopt::Long::Configure(qw(bundling no_ignore_case pass_through require_order)); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 9 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | local $SIG{__WARN__} = sub { print STDERR "game: $_[0]" }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 9 | Getopt::Long::GetOptionsFromArray(\@argv, \%opt, | |||||||||||||||||||||||||||||||||||||||||
| 61 | 'ascii', 'colour|color!', 'pick!', 'version|v', 'help|h'); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 9 | Getopt::Long::Configure($saved); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 9 | ▶ | if (!$ok || (@argv && $argv[0] =~ m/^-./)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $ok or @argv and $argv[0] =~ /^-./u
@argv and $argv[0] =~ /^-./u
MC/DC66%2/3
not $ok or @argv and $argv[0] =~ /^-./u
not $ok
@argv
$argv[0] =~ /^-./u
Truth table75%3/4
not(A) or B and C
A= not $okB= @argvC= $argv[0] =~ /^-./u
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | print STDERR "game: not an option: $argv[0]\n" if $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | print STDERR usage(); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 8 | ▶ | if ($opt{version}) { print "game, Game::Terminal $Game::Terminal::VERSION\n"; return 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | if ($opt{help}) { print usage(); return 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | my %common = (ascii => $opt{ascii} ? 1 : 0, colour => $opt{colour}, pick => $opt{pick}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 6 | my $word = shift @argv; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 6 | ▶ | if (!defined $word) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | require Game::Terminal::Menu; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | return Game::Terminal::Menu->new(common => \%common)->start; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 6 | ▶ | if ($word eq 'list') { return list() } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | ▶ | if ($word eq 'help') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | $word = shift @argv; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if (!defined $word) { print usage(); return 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | @argv = ('--help'); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 5 | my $row = Game::Terminal::find($word); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 5 | ▶ | if (!$row) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | my @near = Game::Terminal::nearest($word); | |||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | print STDERR "game: no such game '$word'." . (@near ? " Did you mean $near[0]?" : '') . "\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | ▶ | if (!Game::Terminal::installed($row)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | print STDERR "game: $row->{key} needs $row->{module}: cpanm $row->{module}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | my @flags = Game::Terminal::flags_for($row, \%common); | |||||||||||||||||||||||||||||||||||||||||
| 98 | my ($status, $error) = Game::Terminal::launch($row, [ @flags, @argv ], | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | ▶ | no_colour => (defined $common{colour} && !$common{colour}) ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $common{"colour"} and not $common{"colour"}
MC/DC0%0/2
defined $common{"colour"} and not $common{"colour"}
defined $common{"colour"}
not $common{"colour"}
Truth table33%1/3
A and not(B)
A= defined $common{"colour"}B= not $common{"colour"}
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | ▶ | if (defined $error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | print STDERR "game: $row->{key} died: $error\n"; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | return 3; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | ▶ | return defined $status && $status =~ m/^\d+$/ ? $status : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $status and $status =~ /^\d+$/u
MC/DC0%0/2
defined $status and $status =~ /^\d+$/u
defined $status
$status =~ /^\d+$/u
Truth table33%1/3
A and B
A= defined $statusB= $status =~ /^\d+$/u
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||||||||||||||||