| 1 | package Games::GUI::Battleship::AI::Hard; | ||
| 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 | use List::Util qw(min max); | ||
| 13 | |||
| 14 | our $CURRENT_SHOTS = {}; | ||
| 15 | |||
| 16 | logic HardAITemplate { | ||
| 17 | fact shot_hit( 0, 0 ); | ||
| 18 | fact shot_miss( 0, 0 ); | ||
| 19 | fact sunk_cell( 0, 0 ); | ||
| 20 | fact ship_alive( 'Carrier', 5 ); | ||
| 21 | fact ship_alive( 'Battleship', 4 ); | ||
| 22 | fact ship_alive( 'Cruiser', 3 ); | ||
| 23 | fact ship_alive( 'Submarine', 3 ); | ||
| 24 | fact ship_alive( 'Destroyer', 2 ); | ||
| 25 | |||
| 26 | rule cell_unshot( $x, $y ) { | ||
| 27 | guard( | ||
| 28 | [ $x, $y ], | ||
| 29 | sub ( $vx, $vy ) { | ||
| 30 | return 0 if $vx < 1 || $vx > 10 || $vy < 1 || $vy > 10; | ||
| 31 | return $Games::GUI::Battleship::AI::Hard::CURRENT_SHOTS->{ | ||
| 32 | "$vx,$vy"} ? 0 : 1; | ||
| 33 | } | ||
| 34 | ); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub new ( $class, %args ) { | ||
| 39 | $args{name} = 'ProbabilityAI'; | ||
| 40 | $args{difficulty} = 'hard'; | ||
| 41 | my $self = $class->SUPER::new(%args); | ||
| 42 | $self->{surviving_ships} = { | ||
| 43 | Carrier => 5, | ||
| 44 | Battleship => 4, | ||
| 45 | Cruiser => 3, | ||
| 46 | Submarine => 3, | ||
| 47 | Destroyer => 2, | ||
| 48 | }; | ||
| 49 | $self->{active_hits} = {}; | ||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub init_program ($self) { | ||
| 54 | $self->{program} = $self->clone_program($HardAITemplate::PROGRAM); | ||
| 55 | $self->{surviving_ships} = { | ||
| 56 | Carrier => 5, | ||
| 57 | Battleship => 4, | ||
| 58 | Cruiser => 3, | ||
| 59 | Submarine => 3, | ||
| 60 | Destroyer => 2, | ||
| 61 | }; | ||
| 62 | $self->{active_hits} = {}; | ||
| 63 | return $self->{program}; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub record_result ( $self, %args ) { | ||
| 67 | my $x = $args{x}; | ||
| 68 | my $y = $args{y}; | ||
| 69 | 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 | } | ||
| 77 | elsif ( $result eq 'sunk' ) { | ||
| 78 | if ($ship) { | ||
| 79 | my $type = $ship->type; | ||
| 80 | if ( exists $self->{surviving_ships}{$type} ) { | ||
| 81 | 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 | } | ||
| 88 | else { | ||
| 89 | delete $self->{active_hits}{"$x,$y"}; | ||
| 90 | my ($smallest) = sort { | ||
| 91 | $self->{surviving_ships}{$a} <=> $self->{surviving_ships}{$b} | ||
| 92 | } keys %{ $self->{surviving_ships} }; | ||
| 93 | if ($smallest) { | ||
| 94 | my $sz = delete $self->{surviving_ships}{$smallest}; | ||
| 95 | $self->{program}->retract( ship_alive => $smallest, $sz ); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return $self; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub select_target ($self) { | ||
| 104 | local $CURRENT_SHOTS = $self->{shots_history}; | ||
| 105 | |||
| 106 | my %surviving = %{ $self->{surviving_ships} }; | ||
| 107 | %surviving = ( Destroyer => 2 ) unless %surviving; | ||
| 108 | my $min_len = min( values %surviving ) // 2; | ||
| 109 | |||
| 110 | my @hits = keys %{ $self->{active_hits} }; | ||
| 111 | my $has_hits = scalar @hits > 0 ? 1 : 0; | ||
| 112 | |||
| 113 | my %weights; | ||
| 114 | |||
| 115 | for my $type ( sort keys %surviving ) { | ||
| 116 | my $len = $surviving{$type}; | ||
| 117 | |||
| 118 | # Horizontal placements | ||
| 119 | for my $y ( 1 .. 10 ) { | ||
| 120 | for my $x ( 1 .. 11 - $len ) { | ||
| 121 | my $valid = 1; | ||
| 122 | my $hit_count = 0; | ||
| 123 | my @coords; | ||
| 124 | |||
| 125 | for my $i ( 0 .. $len - 1 ) { | ||
| 126 | my $cx = $x + $i; | ||
| 127 | my $key = "$cx,$y"; | ||
| 128 | if ( $self->{shots_history}{$key} | ||
| 129 | && !$self->{active_hits}{$key} ) | ||
| 130 | { | ||
| 131 | $valid = 0; | ||
| 132 | last; | ||
| 133 | } | ||
| 134 | $hit_count++ if $self->{active_hits}{$key}; | ||
| 135 | push @coords, $key; | ||
| 136 | } | ||
| 137 | next unless $valid; | ||
| 138 | |||
| 139 | if ($has_hits) { | ||
| 140 | next if $hit_count == 0; | ||
| 141 | my $w = 10**$hit_count; | ||
| 142 | for my $k (@coords) { | ||
| 143 | $weights{$k} += $w unless $self->{shots_history}{$k}; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | for my $k (@coords) { | ||
| 148 | next if $self->{shots_history}{$k}; | ||
| 149 | my ( $cx, $cy ) = split /,/x, $k; | ||
| 150 | my $bonus = ( ( $cx + $cy ) % $min_len == 0 ) ? 2 : 1; | ||
| 151 | $weights{$k} += $bonus; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | # Vertical placements | ||
| 158 | for my $x ( 1 .. 10 ) { | ||
| 159 | for my $y ( 1 .. 11 - $len ) { | ||
| 160 | my $valid = 1; | ||
| 161 | my $hit_count = 0; | ||
| 162 | my @coords; | ||
| 163 | |||
| 164 | for my $i ( 0 .. $len - 1 ) { | ||
| 165 | my $cy = $y + $i; | ||
| 166 | my $key = "$x,$cy"; | ||
| 167 | if ( $self->{shots_history}{$key} | ||
| 168 | && !$self->{active_hits}{$key} ) | ||
| 169 | { | ||
| 170 | $valid = 0; | ||
| 171 | last; | ||
| 172 | } | ||
| 173 | $hit_count++ if $self->{active_hits}{$key}; | ||
| 174 | push @coords, $key; | ||
| 175 | } | ||
| 176 | next unless $valid; | ||
| 177 | |||
| 178 | if ($has_hits) { | ||
| 179 | next if $hit_count == 0; | ||
| 180 | my $w = 10**$hit_count; | ||
| 181 | for my $k (@coords) { | ||
| 182 | $weights{$k} += $w unless $self->{shots_history}{$k}; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | else { | ||
| 186 | for my $k (@coords) { | ||
| 187 | next if $self->{shots_history}{$k}; | ||
| 188 | my ( $cx, $cy ) = split /,/x, $k; | ||
| 189 | my $bonus = ( ( $cx + $cy ) % $min_len == 0 ) ? 2 : 1; | ||
| 190 | $weights{$k} += $bonus; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | unless (%weights) { | ||
| 198 | |||
| 199 | # Fallback to any unshot cell | ||
| 200 | for my $y ( 1 .. 10 ) { | ||
| 201 | for my $x ( 1 .. 10 ) { | ||
| 202 | return ( $x, $y, 'Fallback Grid Search' ) | ||
| 203 | unless $self->{shots_history}{"$x,$y"}; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | return; | ||
| 207 | } | ||
| 208 | |||
| 209 | my $max_w = -1; | ||
| 210 | for my $k ( keys %weights ) { | ||
| 211 | $max_w = $weights{$k} if $weights{$k} > $max_w; | ||
| 212 | } | ||
| 213 | |||
| 214 | my @top = grep { $weights{$_} == $max_w } keys %weights; | ||
| 215 | my $chosen = $top[ int rand @top ]; | ||
| 216 | my ( $tx, $ty ) = split /,/x, $chosen; | ||
| 217 | |||
| 218 | my $mode = 'Probability Density Hunt'; | ||
| 219 | if ($has_hits) { | ||
| 220 | $mode = | ||
| 221 | ( scalar @hits >= 2 && $max_w >= 200 ) | ||
| 222 | ? 'Target Heatmap (Line Extension)' | ||
| 223 | : 'Target Heatmap (Orthogonal Focus)'; | ||
| 224 | } | ||
| 225 | |||
| 226 | return ( $tx, $ty, $mode ); | ||
| 227 | } | ||
| 228 | |||
| 229 | 1; | ||
| 230 |