| 1 | package Games::GUI::Battleship::AI; | ||||||||||||||||||||||
| 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 Carp qw(croak); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 2 | ▶ | use Logic::Relational (); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 2 | ▶ | use Logic::Relational::Goal::Call; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 28 | ▶ | sub new ( $class, %args ) { | ||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||
| 12 | my $self = { | ||||||||||||||||||||||
| 13 | name => $args{name} // 'AI', | ||||||||||||||||||||||
| 14 | 28 | ▶ | difficulty => $args{difficulty} // 'normal', | ||||||||||||||||||||
|
Condition50%2/4
$args{"name"} // "AI"
$args{"difficulty"} // "normal"
MC/DC0%0/1
$args{"name"} // "AI"
$args{"name"}
MC/DC0%0/1
$args{"difficulty"} // "normal"
$args{"difficulty"}
Truth table50%1/2
A
A= $args{"name"}
Truth table50%1/2
A
A= $args{"difficulty"}
| |||||||||||||||||||||||
| 15 | shots_history => {}, | ||||||||||||||||||||||
| 16 | stats => { shots => 0, hits => 0, ships_sunk => 0 }, | ||||||||||||||||||||||
| 17 | program => undef, | ||||||||||||||||||||||
| 18 | }; | ||||||||||||||||||||||
| 19 | 28 | bless $self, $class; | |||||||||||||||||||||
| 20 | 28 | $self->init_program; | |||||||||||||||||||||
| 21 | 28 | return $self; | |||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | 28 | ▶ | sub create ( $class, $difficulty = 'normal' ) { | ||||||||||||||||||||
|
Subroutine
create: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||
| 25 | 28 | $difficulty = lc($difficulty); | |||||||||||||||||||||
| 26 | 28 | ▶ | if ( $difficulty eq 'easy' ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 27 | 2 | require Games::GUI::Battleship::AI::Naive; | |||||||||||||||||||||
| 28 | 2 | return Games::GUI::Battleship::AI::Naive->new; | |||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||
| 30 | elsif ( $difficulty eq 'hard' ) { | ||||||||||||||||||||||
| 31 | 2 | require Games::GUI::Battleship::AI::Hard; | |||||||||||||||||||||
| 32 | 2 | return Games::GUI::Battleship::AI::Hard->new; | |||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||
| 34 | else { | ||||||||||||||||||||||
| 35 | 24 | require Games::GUI::Battleship::AI::Parity; | |||||||||||||||||||||
| 36 | 24 | return Games::GUI::Battleship::AI::Parity->new; | |||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | 4 | ▶ | sub name ($self) { | ||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 41 | 4 | return $self->{name}; | |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | 4 | ▶ | sub difficulty ($self) { | ||||||||||||||||||||
|
Subroutine
difficulty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 45 | 4 | return $self->{difficulty}; | |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | 0 | ▶ | sub stats ($self) { | ||||||||||||||||||||
|
Subroutine
stats: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 49 | 0 | return $self->{stats}; | |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | 0 | ▶ | sub program ($self) { | ||||||||||||||||||||
|
Subroutine
program: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 53 | 0 | return $self->{program}; | |||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||
| 55 | |||||||||||||||||||||||
| 56 | 0 | ▶ | sub shots_history ($self) { | ||||||||||||||||||||
|
Subroutine
shots_history: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 57 | 0 | return $self->{shots_history}; | |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | 1 | ▶ | sub has_shot ( $self, $x, $y ) { | ||||||||||||||||||||
|
Subroutine
has_shot: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||
| 61 | 1 | ▶ | return $self->{shots_history}{"$x,$y"} ? 1 : 0; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | 0 | ▶ | sub init_program ($self) { | ||||||||||||||||||||
|
Subroutine
init_program: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 65 | 0 | croak "Subclasses must implement init_program"; | |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | 0 | ▶ | sub select_target ($self) { | ||||||||||||||||||||
|
Subroutine
select_target: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 69 | 0 | croak "Subclasses must implement select_target"; | |||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | 24 | ▶ | sub record_result ( $self, %args ) { | ||||||||||||||||||||
|
Subroutine
record_result: called CC=8 SCAR=21.3
Pod
undocumented
| |||||||||||||||||||||||
| 73 | 24 | ▶ | my $x = $args{x} // croak 'x is required'; | ||||||||||||||||||||
|
Condition50%1/2
$args{"x"} // croak("x is required")
MC/DC0%0/1
$args{"x"} // croak("x is required")
$args{"x"}
Truth table50%1/2
A
A= $args{"x"}
| |||||||||||||||||||||||
| 74 | 24 | ▶ | my $y = $args{y} // croak 'y is required'; | ||||||||||||||||||||
|
Condition50%1/2
$args{"y"} // croak("y is required")
MC/DC0%0/1
$args{"y"} // croak("y is required")
$args{"y"}
Truth table50%1/2
A
A= $args{"y"}
| |||||||||||||||||||||||
| 75 | 24 | ▶ | my $result = $args{result} // croak 'result is required'; | ||||||||||||||||||||
|
Condition50%1/2
$args{"result"} // croak("result is required")
MC/DC0%0/1
$args{"result"} // croak("result is required")
$args{"result"}
Truth table50%1/2
A
A= $args{"result"}
| |||||||||||||||||||||||
| 76 | 24 | my $ship = $args{ship}; | |||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | 24 | $self->{shots_history}{"$x,$y"} = 1; | |||||||||||||||||||||
| 79 | 24 | $self->{stats}{shots}++; | |||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | 24 | ▶ | if ( $result eq 'hit' ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 82 | 6 | $self->{stats}{hits}++; | |||||||||||||||||||||
| 83 | 6 | $self->{program}->fact( shot_hit => $x, $y ); | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | elsif ( $result eq 'sunk' ) { | ||||||||||||||||||||||
| 86 | 2 | $self->{stats}{hits}++; | |||||||||||||||||||||
| 87 | 2 | $self->{stats}{ships_sunk}++; | |||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | 2 | ▶ | if ($ship) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 90 | 2 | for my $coord ( $ship->coordinates ) { | |||||||||||||||||||||
| 91 | 4 | my ( $cx, $cy ) = ( $coord->[0], $coord->[1] ); | |||||||||||||||||||||
| 92 | 4 | $self->{program}->retract( shot_hit => $cx, $cy ); | |||||||||||||||||||||
| 93 | 4 | $self->{program}->fact( sunk_cell => $cx, $cy ); | |||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||
| 97 | 0 | $self->{program}->retract( shot_hit => $x, $y ); | |||||||||||||||||||||
| 98 | 0 | $self->{program}->fact( sunk_cell => $x, $y ); | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||
| 102 | 16 | $self->{program}->fact( shot_miss => $x, $y ); | |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | 24 | return $self; | |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | 0 | ▶ | sub reset ($self) { | ||||||||||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 109 | 0 | $self->{shots_history} = {}; | |||||||||||||||||||||
| 110 | 0 | $self->{stats} = { shots => 0, hits => 0, ships_sunk => 0 }; | |||||||||||||||||||||
| 111 | 0 | $self->init_program; | |||||||||||||||||||||
| 112 | 0 | return $self; | |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | 28 | ▶ | sub clone_program ( $self, $template ) { | ||||||||||||||||||||
|
Subroutine
clone_program: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 116 | 28 | require Logic::Relational::Program; | |||||||||||||||||||||
| 117 | 28 | my $clone = Logic::Relational::Program->new; | |||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | 28 | for my $key ( keys %{ $template->{predicates} } ) { | |||||||||||||||||||||
| 120 | 604 | $clone->{predicates}{$key} = [ @{ $template->{predicates}{$key} } ]; | |||||||||||||||||||||
| 121 | $clone->{active_predicates}{$key} = | ||||||||||||||||||||||
| 122 | 604 | [ @{ $template->{predicates}{$key} } ]; | |||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||
| 124 | 28 | $clone->{generators} = { %{ $template->{generators} } }; | |||||||||||||||||||||
| 125 | 28 | return $clone; | |||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | 2 | 1; | |||||||||||||||||||||
| 129 | |||||||||||||||||||||||