Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more actions #229

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions clashroyalebuildabot/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
from .action import Action
from .archers_action import ArchersAction
from .arrows_action import ArrowsAction
from .bats_action import BatsAction
from .cannon_action import CannonAction
from .fireball_action import FireballAction
from .giant_action import GiantAction
from .goblin_barrel_action import GoblinBarrelAction
from .hungry_dragon_action import HungryDragonAction
from .knight_action import KnightAction
from .minions_action import MinionsAction
from .minipekka_action import MinipekkaAction
from .musketeer_action import MusketeerAction
from .witch_action import WitchAction
from .zap_action import ZapAction

__all__ = ["Action"]
__all__ = [
"ArchersAction",
"ArrowsAction",
"BatsAction",
"CannonAction",
"FireballAction",
"GiantAction",
"GoblinBarrelAction",
"HungryDragonAction",
"KnightAction",
"MinionsAction",
"MinipekkaAction",
"MusketeerAction",
"WitchAction",
"ZapAction",
]
2 changes: 1 addition & 1 deletion clashroyalebuildabot/actions/archers_action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.action import Action
from clashroyalebuildabot.actions.generic.action import Action


class ArchersAction(Action):
Expand Down
2 changes: 1 addition & 1 deletion clashroyalebuildabot/actions/arrows_action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.spell_action import SpellAction
from clashroyalebuildabot.actions.generic.spell_action import SpellAction


class ArrowsAction(SpellAction):
Expand Down
2 changes: 1 addition & 1 deletion clashroyalebuildabot/actions/bats_action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.overhead_action import OverheadAction
from clashroyalebuildabot.actions.generic.overhead_action import OverheadAction


class BatsAction(OverheadAction):
Expand Down
6 changes: 6 additions & 0 deletions clashroyalebuildabot/actions/cannon_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.generic.defense_action import DefenseAction


class CannonAction(DefenseAction):
CARD = Cards.CANNON
2 changes: 1 addition & 1 deletion clashroyalebuildabot/actions/fireball_action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.spell_action import SpellAction
from clashroyalebuildabot.actions.generic.spell_action import SpellAction


class FireballAction(SpellAction):
Expand Down
Empty file.
25 changes: 25 additions & 0 deletions clashroyalebuildabot/actions/generic/bridge_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from clashroyalebuildabot.actions.generic.action import Action


class BridgeAction(Action):
"""
If you have 10 elixir,
play this card at the bridge on the side of the weakest tower
"""

def calculate_score(self, state):
if (self.tile_x, self.tile_y) not in {(3, 15), (14, 15)}:
return [0]

if state.numbers.elixir.number != 10:
return [0]

left_hp = state.numbers.left_enemy_princess_hp.number
right_hp = state.numbers.right_enemy_princess_hp.number
if self.tile_x == 3:
score = [1, left_hp > 0, left_hp <= right_hp]
else:
# self.tile_x == 14
score = [1, right_hp > 0, right_hp <= left_hp]

return score
31 changes: 31 additions & 0 deletions clashroyalebuildabot/actions/generic/defense_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from clashroyalebuildabot.actions.generic.action import Action


class DefenseAction(Action):
"""
If there are enemies on our side,
play the card in a defensive position
"""

def calculate_score(self, state):
if (self.tile_x, self.tile_y) not in {(8, 9), (9, 9)}:
return [0]

lhs = 0
rhs = 0
for det in state.enemies:
if det.position.tile_y > 16:
continue

if det.position.tile_x >= 9:
rhs += 1
else:
lhs += 1

if lhs == rhs == 0:
return [0]

if lhs >= rhs and self.tile_x == 9:
return [0]

return [1]
23 changes: 23 additions & 0 deletions clashroyalebuildabot/actions/generic/king_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import math

from clashroyalebuildabot.actions.generic.action import Action


class KingAction(Action):
"""
Play the card behind the king, on the side of the closest enemy
"""

def calculate_score(self, state):
if (self.tile_x, self.tile_y) not in {(8, 0), (9, 0)}:
return [0]

min_distance = float("inf")
for det in state.enemies:
distance = math.hypot(
det.position.tile_x - self.tile_x,
det.position.tile_y - self.tile_y,
)
min_distance = min(min_distance, distance)

return [0.5, -min_distance]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math

from clashroyalebuildabot.actions.action import Action
from clashroyalebuildabot.actions.generic.action import Action


class OverheadAction(Action):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import math

from clashroyalebuildabot.actions.action import Action
from clashroyalebuildabot.actions.generic.action import Action
from clashroyalebuildabot.namespaces.units import Units


class SpellAction(Action):
"""
Play the spell to hit as many enemy units as possible
"""

RADIUS = None
MIN_SCORE = 5
UNIT_TO_SCORE = {Units.SKELETON: 1}
Expand Down
2 changes: 1 addition & 1 deletion clashroyalebuildabot/actions/giant_action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.action import Action
from clashroyalebuildabot.actions.generic.action import Action


