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

filter: Statementstmt 100% 122 / 122 Branchbran 75.0% 3 / 4 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 3 totaltotal 90.1% 146 / 162 CC 7 SCAR 12.1
CC
7
Cov
94%
__ANON__
9.7
select_target
2.0
BEGIN
1.0
CRAP 3.3
« lib/Games/GUI/Battleship/AI.pm ↑ Summary lib/Games/GUI/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;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 return $Games::GUI::Battleship::AI::Naive::CURRENT_SHOTS->{
26 2 "$vx,$vy"} ? 0 : 1;
27 2 }
28 2 );
29 2 }
30  
31 2 rule cell_valid( $x, $y ) {
32 between( 1, 10, $y );
33 2 between( 1, 10, $x );
34 2 cell_unshot( $x, $y );
35 }
36  
37 2 rule adjacent( $x1, $y1, $x2, $y2 ) {
38 ▶ is_goal( $x2, [$x1], sub ($v) { $v + 1 } );
Subroutine __ANON__: called CC=6 SCAR=22.7
39 unify( $y1, $y2 );
40 }
41 4 ▶ rule adjacent( $x1, $y1, $x2, $y2 ) {
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
42 is_goal( $x2, [$x1], sub ($v) { $v - 1 } );
43 4 ▶ unify( $y1, $y2 );
Branch: 1 branch
BranchTrueFalseTotal
$$Games::GUI::Battleship::AI::Naive::CURRENT_SHOTS{"$vx,$vy"} ? : 1 3 4
44 }
45 rule adjacent( $x1, $y1, $x2, $y2 ) {
46 2 is_goal( $y2, [$y1], sub ($v) { $v + 1 } );
47 unify( $x1, $x2 );
48 }
49 rule adjacent( $x1, $y1, $x2, $y2 ) {
50 2 is_goal( $y2, [$y1], sub ($v) { $v - 1 } );
51 unify( $x1, $x2 );
52 2 }
53 2  
54 # Priority 1: Basic Adjacent Probe (No Line Extension)
55 rule select_target( $x, $y, $mode ) {
56 2 fresh my ( $hx, $hy );
57 2 shot_hit( $hx, $hy );
58 adjacent( $hx, $hy, $x, $y );
59 cell_valid( $x, $y );
60 unify( $mode, 'Basic Adjacent Probe' );
61 }
62  
63 # Priority 2: Linear Sequential Scan
64 rule select_target( $x, $y, $mode ) {
65 between( 1, 10, $y );
66 2 between( 1, 10, $x );
67 cell_unshot( $x, $y );
68 2 unify( $mode, 'Linear Sequential Scan' );
69 2 }
70 2 }
71 2  
72 sub new ( $class, %args ) {
73 $args{name} = 'NaiveAI';
74 2 $args{difficulty} = 'easy';
75 ▶ return $class->SUPER::new(%args);
Subroutine __ANON__: called CC=1 SCAR=0.0
76 4 }
77  
78 sub init_program ($self) {
79 $self->{program} = $self->clone_program($NaiveAITemplate::PROGRAM);
80 return $self->{program};
81 }
82 2  
83 sub select_target ($self) {
84 2 local $CURRENT_SHOTS = $self->{shots_history};
85 2  
86 2 my $tx = Logic::Relational::variable('x');
87 2 my $ty = Logic::Relational::variable('y');
88 my $tm = Logic::Relational::variable('mode');
89  
90 2 my $q = $self->{program}->query(
91 ▶ Logic::Relational::Goal::Call->new(
Subroutine __ANON__: called CC=1 SCAR=0.0
92 3 'select_target', [ $tx, $ty, $tm ]
93 )
94 );
95  
96 my $sol = $q->next // return;
97 return ( $sol->value($tx), $sol->value($ty), $sol->value($tm) );
98 2 }
99  
100 2 1;
101 2  
« lib/Games/GUI/Battleship/AI.pm ↑ Summary lib/Games/GUI/Battleship/AI/Parity.pm »