bin/xiangqi

filter: Statementstmt 93.4% 86 / 92 Branchbran 88.8% 32 / 36 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 84.6% 11 / 13 Podpod n/a totaltotal 90.7% 137 / 151 CC 22 SCAR 27.0
CC
22
Cov
92%
main
22.4
__ANON__
1.3
__ANON__
1.3
CRAP 14.9
« blib/lib/Game/Xiangqi/Terminal.pm ↑ Summary blib/lib/Game/Xiangqi.pm »
1 #!/usr/bin/env perl
2 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Game::Xiangqi;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Game::Xiangqi::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Game::Xiangqi::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Game::Xiangqi::Terminal;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.03';
13  
14 1 ▶exit main(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
15  
16 4 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
17 4 return <<'USAGE';
18 xiangqi - Chinese chess on a terminal
19  
20 usage: xiangqi [options]
21  
22 --level N node budget for the bot, or a rung index into the ladder
23 --red SEAT p1 or p2; whoever is Red moves first
24 --seat SEAT which seat you play (default p1)
25 --seed STR up to thirty-two bytes, for a reproducible game
26 --fen FEN start from a position instead of the opening
27 --moves LIST replay these ICCS moves first, comma or space separated
28 --wxf show WXF notation beside the coordinates
29 --ascii Latin letters instead of the characters
30 --ansi colour; --no-ansi turns it off (NO_COLOR beats both)
31 --pick choose a move with the arrow keys; --no-pick to type it
32 --ucci speak UCCI on stdin and stdout, and draw nothing
33 --help this
34  
35 exit status:
36 0 you won, or the game drew, or you quit
37 1 you lost
38 2 a bad option
39  
40 With a terminal and Term::ReadKey the arrow keys walk the pieces that have a
41 move and then that piece's points, with the caption saying what each one does.
42 Without either, or with --no-pick, a move is typed as ICCS: the from point then
43 the to point, like h2e2, and "help" lists the rest.
44  
45 The engine is bounded in NODES and never in seconds, so the same position
46 answers the same on a busy machine as on an idle one.
47 USAGE
48 }
49  
50 26 ▶sub main {
Subroutine main: called CC=22 SCAR=31.1
51 26 my @argv = @_;
52 26 my %opt = (seat => 'p1', red => 'p1');
53  
54 26 my $ok = eval {
55 26 ▶ local $SIG{__WARN__} = sub { print STDERR "xiangqi: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
56 26 GetOptionsFromArray(\@argv, \%opt,
57 'level=i', 'red=s', 'seat=s', 'seed=s', 'fen=s', 'moves=s',
58 'wxf', 'ascii', 'ansi!', 'pick!', 'ucci', 'help');
59 };
60 26 ▶ if (!$ok) { print STDERR usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 24 26
61 24 ▶ if ($opt{help}) { print usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 1 23 24
62 23 ▶ if (@argv) { print STDERR "xiangqi: not an option: $argv[0]\n", usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1 22 23
63  
64 22 for my $k (qw(red seat)) {
65 43 ▶ next if $opt{$k} eq 'p1' || $opt{$k} eq 'p2';
Branch: 1 branch
BranchTrueFalseTotal
if $opt{$k} eq "p1" or $opt{$k} eq "p2" 41 2 43
Condition66%2/3
$opt{$k} eq "p1" or $opt{$k} eq "p2"
l!l&&r!l&&!r
4102
MC/DC50%1/2
$opt{$k} eq "p1" or $opt{$k} eq "p2"
$opt{$k} eq "p1" $opt{$k} eq "p2"
Truth table66%2/3
A or B
A= $opt{$k} eq "p1"B= $opt{$k} eq "p2"
ABresult
1X1
011
000
66 2 print STDERR "xiangqi: --$k takes p1 or p2, not '$opt{$k}'\n";
67 2 return 2;
68 }
69  
70 20 my @ladder = @Game::Xiangqi::Bot::LADDER;
71 20 my $level;
72 20 ▶ if (defined $opt{level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"level"}) 7 13 20
73 7 ▶ if ($opt{level} < 0) { print STDERR "xiangqi: --level must not be negative\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} < 0) 1 6 7
74 6 ▶ if ($opt{level} >= scalar @ladder && $opt{level} < $ladder[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]) 2 4 6
Condition100%3/3
$opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]
!ll&&!rl&&r
222
MC/DC100%2/2
$opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]
$opt{"level"} >= scalar @ladder $opt{"level"} < $ladder[0]
Truth table100%3/3
A and B
A= $opt{"level"} >= scalar @ladderB= $opt{"level"} < $ladder[0]
ABresult
0X0
100
111
75 printf STDERR
76 "xiangqi: --level %d is neither a rung nor a budget: a rung is 0 to %d, "
77 . "and a budget is %d nodes or more\n",
78 2 $opt{level}, $#ladder, $ladder[0];
79 2 return 2;
80 }
81 4 ▶ $level = $opt{level} < scalar @ladder ? $ladder[ $opt{level} ] : $opt{level};
Branch: 1 branch
BranchTrueFalseTotal
$opt{'level'} < scalar @ladder ? : 2 2 4
82 }
83  
84 my $terminal = Game::Xiangqi::Terminal->new(
85 ascii => $opt{ascii},
86 wxf => $opt{wxf},
87 colour => defined $opt{ansi} ? $opt{ansi} : undef,
88 picking => $opt{pick},
89 level => $level,
90 seat => $opt{seat},
91 17 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'ansi'} ? : 4 13 17
92  
93 17 ▶ return $terminal->ucci if $opt{ucci};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"ucci"} 1 16 17
94  
95 16 ▶ my $seed = defined $opt{seed} ? $opt{seed} : 'xiangqi-terminal-default-seed32';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'seed'} ? : 0 16 16
96 16 $seed = substr($seed . ('.' x 32), 0, 32);
97  
98 16 my %new = (seed => $seed, red => $opt{red});
99 16 ▶ if (defined $opt{fen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"fen"}) 2 14 16
100 2 my ($pos, $err) = Game::Xiangqi::Engine->of_fen($opt{fen});
101 2 ▶ if (!$pos) { print STDERR "xiangqi: --fen refused, code $err\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($pos) 1 1 2
102 1 $new{position} = $pos;
103 }
104  
105 15 my $game = eval { Game::Xiangqi->new(%new) };
106 15 ▶ if (!$game) { print STDERR "xiangqi: could not start a game\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 15 15
107  
108 15 ▶ if (defined $opt{moves}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"moves"}) 2 13 15
109 2 for my $mv (grep { length } split /[\s,]+/, $opt{moves}) {
110 4 my $refusal = $game->play($mv);
111 4 ▶ next unless $refusal;
Branch: 1 branch
BranchTrueFalseTotal
unless $refusal 3 1 4
112 1 print STDERR "xiangqi: --moves refused at '$mv': ", $refusal->code, "\n";
113 1 return 2;
114 }
115 }
116  
117 14 $terminal->game($game);
118  
119 14 ▶ local $SIG{INT} = sub { $terminal->leave_raw; exit 130 };
Subroutine __ANON__: not called CC=1 SCAR=2.6
120 14 ▶ local $SIG{TERM} = sub { $terminal->leave_raw; exit 143 };
Subroutine __ANON__: not called CC=1 SCAR=2.6
121  
122 14 my $status = eval { $terminal->start };
123 14 my $died = $@;
124 14 $terminal->leave_raw;
125 14 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 14 14
126 14 return $status;
127 }
128  
129 1 1;
130  
131 __END__
« blib/lib/Game/Xiangqi/Terminal.pm ↑ Summary blib/lib/Game/Xiangqi.pm »