| 1 | #!perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Getopt::Long qw/GetOptions/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Game::Checkers; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Game::Checkers::Bot; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Game::Checkers::Terminal; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | my %option = ( | |||||||||||||||||||||||||||||||||||||||||
| 13 | level => [], | ||||||||||||||||||||||||||||||||||||||||||
| 14 | side => 'black', | ||||||||||||||||||||||||||||||||||||||||||
| 15 | ascii => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 16 | seed => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | GetOptions( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | \%option, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 'level=i@', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 'side=s', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 'hotseat', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 'bot-vs-bot', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 'fen=s', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 'load=s', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 'replay=s', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 'seed=i', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 'ascii', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 'colour!', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 'pick!', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 'analyse', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 'version', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 'help' | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ) or usage(2); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | usage(0) if $option{help}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | if ($option{version}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | print "checkers, Game::Checkers $Game::Checkers::VERSION\n"; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | my @level = @{$option{level}}; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | ▶ | push @level, 3 while @level < 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 4 | ▶ | die "level must be 1 to 5\n" if grep { $_ < 1 || $_ > 5 } @level; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_ < 1 || $_ > 5
MC/DC50%1/2
$_ < 1 || $_ > 5
$_ < 1
$_ > 5
Truth table66%2/3
A || B
A= $_ < 1B= $_ > 5
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | my $game = build_game(); | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | ▶ | if ($option{analyse}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | analyse($game); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | if (my $file = $option{replay}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | replay(read_pdn($file)); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | my $human = $option{hotseat} ? 'both' | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | : $option{'bot-vs-bot'} ? 'none' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | : side(); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | my $terminal = Game::Checkers::Terminal->new( | ||||||||||||||||||||||||||||||||||||||||||
| 65 | game => $game, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | human => $human, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | ($human eq 'both' ? () : ( | ||||||||||||||||||||||||||||||||||||||||||
| 68 | bot => Game::Checkers::Bot->new(level => $level[0], seed => $option{seed}) | ||||||||||||||||||||||||||||||||||||||||||
| 69 | )), | ||||||||||||||||||||||||||||||||||||||||||
| 70 | ascii => $option{ascii}, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | (defined $option{colour} ? (colour => $option{colour}) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | (defined $option{pick} ? (picking => $option{pick}) : ()) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | my $result = $terminal->start; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | exit 0 unless $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | exit 0 if $result->is_draw || $human eq 'both' || $human eq 'none'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$result->is_draw or $human eq "both" or $human eq "none"
$result->is_draw or $human eq "both"
MC/DC0%0/3
$result->is_draw or $human eq "both" or $human eq "none"
$result->is_draw
$human eq "both"
$human eq "none"
Truth table0%0/4
A or B or C
A= $result->is_drawB= $human eq "both"C= $human eq "none"
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | exit(($result->winner eq $human) ? 0 : 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub side { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
side: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $side = lc $option{side}; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return $side if $side eq 'black' || $side eq 'white'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$side eq "black" or $side eq "white"
MC/DC0%0/2
$side eq "black" or $side eq "white"
$side eq "black"
$side eq "white"
Truth table0%0/3
A or B
A= $side eq "black"B= $side eq "white"
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return int(rand 2) ? 'black' : 'white' if $side eq 'random'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | die "side must be black, white or random\n"; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | ▶ | sub build_game { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_game: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | return Game::Checkers->from_pdn(read_pdn($option{load})) if $option{load}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | return Game::Checkers->new($option{fen} ? (fen => $option{fen}) : ()); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub read_pdn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_pdn: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | open my $handle, '<', $file or die "cannot read $file: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $pdn = do { local $/; <$handle> }; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | close $handle; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | return $pdn; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | sub analyse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
analyse: called CC=4 SCAR=15.8
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | my ($game) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my $bot = Game::Checkers::Bot->new(level => $level[0], seed => $option{seed}); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | my $move = $bot->choose($game); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | unless ($move) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | print 'no move: ', ($game->result ? $game->result->stringify : 'no legal move'), "\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | my $search = $bot->last_search; | |||||||||||||||||||||||||||||||||||||||||
| 109 | printf "move %s\nscore %s\ndepth %d\nnodes %d\nline %s\n", | ||||||||||||||||||||||||||||||||||||||||||
| 110 | $move->notation, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | defined $search->{score} ? $search->{score} : 'forced', | ||||||||||||||||||||||||||||||||||||||||||
| 112 | $search->{depth}, | ||||||||||||||||||||||||||||||||||||||||||
| 113 | $search->{nodes}, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | join ' ', @{$search->{pv}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub replay { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replay: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my ($pdn) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my $whole = Game::Checkers->from_pdn($pdn); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my $game = Game::Checkers->new(fen => $whole->fen); | |||||||||||||||||||||||||||||||||||||||||
| 122 | my $terminal = Game::Checkers::Terminal->new( | ||||||||||||||||||||||||||||||||||||||||||
| 123 | game => $game, | ||||||||||||||||||||||||||||||||||||||||||
| 124 | human => 'both', | ||||||||||||||||||||||||||||||||||||||||||
| 125 | ascii => $option{ascii}, | ||||||||||||||||||||||||||||||||||||||||||
| 126 | picking => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | (defined $option{colour} ? (colour => $option{colour}) : ()) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | for my $move (@{$whole->history}) { | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $terminal->render; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | $terminal->read_line('Press enter for ' . $move->coord_notation . ' '); | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $game->move($move->notation); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $terminal->render; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | my ($status) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | print <<'USAGE'; | |||||||||||||||||||||||||||||||||||||||||
| 141 | checkers [options] | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | --level N the strength of the bot, 1 to 5, 3 by default. Give it | ||||||||||||||||||||||||||||||||||||||||||
| 144 | twice with --bot-vs-bot for one level a side. | ||||||||||||||||||||||||||||||||||||||||||
| 145 | --side SIDE black, white or random. Black moves first. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | --hotseat two people at one keyboard, no bot. | ||||||||||||||||||||||||||||||||||||||||||
| 147 | --bot-vs-bot watch two bots play. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | --fen STRING start from a position instead of the opening. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | --load FILE read a PDN game and play on from the end of it. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | --replay FILE step through a PDN game, one move a keypress. | ||||||||||||||||||||||||||||||||||||||||||
| 151 | --analyse print the bot's move for the position and stop. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | --seed N the seed the bot varies its easy levels with. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | --ascii letters instead of the draughts symbols. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | --no-colour no ANSI colour. NO_COLOR in the environment does the same. | ||||||||||||||||||||||||||||||||||||||||||
| 155 | --no-pick type the moves out instead of choosing them with the keys. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | --version, --help | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | On a terminal the legal moves are listed under the board: the up and down keys | ||||||||||||||||||||||||||||||||||||||||||
| 159 | choose one, enter plays it, and the board shows where it goes. Press ? for the | ||||||||||||||||||||||||||||||||||||||||||
| 160 | other keys, or : to type a command. | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | Typing a move works everywhere, and is all there is when the input is not a | ||||||||||||||||||||||||||||||||||||||||||
| 163 | terminal or Term::ReadKey is not installed. A move is its squares, read off the | ||||||||||||||||||||||||||||||||||||||||||
| 164 | edge of the board: a3-b4 to slide, c3xe5xg7 to jump. Type help for the commands. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | exit $status; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||