class GiantAction(Action):
Expand Down
2 changes: 1 addition & 1 deletion clashroyalebuildabot/actions/goblin_barrel_action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.action import Action
from clashroyalebuildabot.actions.generic.action import Action


class GoblinBarrelAction(Action):
Expand Down
20 changes: 20 additions & 0 deletions clashroyalebuildabot/actions/hungry_dragon_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import math

from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.generic.action import Action


class HungryDragonAction(Action):
CARD = Cards.HUNGRY_DRAGON

def calculate_score(self, state):
for det in state.enemies:
distance = math.hypot(
det.position.tile_x - self.tile_x,
det.position.tile_y - self.tile_y,
)
if 5 < distance < 6:
return [1]
if distance < 5:
return [0]
return [0]
14 changes: 2 additions & 12 deletions clashroyalebuildabot/actions/knight_action.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.action import Action
from clashroyalebuildabot.actions.generic.defense_action import DefenseAction


class KnightAction(Action):
class KnightAction(DefenseAction):
CARD = Cards.KNIGHT

def calculate_score(self, state):
score = [0.5] if state.numbers.elixir.number == 10 else [0]
for det in state.enemies:
lhs = det.position.tile_x <= 8 and self.tile_x == 8
rhs = det.position.tile_x > 8 and self.tile_x == 9

if self.tile_y < det.position.tile_y <= 14 and (lhs or rhs):
score = [1, self.tile_y - det.position.tile_y]
return score
2 changes: 1 addition & 1 deletion clashroyalebuildabot/actions/minions_action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.overhead_action import OverheadAction
from clashroyalebuildabot.actions.generic.overhead_action import OverheadAction


class MinionsAction(OverheadAction):
Expand Down
19 changes: 2 additions & 17 deletions clashroyalebuildabot/actions/minipekka_action.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.action import Action
from clashroyalebuildabot.actions.generic.bridge_action import BridgeAction


class MinipekkaAction(Action):
class MinipekkaAction(BridgeAction):
CARD = Cards.MINIPEKKA

def calculate_score(self, state):
if state.numbers.elixir.number != 10:
return [0]

left_hp = state.numbers.left_enemy_princess_hp.number
right_hp = state.numbers.right_enemy_princess_hp.number

if (self.tile_x, self.tile_y) == (3, 15):
return [1, left_hp > 0, left_hp <= right_hp]

if (self.tile_x, self.tile_y) == (14, 15):
return [1, right_hp > 0, right_hp <= left_hp]

return [0]
2 changes: 1 addition & 1 deletion clashroyalebuildabot/actions/musketeer_action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import math

from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.action import Action
from clashroyalebuildabot.actions.generic.action import Action


class MusketeerAction(Action):
Expand Down
6 changes: 6 additions & 0 deletions clashroyalebuildabot/actions/witch_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.generic.king_action import KingAction


class WitchAction(KingAction):
CARD = Cards.WITCH
2 changes: 1 addition & 1 deletion clashroyalebuildabot/actions/zap_action.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.spell_action import SpellAction
from clashroyalebuildabot.actions.generic.spell_action import SpellAction


class ZapAction(SpellAction):
Expand Down
24 changes: 11 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@

from loguru import logger

from clashroyalebuildabot.actions.archers_action import ArchersAction
from clashroyalebuildabot.actions.giant_action import GiantAction
from clashroyalebuildabot.actions.goblin_barrel_action import (
GoblinBarrelAction,
)
from clashroyalebuildabot.actions.knight_action import KnightAction
from clashroyalebuildabot.actions.minions_action import MinionsAction
from clashroyalebuildabot.actions.minipekka_action import MinipekkaAction
from clashroyalebuildabot.actions.musketeer_action import MusketeerAction
from clashroyalebuildabot.actions.zap_action import ZapAction
from clashroyalebuildabot.actions import ArchersAction
from clashroyalebuildabot.actions import CannonAction
from clashroyalebuildabot.actions import GoblinBarrelAction
from clashroyalebuildabot.actions import HungryDragonAction
from clashroyalebuildabot.actions import KnightAction
from clashroyalebuildabot.actions import MinipekkaAction
from clashroyalebuildabot.actions import MusketeerAction
from clashroyalebuildabot.actions import WitchAction
from clashroyalebuildabot.bot import Bot

start_time = datetime.now()
Expand Down Expand Up @@ -43,13 +41,13 @@ def update_terminal_title():
def main():
actions = {
ArchersAction,
ZapAction,
CannonAction,
GoblinBarrelAction,
GiantAction,
KnightAction,
MinionsAction,
HungryDragonAction,
MinipekkaAction,
MusketeerAction,
WitchAction,
}
try:
bot = Bot(actions=actions)
Expand Down