Battleship/AI/Naive.pm

filter: Statementstmt 96.8% 124 / 128 Branchbran 75.0% 3 / 4 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 3 totaltotal 87.5% 148 / 169 CC 7 SCAR 11.8
CC
7
Cov
92%
__ANON__
9.7
select_target
2.0
AUTOLOAD
1.5
CRAP 3.3
« Battleship/AI.pm ↑ Summary Battleship/AI/Parity.pm »
1 package Games::GUI::Battleship::AI::Naive;
2  
3 2 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶no warnings 'experimental::try';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use parent 'Games::GUI::Battleship::AI';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Logic::Relational ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Logic::Relational::Syntax;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Logic::Relational::Goal::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $CURRENT_SHOTS = {};
14 2  
15 2 logic NaiveAITemplate {
16 2 fact shot_hit( 0, 0 );
17 2 fact shot_miss( 0, 0 );
18 2 fact sunk_cell( 0, 0 );
19 2  
20 2 rule cell_unshot( $x, $y ) {
21 2 guard(
22 2 [ $x, $y ],
23 2 sub ( $vx, $vy ) {
24 2 return 0 if $vx < 1 || $vx > 10 || $vy < 1 || $vy > 10;
25 2 ▶ return $Games::GUI::Battleship::AI::Naive::CURRENT_SHOTS->{
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 "$vx,$vy"} ? 0 : 1;
27 0 ▶ }
Subroutine AUTOLOAD: not called CC=1 SCAR=4.1
28 );
29 2 }
30 2  
31 rule cell_valid( $x, $y ) {
32 2 between( 1, 10, $y );
33 2 between( 1, 10, $x );
34 cell_unshot( $x, $y );
35 2 }
36  
37 2 rule adjacent( $x1, $y1, $x2, $y2 ) {
38 is_goal( $x2, [$x1], sub ($v) { $v + 1 } );
39 unify( $y1, $y2 );
40 2 }
41 2 rule adjacent( $x1, $y1, $x2, $y2 ) {
42 is_goal( $x2, [$x1], sub ($v) { $v - 1 } );
43 unify( $y1, $y2 );
44 2 }
45 rule adjacent( $x1, $y1, $x2, $y2 ) {
46 is_goal( $y2, [$y1], sub ($v) { $v + 1 } );
47 4 ▶ unify( $x1, $x2 );
Subroutine __ANON__: called CC=6 SCAR=22.7
48 4 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if $vx < 1 or $vx > 10 or $vy < 1 or $vy > 10 0 4 4
Condition33%3/9
$vx < 1 or $vx > 10 or $vy < 1 or $vy > 10
l!l&&r!l&&!r
004
$vx < 1 or $vx > 10 or $vy < 1
l!l&&r!l&&!r
004
$vx < 1 or $vx > 10
l!l&&r!l&&!r
004
MC/DC0%0/4
$vx < 1 or $vx > 10 or $vy < 1 or $vy > 10
$vx < 1 $vx > 10 $vy < 1 $vy > 10
Truth table20%1/5
A or B or C or D
A= $vx < 1B= $vx > 10C= $vy < 1D= $vy > 10
ABCDresult
1XXX1
01XX1
001X1
00011
00000
49 rule adjacent( $x1, $y1, $x2, $y2 ) {
50 4 ▶ is_goal( $y2, [$y1], sub ($v) { $v - 1 } );
Branch: 1 branch
BranchTrueFalseTotal
$$Games::GUI::Battleship::AI::Naive::CURRENT_SHOTS{"$vx,$vy"} ? : 1 3 4
51 unify( $x1, $x2 );
52 }
53 2  
54 # Priority 1: Basic Adjacent Probe (No Line Extension)
55 rule select_target( $x, $y, $mode ) {
56 fresh my ( $hx, $hy );
57 2 shot_hit( $hx, $hy );
58 adjacent( $hx, $hy, $x, $y );
59 cell_valid( $x, $y );
60 2 unify( $mode, 'Basic Adjacent Probe' );
61 2 }
62  
63 # Priority 2: Linear Sequential Scan
64 2 rule select_target( $x, $y, $mode ) {
65 2 between( 1, 10, $y );
66 between( 1, 10, $x );
67 cell_unshot( $x, $y );
68 unify( $mode, 'Linear Sequential Scan' );
69 }
70 }
71  
72 sub new ( $class, %args ) {
73 $args{name} = 'NaiveAI';
74 2 $args{difficulty} = 'easy';
75 return $class->SUPER::new(%args);
76 }
77 2  
78 2 sub init_program ($self) {
79 2 $self->{program} = $self->clone_program($NaiveAITemplate::PROGRAM);
80 2 return $self->{program};
81 }
82  
83 2 sub select_target ($self) {
84 local $CURRENT_SHOTS = $self->{shots_history};
85 4 ▶ 
Subroutine __ANON__: called CC=1 SCAR=0.0
86 my $tx = Logic::Relational::variable('x');
87 my $ty = Logic::Relational::variable('y');
88 my $tm = Logic::Relational::variable('mode');
89  
90 my $q = $self->{program}->query(
91 2 Logic::Relational::Goal::Call->new(
92 'select_target', [ $tx, $ty, $tm ]
93 )
94 2 );
95 2  
96 2 my $sol = $q->next // return;
97 2 return ( $sol->value($tx), $sol->value($ty), $sol->value($tm) );
98 }
99  
100 2 1;
101  
« Battleship/AI.pm ↑ Summary Battleship/AI/Parity.pm »