| 1 | package Games::GUI::Battleship::AI::Parity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 ParityAITemplate { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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::Parity::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, $x ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | between( 1, 10, $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 3900 | ▶ | unify( $x1, $x2 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=22.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 3900 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$vx < 1 or $vx > 10 or $vy < 1 or $vy > 10
$vx < 1 or $vx > 10 or $vy < 1
$vx < 1 or $vx > 10
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | rule adjacent( $x1, $y1, $x2, $y2 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 3900 | ▶ | is_goal( $y2, [$y1], sub ($v) { $v - 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | unify( $x1, $x2 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # Priority 1: Line Extension (Horizontal) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | rule select_target( $x, $y, $mode ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | fresh my ( $x1, $x2, $yh ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | shot_hit( $x1, $yh ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | is_goal( $x2, [$x1], sub ($v) { $v + 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | shot_hit( $x2, $yh ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | unify( $y, $yh ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | line_endpoint_h( $x1, $x2, $x ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | cell_valid( $x, $y ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | unify( $mode, 'Line Extension (Horizontal)' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | rule line_endpoint_h( $x1, $x2, $x ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | is_goal( $x, [$x2], sub ($v) { $v + 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | rule line_endpoint_h( $x1, $x2, $x ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | is_goal( $x, [$x1], sub ($v) { $v - 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # Priority 1: Line Extension (Vertical) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | rule select_target( $x, $y, $mode ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | fresh my ( $y1, $y2, $xh ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | shot_hit( $xh, $y1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | is_goal( $y2, [$y1], sub ($v) { $v + 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | shot_hit( $xh, $y2 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | unify( $x, $xh ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | line_endpoint_v( $y1, $y2, $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | cell_valid( $x, $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | unify( $mode, 'Line Extension (Vertical)' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | rule line_endpoint_v( $y1, $y2, $y ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | is_goal( $y, [$y2], sub ($v) { $v + 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 99 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | rule line_endpoint_v( $y1, $y2, $y ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | is_goal( $y, [$y1], sub ($v) { $v - 1 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # Priority 2: Orthogonal Neighbor Probe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | rule select_target( $x, $y, $mode ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | fresh my ( $hx, $hy ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | shot_hit( $hx, $hy ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 2 | adjacent( $hx, $hy, $x, $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | cell_valid( $x, $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | unify( $mode, 'Orthogonal Target Probe' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # Priority 3: Checkerboard Parity Hunt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | rule select_target( $x, $y, $mode ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | between( 1, 10, $x ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 99 | ▶ | between( 1, 10, $y ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | cell_unshot( $x, $y ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | guard( [ $x, $y ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | sub ( $vx, $vy ) { return ( $vx + $vy ) % 2 == 0; } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | unify( $mode, 'Checkerboard Parity Hunt' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # Priority 4: Fallback Search | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | rule select_target( $x, $y, $mode ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | between( 1, 10, $x ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | between( 1, 10, $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | cell_unshot( $x, $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | unify( $mode, 'Fallback Grid Search' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | sub new ( $class, %args ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 98 | ▶ | $args{name} = 'ParityAI'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | $args{difficulty} = 'normal'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | return $class->SUPER::new(%args); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | sub init_program ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | $self->{program} = $self->clone_program($ParityAITemplate::PROGRAM); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | return $self->{program}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | sub _mode_tier ($mode) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | return 1 if index( $mode, 'Line Extension' ) == 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | return 2 if $mode eq 'Orthogonal Target Probe'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | return 3 if $mode eq 'Checkerboard Parity Hunt'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | return 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 98 | ▶ | sub select_target ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | local $CURRENT_SHOTS = $self->{shots_history}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | my $tx = Logic::Relational::variable('x'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | my $ty = Logic::Relational::variable('y'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | my $tm = Logic::Relational::variable('mode'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | my $q = $self->{program}->query( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | Logic::Relational::Goal::Call->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 'select_target', [ $tx, $ty, $tm ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 2 | my $first_sol = $q->next // return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | my $target_tier = _mode_tier( $first_sol->value($tm) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | my @candidates = ($first_sol); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 2 | while ( my $sol = $q->next ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 2 | if ( _mode_tier( $sol->value($tm) ) == $target_tier ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 2 | push @candidates, $sol; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 100 | ▶ | last; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | my $chosen = $candidates[ int rand @candidates ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | return ( $chosen->value($tx), $chosen->value($ty), $chosen->value($tm) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||