Example #1
0
class RandomAI:
    def __init__(self):
        self.engine = GameEngine()

    def get_action(self, gamestate, player):
        if self.engine.current_phase_string(gamestate) == "Pre-combat main phase":
            # play a land if possible
            for card in player.hand:
                if card.type == "Land" and not gamestate.player_played_land:
                    return {"Play": card, "player": player.ID}
            # tap all lands for mana
            use_cards = []
            for card in player.battlefield:
                if card.untapped and card.type == "Land":
                    use_cards.append(card)
            if use_cards:
                return {"Use": use_cards, "player": player.ID}

            # cast any cards we can
            for card in player.hand:
                if card.type == "Creature":
                    if player.mana_pool >= card.cmc:
                        return {"Play": card, "player": player.ID}
            return "pass_priority"
        elif self.engine.current_phase_string(gamestate) == "Declare attackers step":
            attackers = []
            for card in player.battlefield:
                if card.type == "Creature":
                    if card.summoning_sickness is False:
                        attackers.append(card)
            return {"Attack": attackers, "player": player.ID}
Example #2
0
class RandomAI():
    def __init__(self):
        self.engine = GameEngine()

    def get_action(self, gamestate, player):
        if self.engine.current_phase_string(gamestate) == 'Pre-combat main phase':
            # play a land if possible
            for card in player.hand:
                if card.type == 'Land' and not gamestate.player_played_land:
                    return {'Play': card, "player": player.ID}
            # tap all lands for mana
            use_cards = []
            for card in player.battlefield:
                if card.untapped and card.type == 'Land':
                    use_cards.append(card)
            if use_cards:
                return {'Use': use_cards, "player": player.ID}

            # cast any cards we can
            for card in player.hand:
                if card.type == "Creature":
                    if player.mana_pool >= card.cmc:
                        return {'Play': card, "player": player.ID}
            return "pass_priority"
        elif self.engine.current_phase_string(gamestate) == 'Declare attackers step':
            attackers = []
            for card in player.battlefield:
                if card.type == "Creature":
                    if card.summoning_sickness is False:
                        attackers.append(card)
            return {'Attack': attackers, "player": player.ID}
Example #3
0
game_engine = GameEngine()
AI_1 = RandomAI()
AI_2 = RandomAI()

deck_1 = Deck('goblins.deck').cards
deck_2 = Deck('goblins.deck').cards

gamestate = GameState()

game_engine.setup_game(gamestate, deck_1, deck_2)

while not game_engine.game_over(gamestate):
    if game_engine.who_has_action(gamestate) == 0:
        action = AI_1.get_action(gamestate, gamestate.players[0])
    else:
        action = AI_2.get_action(gamestate, gamestate.players[1])
    game_engine.take_action(gamestate, action)

    print "-------------------------------------------------------------------"
    print "turn: ", gamestate.turn, "phase:", game_engine.current_phase_string(
        gamestate)
    print "life: ", gamestate.players[1].life, "mana", gamestate.players[
        1].mana_pool, "library: ", len(gamestate.players[1].library)
    print "player_1_hand", gamestate.players[1].hand
    print "player_1_battlefield", gamestate.players[1].battlefield
    print "player_0_battlefield", gamestate.players[0].battlefield
    print "player_0_hand", gamestate.players[0].hand
    print "life: ", gamestate.players[0].life, "mana", gamestate.players[
        0].mana_pool, "library: ", len(gamestate.players[0].library)

print "player_0 lost: ", gamestate.players[0].lost
Example #4
0
from AI.randomAI import RandomAI
from deck import Deck

game_engine = GameEngine()
AI_1 = RandomAI()
AI_2 = RandomAI()

deck_1 = Deck('goblins.deck').cards
deck_2 = Deck('goblins.deck').cards

gamestate = GameState()

game_engine.setup_game(gamestate, deck_1, deck_2)

while not game_engine.game_over(gamestate):
    if game_engine.who_has_action(gamestate) == 0:
        action = AI_1.get_action(gamestate, gamestate.players[0])
    else:
        action = AI_2.get_action(gamestate, gamestate.players[1])
    game_engine.take_action(gamestate, action)

    print "-------------------------------------------------------------------"
    print "turn: ", gamestate.turn, "phase:", game_engine.current_phase_string(gamestate)
    print "life: ", gamestate.players[1].life, "mana", gamestate.players[1].mana_pool, "library: ", len(gamestate.players[1].library)
    print "player_1_hand", gamestate.players[1].hand
    print "player_1_battlefield", gamestate.players[1].battlefield
    print "player_0_battlefield", gamestate.players[0].battlefield
    print "player_0_hand", gamestate.players[0].hand
    print "life: ", gamestate.players[0].life, "mana", gamestate.players[0].mana_pool, "library: ", len(gamestate.players[0].library)

print "player_0 lost: ", gamestate.players[0].lost