lib/Games/GUI/Battleship/AI/Hard.pm

filter: Statementstmt 92.4% 159 / 172 Branchbran 75.0% 45 / 60 Conditioncond 45.0% 9 / 20 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 4 totaltotal 82.2% 232 / 282 CC 53 SCAR 34.7
CC
53
Cov
85%
select_target
43.3
__ANON__
42.0
record_result
9.6
CRAP 32.1
« blib/lib/Games/GUI/Battleship/Renderer.pm ↑ Summary blib/lib/Games/GUI/Battleship/Board.pm »
1 package Games::GUI::Battleship::AI::Hard;
2  
3 1 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::try';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent 'Games::GUI::Battleship::AI';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Logic::Relational ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Logic::Relational::Syntax;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Logic::Relational::Goal::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $CURRENT_SHOTS = {};
15 1  
16 1 logic HardAITemplate {
17 1 fact shot_hit( 0, 0 );
18 1 fact shot_miss( 0, 0 );
19 1 fact sunk_cell( 0, 0 );
20 1 fact ship_alive( 'Carrier', 5 );
21 1 fact ship_alive( 'Battleship', 4 );
22 1 fact ship_alive( 'Cruiser', 3 );
23 1 fact ship_alive( 'Submarine', 3 );
24 1 fact ship_alive( 'Destroyer', 2 );
25 1 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 rule cell_unshot( $x, $y ) {
27 1 guard(
28 1 [ $x, $y ],
29 1 sub ( $vx, $vy ) {
30 1 return 0 if $vx < 1 || $vx > 10 || $vy < 1 || $vy > 10;
31 1 return $Games::GUI::Battleship::AI::Hard::CURRENT_SHOTS->{
32 1 "$vx,$vy"} ? 0 : 1;
33 1 }
34 1 );
35 1 }
36 1 }
37 1  
38 1 sub new ( $class, %args ) {
39 1 $args{name} = 'ProbabilityAI';
40 1 $args{difficulty} = 'hard';
41 my $self = $class->SUPER::new(%args);
42 1 $self->{surviving_ships} = {
43 Carrier => 5,
44 1 Battleship => 4,
45 1 Cruiser => 3,
46 Submarine => 3,
47 Destroyer => 2,
48 1 };
49 ▶ $self->{active_hits} = {};
Subroutine __ANON__: not called CC=6 SCAR=37.4
50 return $self;
51 }
52 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if $vx < 1 or $vx > 10 or $vy < 1 or $vy > 10 0 0 0
Condition0%0/9
$vx < 1 or $vx > 10 or $vy < 1 or $vy > 10
l!l&&r!l&&!r
000
$vx < 1 or $vx > 10 or $vy < 1
l!l&&r!l&&!r
000
$vx < 1 or $vx > 10
l!l&&r!l&&!r
000
MC/DC0%0/4
$vx < 1 or $vx > 10 or $vy < 1 or $vy > 10
$vx < 1 $vx > 10 $vy < 1 $vy > 10
Truth table0%0/5
A or B or C or D
A= $vx < 1B= $vx > 10C= $vy < 1D= $vy > 10
ABCDresult
1XXX1
01XX1
001X1
00011
00000
53 sub init_program ($self) {
54 0 ▶ $self->{program} = $self->clone_program($HardAITemplate::PROGRAM);
Branch: 1 branch
BranchTrueFalseTotal
$$Games::GUI::Battleship::AI::Hard::CURRENT_SHOTS{"$vx,$vy"} ? : 0 0 0
55 $self->{surviving_ships} = {
56 Carrier => 5,
57 1 Battleship => 4,
58 Cruiser => 3,
59 Submarine => 3,
60 Destroyer => 2,
61 };
62 $self->{active_hits} = {};
63 return $self->{program};
64 ▶}
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
65 2  
66 2 sub record_result ( $self, %args ) {
67 2 my $x = $args{x};
68 my $y = $args{y};
69 2 my $result = $args{result};
70 my $ship = $args{ship};
71  
72 $self->SUPER::record_result(%args);
73  
74 if ( $result eq 'hit' ) {
75 2 $self->{active_hits}{"$x,$y"} = 1;
76 2 }
77 elsif ( $result eq 'sunk' ) {
78 if ($ship) {
79 ▶ my $type = $ship->type;
Subroutine init_program: called CC=1 SCAR=0.0
Pod undocumented
80 2 if ( exists $self->{surviving_ships}{$type} ) {
81 my $sz = delete $self->{surviving_ships}{$type};
82 2 $self->{program}->retract( ship_alive => $type, $sz );
83 }
84 for my $coord ( $ship->coordinates ) {
85 delete $self->{active_hits}{"$coord->[0],$coord->[1]"};
86 }
87 }
88 2 else {
89 2 delete $self->{active_hits}{"$x,$y"};
90 my ($smallest) = sort {
91 $self->{surviving_ships}{$a} <=> $self->{surviving_ships}{$b}
92 ▶ } keys %{ $self->{surviving_ships} };
Subroutine record_result: called CC=7 SCAR=22.6
Pod undocumented
93 3 if ($smallest) {
94 3 my $sz = delete $self->{surviving_ships}{$smallest};
95 3 $self->{program}->retract( ship_alive => $smallest, $sz );
96 3 }
97 }
98 3 }
99  
100 3 ▶ return $self;
Branch: 2 branches
BranchTrueFalseTotal
if ($result eq 'hit') { } 2 1 3
elsif ($result eq 'sunk') { } 1 0 1
101 2 }
102  
103 sub select_target ($self) {
104 1 ▶ local $CURRENT_SHOTS = $self->{shots_history};
Branch: 1 branch
BranchTrueFalseTotal
if ($ship) { } 1 0 1
105 1  
106 1 ▶ my %surviving = %{ $self->{surviving_ships} };
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"surviving_ships"}{$type}) 1 0 1
107 1 %surviving = ( Destroyer => 2 ) unless %surviving;
108 1 my $min_len = min( values %surviving ) // 2;
109  
110 1 my @hits = keys %{ $self->{active_hits} };
111 2 my $has_hits = scalar @hits > 0 ? 1 : 0;
112  
113 my %weights;
114  
115 0 for my $type ( sort keys %surviving ) {
116 my $len = $surviving{$type};
117 0  
118 0 # Horizontal placements
119 0 ▶ for my $y ( 1 .. 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smallest) 0 0 0
120 0 for my $x ( 1 .. 11 - $len ) {
121 0 my $valid = 1;
122 my $hit_count = 0;
123 my @coords;
124  
125 for my $i ( 0 .. $len - 1 ) {
126 3 my $cx = $x + $i;
127 my $key = "$cx,$y";
128 if ( $self->{shots_history}{$key}
129 ▶ && !$self->{active_hits}{$key} )
Subroutine select_target: called CC=42 SCAR=37.7
Pod undocumented
130 4 {
131 $valid = 0;
132 4 last;
133 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
unless %surviving 0 4 4
134 4 ▶ $hit_count++ if $self->{active_hits}{$key};
Condition50%1/2
&min(values %surviving) // 2
l!l
40
MC/DC0%0/1
&min(values %surviving) // 2
&min(values %surviving)
Truth table50%1/2
A
A= &min(values %surviving)
Aresult
11
00
135 push @coords, $key;
136 4 }
137 4 ▶ next unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
scalar @hits > 0 ? : 2 2 4
138  
139 4 if ($has_hits) {
140 next if $hit_count == 0;
141 4 my $w = 10**$hit_count;
142 19 for my $k (@coords) {
143 $weights{$k} += $w unless $self->{shots_history}{$k};
144 }
145 19 }
146 190 else {
147 1430 for my $k (@coords) {
148 1430 next if $self->{shots_history}{$k};
149 1430 my ( $cx, $cy ) = split /,/x, $k;
150 my $bonus = ( ( $cx + $cy ) % $min_len == 0 ) ? 2 : 1;
151 1430 $weights{$k} += $bonus;
152 4747 }
153 4747 }
154 4747 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"shots_history"}{$key} and not $self->{"active_hits"}{$key}) 19 4728 4747
Condition100%3/3
$self->{"shots_history"}{$key} and not $self->{"active_hits"}{$key}
!ll&&!rl&&r
46775119
MC/DC100%2/2
$self->{"shots_history"}{$key} and not $self->{"active_hits"}{$key}
$self->{"shots_history"}{$key} not $self->{"active_hits"}{$key}
Truth table100%3/3
A and not(B)
A= $self->{"shots_history"}{$key}B= not $self->{"active_hits"}{$key}
ABresult
0X0
100
111
155 }
156  
157 19 # Vertical placements
158 19 for my $x ( 1 .. 10 ) {
159 for my $y ( 1 .. 11 - $len ) {
160 4728 ▶ my $valid = 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_hits"}{$key} 51 4677 4728
161 4728 my $hit_count = 0;
162 my @coords;
163 1430 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 19 1411 1430
164 for my $i ( 0 .. $len - 1 ) {
165 1411 ▶ my $cy = $y + $i;
Branch: 1 branch
BranchTrueFalseTotal
if ($has_hits) { } 760 651 1411
166 760 ▶ my $key = "$x,$cy";
Branch: 1 branch
BranchTrueFalseTotal
if $hit_count == 0 721 39 760
167 39 if ( $self->{shots_history}{$key}
168 39 && !$self->{active_hits}{$key} )
169 143 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shots_history"}{$k} 92 51 143
170 $valid = 0;
171 last;
172 }
173 651 $hit_count++ if $self->{active_hits}{$key};
174 2226 ▶ push @coords, $key;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shots_history"}{$k} 0 2226 2226
175 2226 }
176 2226 ▶ next unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
($cx + $cy) % $min_len == 0 ? : 949 1277 2226
177 2226  
178 if ($has_hits) {
179 next if $hit_count == 0;
180 my $w = 10**$hit_count;
181 for my $k (@coords) {
182 $weights{$k} += $w unless $self->{shots_history}{$k};
183 }
184 19 }
185 190 else {
186 1430 for my $k (@coords) {
187 1430 next if $self->{shots_history}{$k};
188 1430 my ( $cx, $cy ) = split /,/x, $k;
189 my $bonus = ( ( $cx + $cy ) % $min_len == 0 ) ? 2 : 1;
190 1430 $weights{$k} += $bonus;
191 4736 }
192 4736 }
193 4736 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"shots_history"}{$key} and not $self->{"active_hits"}{$key}) 30 4706 4736
Condition100%3/3
$self->{"shots_history"}{$key} and not $self->{"active_hits"}{$key}
!ll&&!rl&&r
46555130
MC/DC100%2/2
$self->{"shots_history"}{$key} and not $self->{"active_hits"}{$key}
$self->{"shots_history"}{$key} not $self->{"active_hits"}{$key}
Truth table100%3/3
A and not(B)
A= $self->{"shots_history"}{$key}B= not $self->{"active_hits"}{$key}
ABresult
0X0
100
111
194 }
195 }
196 30  
197 30 unless (%weights) {
198  
199 4706 ▶ # Fallback to any unshot cell
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_hits"}{$key} 51 4655 4706
200 4706 for my $y ( 1 .. 10 ) {
201 for my $x ( 1 .. 10 ) {
202 1430 ▶ return ( $x, $y, 'Fallback Grid Search' )
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 30 1400 1430
203 unless $self->{shots_history}{"$x,$y"};
204 1400 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if ($has_hits) { } 760 640 1400
205 760 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if $hit_count == 0 709 51 760
206 51 return;
207 51 }
208 189 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shots_history"}{$k} 138 51 189
209 my $max_w = -1;
210 for my $k ( keys %weights ) {
211 $max_w = $weights{$k} if $weights{$k} > $max_w;
212 640 }
213 2182 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shots_history"}{$k} 0 2182 2182
214 2182 my @top = grep { $weights{$_} == $max_w } keys %weights;
215 2182 ▶ my $chosen = $top[ int rand @top ];
Branch: 1 branch
BranchTrueFalseTotal
($cx + $cy) % $min_len == 0 ? : 935 1247 2182
216 2182 my ( $tx, $ty ) = split /,/x, $chosen;
217  
218 my $mode = 'Probability Density Hunt';
219 if ($has_hits) {
220 $mode =
221 ( scalar @hits >= 2 && $max_w >= 200 )
222 ? 'Target Heatmap (Line Extension)'
223 4 ▶ : 'Target Heatmap (Orthogonal Focus)';
Branch: 1 branch
BranchTrueFalseTotal
unless (%weights) 0 4 4
224 }
225  
226 0 return ( $tx, $ty, $mode );
227 0 }
228  
229 0 ▶1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shots_history"}{"$x,$y"} 0 0 0
230  
« blib/lib/Games/GUI/Battleship/Renderer.pm ↑ Summary blib/lib/Games/GUI/Battleship/Board.pm »