| 1 | package Games::GUI::Battleship::AI::Parity; | ||
| 2 | |||
| 3 | use v5.38; | ||
| 4 | use experimental 'signatures'; | ||
| 5 | use feature 'try'; | ||
| 6 | no warnings 'experimental::try'; | ||
| 7 | use parent 'Games::GUI::Battleship::AI'; | ||
| 8 | |||
| 9 | use Logic::Relational (); | ||
| 10 | use Logic::Relational::Syntax; | ||
| 11 | use Logic::Relational::Goal::Call; | ||
| 12 | |||
| 13 | our $CURRENT_SHOTS = {}; | ||
| 14 | |||
| 15 | logic ParityAITemplate { | ||
| 16 | fact shot_hit( 0, 0 ); | ||
| 17 | fact shot_miss( 0, 0 ); | ||
| 18 | fact sunk_cell( 0, 0 ); | ||
| 19 | |||
| 20 | rule cell_unshot( $x, $y ) { | ||
| 21 | guard( | ||
| 22 | [ $x, $y ], | ||
| 23 | sub ( $vx, $vy ) { | ||
| 24 | return 0 if $vx < 1 || $vx > 10 || $vy < 1 || $vy > 10; | ||
| 25 | return $Games::GUI::Battleship::AI::Parity::CURRENT_SHOTS->{ | ||
| 26 | "$vx,$vy"} ? 0 : 1; | ||
| 27 | } | ||
| 28 | ); | ||
| 29 | } | ||
| 30 | |||
| 31 | rule cell_valid( $x, $y ) { | ||
| 32 | between( 1, 10, $x ); | ||
| 33 | between( 1, 10, $y ); | ||
| 34 | cell_unshot( $x, $y ); | ||
| 35 | } | ||
| 36 | |||
| 37 | rule adjacent( $x1, $y1, $x2, $y2 ) { | ||
| 38 | is_goal( $x2, [$x1], sub ($v) { $v + 1 } ); | ||
| 39 | unify( $y1, $y2 ); | ||
| 40 | } | ||
| 41 | rule adjacent( $x1, $y1, $x2, $y2 ) { | ||
| 42 | is_goal( $x2, [$x1], sub ($v) { $v - 1 } ); | ||
| 43 | unify( $y1, $y2 ); | ||
| 44 | } | ||
| 45 | rule adjacent( $x1, $y1, $x2, $y2 ) { | ||
| 46 | is_goal( $y2, [$y1], sub ($v) { $v + 1 } ); | ||
| 47 | unify( $x1, $x2 ); | ||
| 48 | } | ||
| 49 | rule adjacent( $x1, $y1, $x2, $y2 ) { | ||
| 50 | is_goal( $y2, [$y1], sub ($v) { $v - 1 } ); | ||
| 51 | unify( $x1, $x2 ); | ||
| 52 | } | ||
| 53 | |||
| 54 | # Priority 1: Line Extension (Horizontal) | ||
| 55 | rule select_target( $x, $y, $mode ) { | ||
| 56 | fresh my ( $x1, $x2, $yh ); | ||
| 57 | shot_hit( $x1, $yh ); | ||
| 58 | is_goal( $x2, [$x1], sub ($v) { $v + 1 } ); | ||
| 59 | shot_hit( $x2, $yh ); | ||
| 60 | unify( $y, $yh ); | ||
| 61 | line_endpoint_h( $x1, $x2, $x ); | ||
| 62 | cell_valid( $x, $y ); | ||
| 63 | unify( $mode, 'Line Extension (Horizontal)' ); | ||
| 64 | } | ||
| 65 | 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 | fresh my ( $y1, $y2, $xh ); | ||
| 75 | shot_hit( $xh, $y1 ); | ||
| 76 | is_goal( $y2, [$y1], sub ($v) { $v + 1 } ); | ||
| 77 | shot_hit( $xh, $y2 ); | ||
| 78 | unify( $x, $xh ); | ||
| 79 | line_endpoint_v( $y1, $y2, $y ); | ||
| 80 | cell_valid( $x, $y ); | ||
| 81 | unify( $mode, 'Line Extension (Vertical)' ); | ||
| 82 | } | ||
| 83 | rule line_endpoint_v( $y1, $y2, $y ) { | ||
| 84 | is_goal( $y, [$y2], sub ($v) { $v + 1 } ); | ||
| 85 | } | ||
| 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 | rule select_target( $x, $y, $mode ) { | ||
| 92 | fresh my ( $hx, $hy ); | ||
| 93 | shot_hit( $hx, $hy ); | ||
| 94 | adjacent( $hx, $hy, $x, $y ); | ||
| 95 | cell_valid( $x, $y ); | ||
| 96 | unify( $mode, 'Orthogonal Target Probe' ); | ||
| 97 | } | ||
| 98 | |||
| 99 | # Priority 3: Checkerboard Parity Hunt | ||
| 100 | rule select_target( $x, $y, $mode ) { | ||
| 101 | between( 1, 10, $x ); | ||
| 102 | between( 1, 10, $y ); | ||
| 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 | |||
| 109 | # Priority 4: Fallback Search | ||
| 110 | rule select_target( $x, $y, $mode ) { | ||
| 111 | between( 1, 10, $x ); | ||
| 112 | between( 1, 10, $y ); | ||
| 113 | cell_unshot( $x, $y ); | ||
| 114 | unify( $mode, 'Fallback Grid Search' ); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | sub new ( $class, %args ) { | ||
| 119 | $args{name} = 'ParityAI'; | ||
| 120 | $args{difficulty} = 'normal'; | ||
| 121 | return $class->SUPER::new(%args); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub init_program ($self) { | ||
| 125 | $self->{program} = $self->clone_program($ParityAITemplate::PROGRAM); | ||
| 126 | return $self->{program}; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _mode_tier ($mode) { | ||
| 130 | return 1 if index( $mode, 'Line Extension' ) == 0; | ||
| 131 | return 2 if $mode eq 'Orthogonal Target Probe'; | ||
| 132 | return 3 if $mode eq 'Checkerboard Parity Hunt'; | ||
| 133 | return 4; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub select_target ($self) { | ||
| 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 | Logic::Relational::Goal::Call->new( | ||
| 145 | 'select_target', [ $tx, $ty, $tm ] | ||
| 146 | ) | ||
| 147 | ); | ||
| 148 | |||
| 149 | my $first_sol = $q->next // return; | ||
| 150 | my $target_tier = _mode_tier( $first_sol->value($tm) ); | ||
| 151 | my @candidates = ($first_sol); | ||
| 152 | |||
| 153 | while ( my $sol = $q->next ) { | ||
| 154 | if ( _mode_tier( $sol->value($tm) ) == $target_tier ) { | ||
| 155 | push @candidates, $sol; | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | last; | ||
| 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 |