1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
| #!/usr/bin/env perl
use v5.36;
package Command {
use Moo::Role;
requires 'execute';
has actor => (
is => 'ro',
required => 1,
);
has target => (
is => 'ro',
required => 0,
);
}
package Character {
use Moo;
has name => (
is => 'ro',
required => 1,
);
has hp => (
is => 'rw',
default => 100,
);
has max_hp => (
is => 'ro',
default => 100,
);
has attack_power => (
is => 'ro',
default => 10,
);
has is_defending => (
is => 'rw',
default => 0,
);
has ai_strategy => (
is => 'rw',
default => sub { undef },
);
sub is_alive($self) {
return $self->hp > 0;
}
sub take_damage($self, $damage) {
my $actual_damage = $self->is_defending ? int($damage / 2) : $damage;
my $new_hp = $self->hp - $actual_damage;
$self->hp($new_hp < 0 ? 0 : $new_hp);
$self->is_defending(0);
return $actual_damage;
}
sub decide_action($self, $target) {
die "No AI strategy set" unless $self->ai_strategy;
return $self->ai_strategy->decide_action($self, $target);
}
}
package AttackCommand {
use Moo;
with 'Command';
sub execute($self) {
my $actor = $self->actor;
my $target = $self->target;
my $damage = $actor->attack_power;
my $actual = $target->take_damage($damage);
my $msg = $actor->name . "の攻撃! " . $target->name . "に " . $actual . " のダメージ!";
$msg .= "(防御で軽減)" if $actual < $damage;
say $msg;
}
}
package DefendCommand {
use Moo;
with 'Command';
sub execute($self) {
my $actor = $self->actor;
$actor->is_defending(1);
say $actor->name . "は防御の構えをとった!";
}
}
package ItemCommand {
use Moo;
with 'Command';
has item_name => (
is => 'ro',
default => 'ポーション',
);
has heal_amount => (
is => 'ro',
default => 30,
);
sub execute($self) {
my $actor = $self->actor;
my $max_hp = $actor->max_hp;
my $new_hp = $actor->hp + $self->heal_amount;
$actor->hp($new_hp > $max_hp ? $max_hp : $new_hp);
say $actor->name . "は" . $self->item_name . "を使った! HPが " . $self->heal_amount . " 回復!";
}
}
package AIStrategy {
use Moo::Role;
requires 'decide_action';
}
package AggressiveAI {
use Moo;
with 'AIStrategy';
sub decide_action($self, $actor, $target) {
return AttackCommand->new(
actor => $actor,
target => $target,
);
}
}
package DefensiveAI {
use Moo;
with 'AIStrategy';
sub decide_action($self, $actor, $target) {
if ($actor->hp <= $actor->max_hp / 2) {
return ItemCommand->new(
actor => $actor,
item_name => 'ポーション',
heal_amount => 20,
);
}
return AttackCommand->new(
actor => $actor,
target => $target,
);
}
}
package RandomAI {
use Moo;
with 'AIStrategy';
sub decide_action($self, $actor, $target) {
my @actions = (
sub { AttackCommand->new(actor => $actor, target => $target) },
sub { DefendCommand->new(actor => $actor) },
sub { ItemCommand->new(actor => $actor, item_name => 'ポーション', heal_amount => 10) },
);
my $chosen = $actions[int(rand(@actions))];
return $chosen->();
}
}
# デモ: 各AIの動作確認
say "=== AI戦略デモ ===";
say "";
my $hero = Character->new(
name => '勇者',
hp => 100,
max_hp => 100,
attack_power => 15,
);
# 攻撃的AI
say "【攻撃的AI】";
my $goblin = Character->new(
name => 'ゴブリン',
hp => 50,
max_hp => 50,
attack_power => 8,
ai_strategy => AggressiveAI->new(),
);
$goblin->decide_action($hero)->execute();
say "";
# 防御的AI(HPが半分以下)
say "【防御的AI(HP低下時)】";
my $orc = Character->new(
name => 'オーク',
hp => 20, # 半分以下
max_hp => 50,
attack_power => 12,
ai_strategy => DefensiveAI->new(),
);
$orc->decide_action($hero)->execute();
say "";
# ランダムAI
say "【ランダムAI(3回実行)】";
my $slime = Character->new(
name => 'スライム',
hp => 30,
max_hp => 30,
attack_power => 5,
ai_strategy => RandomAI->new(),
);
for (1..3) {
$slime->decide_action($hero)->execute();
}
|