Battleship/AI/Hard.pm

filter: Statementstmt 90.4% 161 / 178 Branchbran 75.0% 45 / 60 Conditioncond 45.0% 9 / 20 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 4 totaltotal 80.9% 234 / 289 CC 53 SCAR 34.5
CC
53
Cov
83%
select_target
43.3
__ANON__
42.0
record_result
9.6
CRAP 31.7
↑ Summary 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  
26 1 ▶ rule cell_unshot( $x, $y ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 guard(
28 0 ▶ [ $x, $y ],
Subroutine AUTOLOAD: not called CC=1 SCAR=4.1
29 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 "$vx,$vy"} ? 0 : 1;
33 1 }
34 1 );
35 }
36 1 }
37 1  
38 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 1 Carrier => 5,
44 Battleship => 4,
45 1 Cruiser => 3,
46 1 Submarine => 3,
47 Destroyer => 2,
48 1 };
49 1 $self->{active_hits} = {};
50 return $self;
51 1 }
52  
53 1 sub init_program ($self) {
54 $self->{program} = $self->clone_program($HardAITemplate::PROGRAM);
55 $self->{surviving_ships} = {
56 1 Carrier => 5,
57 1 Battleship => 4,
58 Cruiser => 3,
59 Submarine => 3,
60 1 Destroyer => 2,
61 };
62 $self->{active_hits} = {};
63 0 ▶ return $self->{program};
Subroutine __ANON__: not called CC=6 SCAR=37.4
64 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
65  
66 0 ▶sub record_result ( $self, %args ) {
Branch: 1 branch
BranchTrueFalseTotal
$$Games::GUI::Battleship::AI::Hard::CURRENT_SHOTS{"$vx,$vy"} ? : 0 0 0
67 my $x = $args{x};
68 my $y = $args{y};
69 1 my $result = $args{result};
70 my $ship = $args{ship};
71  
72 $self->SUPER::record_result(%args);
73  
74 if ( $result eq 'hit' ) {
75 $self->{active_hits}{"$x,$y"} = 1;
76 2 ▶ }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
77 2 elsif ( $result eq 'sunk' ) {
78 2 if ($ship) {
79 2 my $type = $ship->type;
80 if ( exists $self->{surviving_ships}{$type} ) {
81 2 my $sz = delete $self->{surviving_ships}{$type};
82 $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 2 }
88 2 else {
89 delete $self->{active_hits}{"$x,$y"};
90 my ($smallest) = sort {
91 2 ▶ $self->{surviving_ships}{$a} <=> $self->{surviving_ships}{$b}
Subroutine init_program: called CC=1 SCAR=0.0
Pod undocumented
92 2 } keys %{ $self->{surviving_ships} };
93 if ($smallest) {
94 2 my $sz = delete $self->{surviving_ships}{$smallest};
95 $self->{program}->retract( ship_alive => $smallest, $sz );
96 }
97 }
98 }
99  
100 2 return $self;
101 2 }
102  
103 sub select_target ($self) {
104 3 ▶ local $CURRENT_SHOTS = $self->{shots_history};
Subroutine record_result: called CC=7 SCAR=22.6
Pod undocumented
105 3  
106 3 my %surviving = %{ $self->{surviving_ships} };
107 3 %surviving = ( Destroyer => 2 ) unless %surviving;
108 3 my $min_len = min( values %surviving ) // 2;
109  
110 3 my @hits = keys %{ $self->{active_hits} };
111 my $has_hits = scalar @hits > 0 ? 1 : 0;
112 3 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
if ($result eq 'hit') { } 2 1 3
elsif ($result eq 'sunk') { } 1 0 1
113 2 my %weights;
114  
115 for my $type ( sort keys %surviving ) {
116 1 ▶ my $len = $surviving{$type};
Branch: 1 branch
BranchTrueFalseTotal
if ($ship) { } 1 0 1
117 1  
118 1 ▶ # Horizontal placements
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"surviving_ships"}{$type}) 1 0 1
119 1 for my $y ( 1 .. 10 ) {
120 1 for my $x ( 1 .. 11 - $len ) {
121 my $valid = 1;
122 1 my $hit_count = 0;
123 2 my @coords;
124  
125 for my $i ( 0 .. $len - 1 ) {
126 my $cx = $x + $i;
127 0 my $key = "$cx,$y";
128 if ( $self->{shots_history}{$key}
129 0 && !$self->{active_hits}{$key} )
130 0 {
131 0 ▶ $valid = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($smallest) 0 0 0
132 0 last;
133 0 }
134 $hit_count++ if $self->{active_hits}{$key};
135 push @coords, $key;
136 }
137 next unless $valid;
138 3  
139 if ($has_hits) {
140 next if $hit_count == 0;
141 4 ▶ my $w = 10**$hit_count;
Subroutine select_target: called CC=42 SCAR=37.7
Pod undocumented
142 4 for my $k (@coords) {
143 $weights{$k} += $w unless $self->{shots_history}{$k};
144 4 }
145 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
unless %surviving 0 4 4
146 4 ▶ else {
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
147 for my $k (@coords) {
148 4 next if $self->{shots_history}{$k};
149 4 ▶ my ( $cx, $cy ) = split /,/x, $k;
Branch: 1 branch
BranchTrueFalseTotal
scalar @hits > 0 ? : 2 2 4
150 my $bonus = ( ( $cx + $cy ) % $min_len == 0 ) ? 2 : 1;
151 4 $weights{$k} += $bonus;
152 }
153 4 }
154 19 }
155 }
156  
157 19 # Vertical placements
158 190 for my $x ( 1 .. 10 ) {
159 1430 for my $y ( 1 .. 11 - $len ) {
160 1430 my $valid = 1;
161 1430 my $hit_count = 0;
162 my @coords;
163 1430  
164 4747 for my $i ( 0 .. $len - 1 ) {
165 4747 my $cy = $y + $i;
166 4747 ▶ my $key = "$x,$cy";
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
167 if ( $self->{shots_history}{$key}
168 && !$self->{active_hits}{$key} )
169 19 {
170 19 $valid = 0;
171 last;
172 4728 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_hits"}{$key} 51 4677 4728
173 4728 $hit_count++ if $self->{active_hits}{$key};
174 push @coords, $key;
175 1430 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 19 1411 1430
176 next unless $valid;
177 1411 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if ($has_hits) { } 760 651 1411
178 760 ▶ if ($has_hits) {
Branch: 1 branch
BranchTrueFalseTotal
if $hit_count == 0 721 39 760
179 39 next if $hit_count == 0;
180 39 my $w = 10**$hit_count;
181 143 ▶ for my $k (@coords) {
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shots_history"}{$k} 92 51 143
182 $weights{$k} += $w unless $self->{shots_history}{$k};
183 }
184 }
185 651 else {
186 2226 ▶ for my $k (@coords) {
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shots_history"}{$k} 0 2226 2226
187 2226 next if $self->{shots_history}{$k};
188 2226 ▶ my ( $cx, $cy ) = split /,/x, $k;
Branch: 1 branch
BranchTrueFalseTotal
($cx + $cy) % $min_len == 0 ? : 949 1277 2226
189 2226 my $bonus = ( ( $cx + $cy ) % $min_len == 0 ) ? 2 : 1;
190 $weights{$k} += $bonus;
191 }
192 }
193 }
194 }
195 }
196 19  
197 190 unless (%weights) {
198 1430  
199 1430 # Fallback to any unshot cell
200 1430 for my $y ( 1 .. 10 ) {
201 for my $x ( 1 .. 10 ) {
202 1430 return ( $x, $y, 'Fallback Grid Search' )
203 4736 unless $self->{shots_history}{"$x,$y"};
204 4736 }
205 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
206 return;
207 }
208 30  
209 30 my $max_w = -1;
210 for my $k ( keys %weights ) {
211 4706 ▶ $max_w = $weights{$k} if $weights{$k} > $max_w;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_hits"}{$key} 51 4655 4706
212 4706 }
213  
214 1430 ▶ my @top = grep { $weights{$_} == $max_w } keys %weights;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 30 1400 1430
215 my $chosen = $top[ int rand @top ];
216 1400 ▶ my ( $tx, $ty ) = split /,/x, $chosen;
Branch: 1 branch
BranchTrueFalseTotal
if ($has_hits) { } 760 640 1400
217 760 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if $hit_count == 0 709 51 760
218 51 my $mode = 'Probability Density Hunt';
219 51 if ($has_hits) {
220 189 ▶ $mode =
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shots_history"}{$k} 138 51 189
221 ( scalar @hits >= 2 && $max_w >= 200 )
222 ? 'Target Heatmap (Line Extension)'
223 : 'Target Heatmap (Orthogonal Focus)';
224 640 }
225 2182 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shots_history"}{$k} 0 2182 2182
226 2182 return ( $tx, $ty, $mode );
227 2182 ▶}
Branch: 1 branch
BranchTrueFalseTotal
($cx + $cy) % $min_len == 0 ? : 935 1247 2182
228 2182  
229 1;
230  
↑ Summary Battleship/Board.pm »