AIAlice.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. """
  4. PyAISnake - 2018/2020 - by psy (epsylon@riseup.net)
  5. You should have received a copy of the GNU General Public License along
  6. with PyAISnake; if not, write to the Free Software Foundation, Inc., 51
  7. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  8. """
  9. from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
  10. import random
  11. # movements strategy (evading suicidal behavior) and trying to identify next location...
  12. class AIAlice(object):
  13. def extractName(self):
  14. name = "AliC3"
  15. return name
  16. def makeStrategy(self, allowed_moves, food, snake):
  17. if snake[0][0] == food[0]:
  18. if snake[0][1] < food[1]:
  19. move = KEY_RIGHT
  20. if move not in allowed_moves:
  21. move = KEY_LEFT
  22. else:
  23. move = KEY_LEFT
  24. if move not in allowed_moves:
  25. move = KEY_RIGHT
  26. else:
  27. if snake[0][0] < food[0]:
  28. move = KEY_UP
  29. if move not in allowed_moves:
  30. move = KEY_DOWN
  31. else:
  32. move = KEY_DOWN
  33. if move not in allowed_moves:
  34. move = KEY_UP
  35. return move
  36. def makeMove(self, win, prev_move, food, snake):
  37. name = self.extractName()
  38. if prev_move == KEY_UP:
  39. allowed_moves = [KEY_UP, KEY_LEFT, KEY_RIGHT]
  40. elif prev_move == KEY_LEFT:
  41. allowed_moves = [KEY_UP, KEY_LEFT, KEY_DOWN]
  42. elif prev_move == KEY_RIGHT:
  43. allowed_moves = [KEY_UP, KEY_RIGHT, KEY_DOWN]
  44. elif prev_move == KEY_DOWN:
  45. allowed_moves = [KEY_LEFT, KEY_RIGHT, KEY_DOWN]
  46. else:
  47. allowed_moves = [KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_DOWN]
  48. move = self.makeStrategy(allowed_moves, food, snake)
  49. return move