lib/Games/Checkers/BoardTree.pm

filter: Statementstmt 25.8% 23 / 89 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 46.6% 7 / 15 Podpod 0.0% 0 / 3 totaltotal 21.5% 30 / 139 CC 17 SCAR 45.7
CC
17
Cov
19%
choose_best_son
210.0
unexpand
6.0
new
6.0
CRAP 96.5
« lib/Games/Checkers/Board.pm ↑ Summary lib/Games/Checkers/Game.pm »
1 # Games::Checkers, Copyright (C) 1996-2012 Mikhael Goikhman, migo@cpan.org
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 package Games::Checkers::BoardTreeNode;
20  
21 1 ▶use Games::Checkers::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Games::Checkers::MoveConstants;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Games::Checkers::CreateMoveList;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 my $stopped = No;
26 0 ▶sub check_user_interaction () {
Subroutine check_user_interaction: not called CC=1 SCAR=6.9
27 # no user interaction yet
28 0 return Ok;
29 }
30  
31 0 ▶sub new ($$$) {
Subroutine new: not called CC=1 SCAR=6.9
32 0 my $class = shift;
33 0 my $board = shift;
34 0 my $move = shift;
35  
36 0 my $self = {
37 board => $board,
38 move => $move,
39 sons => [],
40 expanded => 0,
41 };
42  
43 0 return bless $self, $class;
44 }
45  
46 # o 3 0
47 # | white max
48 # o-----------------------o 2 1
49 # | | black min
50 # o-------o-------o o-------o-------o 1 2
51 # | | | | | | white max
52 # o-o-o o-o-o o-o-o o-o-o o-o-o o-o-o 0 3
53  
54 0 ▶sub expand ($$) {
Subroutine expand: not called CC=1 SCAR=6.9
55 0 my $self = shift;
56 0 my $color = shift;
57  
58 0 my $board = $self->{board};
59 0 my $builder = Games::Checkers::CreateMoveList->new($board, $color);
60  
61 0 @{$self->{sons}} = map {
62 0 Games::Checkers::BoardTreeNode->new($_->[1], $_->[0])
63 0 } @{$builder->get_move_boards};
64 0 $self->{expanded} = 1;
65  
66 0 return $builder->{status};
67 }
68  
69 0 ▶sub unexpand ($) {
Subroutine unexpand: not called CC=2 SCAR=17.9
70 0 my $self = shift;
71  
72 0 $_->unexpand foreach @{$self->{sons}};
73 0 @{$self->{sons}} = ();
74 0 $self->{expanded} = 0;
75 }
76  
77 0 ▶sub choose_best_son ($$$;$$) {
Subroutine choose_best_son: not called CC=14 SCAR=53.5
78 0 my $self = shift;
79 0 my $color = shift;
80 0 my $level = shift;
81 0 ▶ my $min = shift; $min = MIN_SCORE unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 0 0 0
82 0 ▶ my $max = shift; $max = MAX_SCORE unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 0 0
83  
84 # return if $stopped || check_user_interaction() != Ok;
85  
86 0 ▶ my $is_maximizing = $color == ($::RULES{GIVE_AWAY} ? Black : White);
Branch: 1 branch
BranchTrueFalseTotal
$main::RULES{'GIVE_AWAY'} ? : 0 0 0
87 0 my $best_node;
88  
89 0 ▶ if ($level <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level <= 0) { } 0 0 0
90 0 $min = $max = $self->{board}->get_score($color);
91 } else {
92 0 ▶ $self->expand($color) unless $self->{expanded};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"expanded"} 0 0 0
93  
94 0 foreach my $son (@{$self->{sons}}) {
95 0 my ($score) = $son->choose_best_son(!$color, $level - 1, $min, $max);
96  
97 0 ▶ if ($is_maximizing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_maximizing) { } 0 0 0
98 0 ▶ if ($score > $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score > $min) 0 0 0
99 0 $min = $score;
100 0 $best_node = $son;
101 }
102 }
103 else {
104 0 ▶ if ($score < $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score < $max) 0 0 0
105 0 $max = $score;
106 0 $best_node = $son;
107 }
108 }
109 # alpha-beta pruning
110 0 ▶ $is_maximizing ^= 1, last if $min >= $max;
Branch: 1 branch
BranchTrueFalseTotal
if $min >= $max 0 0 0
111 }
112  
113 # all moves if any lead to losage, choose a random one
114 0 ▶ $best_node ||= $self->{sons}[rand(@{$self->{sons}})];
Condition0%0/3
$best_node ||= $self->{"sons"}[rand @{$self->{"sons"};}]
l!l&&r!l&&!r
000
MC/DC0%0/2
$best_node ||= $self->{"sons"}[rand @{$self->{"sons"};}]
$best_node $self->{"sons"}[rand @{$self->{"sons"};}]
Truth table0%0/3
A ||= B
A= $best_nodeB= $self->{"sons"}[rand @{$self->{"sons"};}]
ABresult
1X1
011
000
115  
116 0 $self->unexpand;
117 }
118  
119 0 ▶ return wantarray ? ($is_maximizing ? $min : $max) : $best_node;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
$is_maximizing ? : 0 0 0
120 }
121  
122 package Games::Checkers::BoardTree;
123  
124 1 ▶use Games::Checkers::MoveConstants;
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 1 ▶use constant DEFAULT_LEVEL => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 0 ▶sub new ($$$;$) {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
129 0 my $class = shift;
130 0 my $board = shift;
131 0 my $color = shift;
132 0 ▶ my $level = shift || DEFAULT_LEVEL;
Condition0%0/2
shift() || 5
l!l
00
MC/DC0%0/1
shift() || 5
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
133  
134 0 my $self = {
135 head => new Games::Checkers::BoardTreeNode($board, NO_MOVE),
136 max_level => $level,
137 real_level => undef,
138 color => $color,
139 };
140  
141 0 return bless $self, $class;
142 }
143  
144 0 ▶sub choose_best_move ($) {
Subroutine choose_best_move: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my $self = shift;
146  
147 0 my $max_level = $self->{max_level};
148 0 my $son = $self->{head}->choose_best_son($self->{color}, $max_level);
149 # foreach my $son0 (@{$self->{sons}}) {
150 # next if defined $son && $son == $son0;
151 # $son0->unexpand;
152 # }
153 0 ▶ return NO_MOVE unless $son;
Branch: 1 branch
BranchTrueFalseTotal
unless $son 0 0 0
154 0 return $son->{move};
155 }
156  
157 0 ▶sub choose_random_move ($) {
Subroutine choose_random_move: not called CC=1 SCAR=6.9
Pod undocumented
158 0 my $self = shift;
159  
160 0 $self->{head}->expand($self->{color});
161 0 my $sons = $self->{head}->{sons};
162 0 my $move = $sons->[int(rand(@$sons))]->{move};
163 0 $self->{head}->unexpand;
164 0 return $move;
165 }
166  
167 1 1;
« lib/Games/Checkers/Board.pm ↑ Summary lib/Games/Checkers/Game.pm »