def weapon(): game_reader = GameReader.GameReader("Linux", before) hand, turn, board, game_step, mana = game_reader.update_state() chain = HearthstoneAI.smarter_smorc(board) print(chain) game_reader = GameReader.GameReader("Linux", after) hand, turn, board, game_step, mana = game_reader.update_state() chain = HearthstoneAI.smarter_smorc(board) print(chain)
def test_simple_smorcAI(): AI = HearthstoneAI() # game_reader = GameReader.GameReader("Linux") game_reader = GameReader.GameReader("Windows") hand, turn, board, game_step, mana = game_reader.update_state() print(AI.simple_smorc(board))
def test_isUnsolved(self): problemFile = "Puzzles/Testing/test1.txt" g = Main.Grid() g.setGrid(GameReader.getGridFromFile(problemFile)) s = Solver.Solver() # Check for row duplicates g.setCell((1, 3), 7) with self.assertRaises(Solver.DuplicateError) as cm: s.isUnsolved(g) exception = cm.exception message = exception.message self.assertEqual("Row 1 contains a duplicate", message) g.setCell((1, 3), 0) # Check for column duplicates g.setCell((4, 0), 7) with self.assertRaises(Solver.DuplicateError) as cm: s.isUnsolved(g) exception = cm.exception message = exception.message self.assertEqual("Column 0 contains a duplicate", message) g.setCell((4, 0), 0) # Check for box duplicates g.setCell((1, 1), 8) with self.assertRaises(Solver.DuplicateError) as cm: s.isUnsolved(g) exception = cm.exception message = exception.message self.assertEqual("Box 0 contains a duplicate", message) g.setCell((1, 1), 0) # Check that check for 0s works properly self.assertTrue(s.isUnsolved(g))
def test_getBox(self): problemFile = "Puzzles/Testing/test1.txt" g = Main.Grid() g.setGrid(GameReader.getGridFromFile(problemFile)) correctBox = [7, 6, 8, 3, 0, 0, 4, 0, 0] self.assertEqual(correctBox, g.cellsToVals(g.getBox((0, 0)))) self.assertEqual(correctBox, g.cellsToVals(g.getBox((2, 2))))
def test_getColumn(self): problemFile = "Puzzles/Testing/test1.txt" g = Main.Grid() g.setGrid(GameReader.getGridFromFile(problemFile)) correctColumn = [7, 3, 4, 6, 0, 1, 5, 8, 9] self.assertEqual(correctColumn, g.cellsToVals(g.getColumn((0, 0)))) self.assertEqual(correctColumn, g.cellsToVals(g.getColumn((5, 0))))
def general_bot_test(): AI = HearthstoneAI() game_reader = GameReader.GameReader("Linux") # game_reader = GameReader.GameReader("Windows") hand, turn, board, game_step, mana = game_reader.update_state() if game_step == Step.BEGIN_MULLIGAN: # Mulligan step # time.sleep(4) mull = HearthstoneAI.get_mulligan(hand.hand) # time.sleep(4) elif game_step == Step.FINAL_GAMEOVER: # Start new game # self.start_game(mouse) pass elif turn: # print(mana) chain, val = HearthstoneAI.play_card(hand, mana) # print(chain) # print(val) while chain: # print(hand.size, chain[0]) # self.play_card(mouse, hand.size, chain[0]) # time.sleep(3) print("Hand size: " + str(hand.size)) print("chain: " + str(chain)) # print(val) hand, turn, board, game_step, mana = game_reader.update_state() print("mana: " + str(mana)) chain, val = HearthstoneAI.play_card(hand, mana) time.sleep(3)
def str_test(): AI = HearthstoneAI() game_reader = GameReader.GameReader("Linux") hand, turn, board, game_step, mana = game_reader.update_state() print(hand) print(board)
def test_getRow(self): problemFile = "Puzzles/Testing/test1.txt" g = Main.Grid() g.setGrid(GameReader.getGridFromFile(problemFile)) correctRow = [7, 6, 8, 5, 0, 2, 1, 4, 9] self.assertEqual(correctRow, g.cellsToVals(g.getRow((0, 0)))) self.assertEqual(correctRow, g.cellsToVals(g.getRow((0, 4))))
def value_trade_test(): game_reader = GameReader.GameReader("Linux", board_state) hand, turn, board, game_step, mana = game_reader.update_state() print(board) trades = HearthstoneAI.value_trade(board) print(trades)
def hash_test(): AI = HearthstoneAI() game_reader = GameReader.GameReader("Windows") hand, turn, board, game_step, mana = game_reader.update_state() print(board.ally) playstate = game_reader.friendly_player.tags.get(GameTag.PLAYSTATE, None) print(playstate)
def mid_gamemana(): game_reader = GameReader.GameReader("Linux", data) hand, turn, board, game_step, mana = game_reader.update_state() print(hand) chain, val = HearthstoneAI.play_card(hand, mana) print(chain) print(val)
def test_setPossibles(self): problemFile = "Puzzles/Testing/test1.txt" g = Main.Grid() g.setGrid(GameReader.getGridFromFile(problemFile)) g.setPossibles() correctPossibles = [5, 9] cell = g.getCell((1, 1)) self.assertEqual(correctPossibles, cell.possibleValues)
def board_test(): AI = HearthstoneAI() game_reader = GameReader.GameReader("Linux", board_state) hand, turn, board, game_step, mana = game_reader.update_state() print(board.weapon.name) print(board.weapon.position) print(board)
def multi_taunt(): game_reader = GameReader.GameReader("Linux", taunts) hand, turn, board, game_step, mana = game_reader.update_state() print(board) # for i in board.ally_minions: # print(i) chain = HearthstoneAI.smarter_smorc(board) print(chain)
def play_phase_test(): AI = HearthstoneAI() game_reader = GameReader.GameReader("Linux", board_state) hand, turn, board, game_step, mana = game_reader.update_state() t1 = time.time() chain, val = HearthstoneAI.play_card(hand, mana) t2 = time.time() print(t2 - t1) print(chain)
def coin(): game_reader = GameReader.GameReader("Linux", coins) hand, turn, board, game_step, mana = game_reader.update_state() game = game_reader.get_game() players = game.players print(players) me = players[0] print(mana)
def run(argv): if len(argv) == 1: problemFile = dirprefix + fileName else: problemFile = argv[1] g = Grid() g.setGrid(GameReader.getGridFromFile(problemFile)) g.setPossibles() s = Solver.Solver() s.solve(g) g.print()
def won(): a = time.time() AI = HearthstoneAI() game_reader = GameReader.GameReader("Windows") b = time.time() print(b - a) # hand, turn, board, game_step, mana = game_reader.update_state() game = game_reader.get_game() print(game.tags) for i in game.tags: print(i) print(game.tags[GameTag.STATE])
def get_enemy_hp(): AI = HearthstoneAI() game_reader = GameReader.GameReader("Linux") hand, turn, board, game_step, mana = game_reader.update_state() game = game_reader.get_game() for i in game.in_zone(1): print(i) if type(i) == Card and 'HERO' in i.card_id: for j in i.tags: print(j) print(i.tags[GameTag.HEALTH])
def mana_test(): AI = HearthstoneAI() game_reader = GameReader.GameReader("Linux", board_state) hand, turn, board, game_step, mana = game_reader.update_state() print(mana)
def test_importing(self): problemFile = "Puzzles/Testing/test1.txt" g = Main.Grid() g.setGrid(GameReader.getGridFromFile(problemFile)) self.assertEqual(7, g.getCell((0, 0)).getValue())
def handle_play(self, game_frame, test=False): if test: mouse = None else: mouse = InputController(game = self.game) game_reader = GameReader.GameReader("Windows") with io.open(r'Logs\wins.log', 'r') as f: data = [] for line in f.readlines(): string = line.split(' ') data.append((string[-1].strip())) wins, losses, total, prev_oppo = (int)(data[0]), (int)(data[1]), (int)(data[2]), data[3] curr_oppo = prev_oppo hand, turn, board, game_step, mana = game_reader.update_state() if board.enemy: curr_oppo = board.enemy.name if game_step == Step.BEGIN_MULLIGAN: time.sleep(9) mull = HearthstoneAI.get_mulligan(hand.hand) self.mull_card(mouse, hand, mull) while game_step == Step.BEGIN_MULLIGAN: self.mull_card(mouse, hand, mull) print("Waiting for opponent to mulligan") time.sleep(3) hand, turn, board, game_step, mana = game_reader.update_state() elif game_step == Step.FINAL_GAMEOVER: self.start_game(mouse) elif turn: time.sleep(2) t0 = time.time() ## CARD PLAY PHASE # 1. Calculate best chain of cards to play using HearthstoneAI.play_cards # 2. Play first card and wait in case of drawing card # 3. Repeat steps 1-2 chain, val= HearthstoneAI.play_card(hand, mana) playstate = game_reader.friendly_player.tags.get(GameTag.PLAYSTATE, None) game_end = playstate == PlayState.WON or playstate == PlayState.LOST print("Hand: " + str(hand)) print("Play_chain" + str(chain)) timeout = 0 hp = 1 while chain and turn and len(board.ally_minions) != 7 and not game_end and timeout < 11: playstate = game_reader.friendly_player.tags.get(GameTag.PLAYSTATE, None) print("Playstate: " + str(playstate)) self.play_card(mouse, hand.size, chain[0]) hp = chain[0] != -2 time.sleep(1) t3 = time.time() hand, turn, board, game_step, mana = game_reader.update_state(hp) t4 = time.time() print("update state time: " + str(t4-t3)) if mana == 0: break t5 = time.time() chain, val = HearthstoneAI.play_card(hand, mana) print("Hand" + str(hand)) print("Play_chain" + str(chain)) t6 = time.time() print("play_card AI: " + str(t6-t5)) timeout += 1 t1 = time.time() print("PLAY PHASE: " + str(t1-t0)) ## ATTACK PHASE # Attacking strategy: # 1. Calculate chain of attack actions # 2. Execute first attack action and wait (in case of deathrattle summons) # 3. Repeat steps 1-2 until no minions can attack anymore t0 = time.time() timeout = 0 hand, turn, board, game_step, mana = game_reader.update_state() chain = HearthstoneAI.smarter_smorc(board) playstate = game_reader.friendly_player.tags.get(GameTag.PLAYSTATE, None) game_end = playstate == PlayState.WON or playstate == PlayState.LOST print("Attack Chain: " + str(chain)) while chain and turn and not game_end and timeout < 10: self.attack(mouse, len(board.ally_minions), len(board.enemy_minions), chain[0]) time.sleep(1) # Update state timeout += 1 hand, turn, board, game_step, mana = game_reader.update_state() chain = HearthstoneAI.smarter_smorc(board) space = len(board.ally_minions) == 7 playstate = game_reader.friendly_player.tags.get(GameTag.PLAYSTATE, None) game_end = playstate == PlayState.WON or playstate == PlayState.LOST print("Attack Chain: " + str(chain)) t1 = time.time() print("ATTACK PHASE: " + str(t1-t0)) if mana >= 2 and hp: self.hero_power(mouse) self.end_turn(mouse) try: playstate = game_reader.friendly_player.tags.get(GameTag.PLAYSTATE, None) except: time.sleep(3) hand, turn, board, game_step, mana = game_reader.update_state() playstate = game_reader.friendly_player.tags.get(GameTag.PLAYSTATE, None) if playstate == PlayState.WON or playstate == PlayState.LOST: if prev_oppo != curr_oppo: if playstate == PlayState.WON: time.sleep(3) self.concede(mouse, game_reader) wins += 1 elif playstate == PlayState.LOST: losses += 1 total += 1 # print("Win ratio: " + str(wins/total)) with io.open(r'Logs/wins.log', 'w') as f: f.write('Wins: ' + str(wins) + '\n') f.write('Losses: ' + str(losses) + '\n') f.write('Total: ' + str(total) + '\n') f.write('Previous Opponnent: ' + curr_oppo + '\n')
def setup(self, problemFile): self.setGrid(GameReader.getGridFromFile(problemFile)) self.setPossibles()