Skip to content

Instantly share code, notes, and snippets.

@synap5e
Created August 6, 2015 07:55
Show Gist options
  • Save synap5e/e2580790d3cef78d1e35 to your computer and use it in GitHub Desktop.
Save synap5e/e2580790d3cef78d1e35 to your computer and use it in GitHub Desktop.
class Branch:
def __init__(self, **kwds):
self.__dict__.update(kwds)
def possible_actions(game):
for card_index, card in enumerate(game.current_player.hand):
if card.is_playable() and card.id != WISP: # wisps are the deck filler
if card.has_target():
for target_index, target in enumerate(card.targets):
new_game = copy.deepcopy(game)
new_card = new_game.current_player.hand[card_index]
new_target = new_card.targets[target_index]
action_name = action_name = "Play %s on %s (%d/%d)" % (new_card.name, new_target.name, new_target.atk, new_target.health)
try:
new_card.play(target=new_target)
except GameOver:
pass
yield Branch(
old_game = game,
new_game = new_game,
action_name = action_name,
action = ('PLAY', new_card.id, new_target.id),
entities = (new_card, new_target)
)
else:
new_game = copy.deepcopy(game)
new_card = new_game.current_player.hand[card_index]
try:
new_card.play()
except GameOver:
pass
yield Branch(
old_game = game,
new_game = new_game,
action_name = "Play %s" % (new_card.name, ),
action = ('PLAY', new_card.id),
entities = (new_card, )
)
for character_index, character in enumerate(game.current_player.characters):
if character.can_attack():
for target_index, target in enumerate(character.targets):
new_game = copy.deepcopy(game)
new_character = new_game.current_player.characters[character_index]
new_target = new_character.targets[target_index]
# need to set name before attack
action_name = "Attack %s (%d/%d) with %s (%d/%d)" % (
new_target.name, new_target.atk, new_target.health,
new_character.name, new_character.atk, new_character.health
)
try:
new_character.attack(new_target)
new_game.process_deaths()
except GameOver:
pass
yield Branch(
old_game = game,
new_game = new_game,
action_name = action_name,
action = ('ATTACK', new_character.id, new_target.id),
entities = (new_character, new_target)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment