bot.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. import os
  3. import time
  4. from itertools import count
  5. from strategy import State, moves_to_keys
  6. from interaction import get_exapunks_window, focus_window, \
  7. screenshot_board, press_keys, listen_keys
  8. def save_screenshot(win):
  9. board = screenshot_board(win).convert('RGB')
  10. os.makedirs('screens', exist_ok=True)
  11. for i in count(1):
  12. path = 'screens/board%d.png' % i
  13. if not os.path.exists(path):
  14. print('save screenshot in', path)
  15. board.save(path)
  16. break
  17. if __name__ == '__main__':
  18. win = get_exapunks_window()
  19. focus_window(win)
  20. listen_keys({'s': lambda: save_screenshot(win)})
  21. prev_score = None
  22. while True:
  23. board = screenshot_board(win)
  24. try:
  25. state = State.detect(board)
  26. except (TypeError, AssertionError):
  27. print('error during parsing, wait for a bit')
  28. time.sleep(.1)
  29. continue
  30. print('\033c', end='')
  31. print('parsed:')
  32. state.print()
  33. print()
  34. start = time.time()
  35. moves = state.solve()
  36. end = time.time()
  37. print('thought for %.4f seconds' % (end - start))
  38. if moves:
  39. print('moves:', moves_to_keys(moves))
  40. points, newstate = state.simulate(moves)
  41. score = newstate.score(points, moves, state)
  42. print(' score:', score)
  43. print('prev score:', prev_score)
  44. print()
  45. prev_score = score
  46. print('target after moves:')
  47. newstate.print()
  48. print()
  49. press_keys(win, moves_to_keys(moves))
  50. else:
  51. print('no moves')