snake.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. """
  4. PyAISnake - 2018 - 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. import curses, random, time
  10. from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
  11. ######################################################################3
  12. # import AI models into sandbox
  13. from models.AIBob import AIBob
  14. AIBob = AIBob() # BoB
  15. # extract name from players
  16. def extract_name(p):
  17. name = AIBob.extractName() # BoB
  18. return name
  19. ######################################################################3
  20. # translate a thought into a text
  21. def text_to_thought(move):
  22. if move == KEY_UP:
  23. thought = "UP"
  24. elif move == KEY_LEFT:
  25. thought = "LEFT"
  26. elif move == KEY_RIGHT:
  27. thought = "RIGHT"
  28. elif move == KEY_DOWN:
  29. thought = "DOWN"
  30. else:
  31. thought = "LEARNING..."
  32. return thought
  33. # create matrix (with some random init values)
  34. def init(evol, record, max_moves):
  35. curses.initscr()
  36. x_height = 0
  37. y_height = 20
  38. x_width = 60
  39. y_width = 0
  40. win = curses.newwin(y_height, x_width, x_height, y_width)
  41. win.keypad(1)
  42. curses.noecho()
  43. curses.curs_set(0)
  44. win.border(0)
  45. win.nodelay(1)
  46. food = [random.randint(1, 18), random.randint(1, 58)] # set resources into matrix
  47. win.addch(food[0], food[1], '*')
  48. p1_a1 = random.randint(1, 10)
  49. p1_a2 = random.randint(1, 10)
  50. p1_b1 = random.randint(1, 10)
  51. p1_b2 = random.randint(1, 10)
  52. p1_c1 = random.randint(1, 10)
  53. p1_c2 = random.randint(1, 10)
  54. snake = [[p1_a1,p1_a2], [p1_b1,p1_b2], [p1_c1,p1_c2]] # generate starting point
  55. startGame(win, food, snake, evol, record, max_moves) # start NEW GAME!
  56. # start a new game simulation
  57. def startGame(win, food, snake, evol, record, max_moves):
  58. moves = 0
  59. score = 0
  60. thought = "WAKING UP..."
  61. ######################################################################3
  62. name = AIBob.extractName() # BoB
  63. move = AIBob.makeMove(win, None, food, snake) # BoB
  64. ######################################################################3
  65. moves += 1
  66. thought = text_to_thought(move)
  67. # build game
  68. while move != 27: # SPACEBAR
  69. win.border(0)
  70. win.addstr(0, 4, '| Moves: '+str(moves)+' - Max: '+str(max_moves)+' | Score: '+str(score)+' - Record: '+str(record)+' |')
  71. win.addstr(19, 4, '| '+str(name)+' -> GENERATION: '+str(evol)+' [IDEA: '+str(thought)+'] |')
  72. win.timeout(150 - (len(snake)/5 + len(snake)/10)%120) # if > length: > speed
  73. prevMove = move
  74. event = win.getch()
  75. ######################################################################3
  76. move = AIBob.makeMove(win, prevMove, food, snake) # BoB
  77. ######################################################################3
  78. moves += 1
  79. thought = text_to_thought(move)
  80. move = move if event == -1 else event
  81. if move != KEY_UP and move != KEY_DOWN and move != KEY_LEFT and move != KEY_RIGHT : # ANY KEY for pause
  82. move = -1 # pause
  83. paused = ' GAME PAUSED: PRESS -SPACEBAR- TO RESTORE'
  84. win.addstr(9, 9, paused)
  85. while move != ord(' '):
  86. paused = ' ' # 42
  87. move = win.getch()
  88. win.addstr(9, 9, paused)
  89. win.addch(food[0], food[1], '*')
  90. move = prevMove
  91. continue
  92. if score >= record: # NEW record!
  93. record = score
  94. if moves >= max_moves: # NEW max moves!
  95. max_moves = moves
  96. snake.insert(0, [snake[0][0] + (move == KEY_DOWN and 1) + (move == KEY_UP and -1), snake[0][1] + (move == KEY_LEFT and -1) + (move == KEY_RIGHT and 1)])
  97. if snake[0][0] == 0:
  98. snake[0][0] = 18
  99. if snake[0][1] == 0:
  100. snake[0][1] = 58
  101. if snake[0][0] == 19:
  102. snake[0][0] = 1
  103. if snake[0][1] == 59:
  104. snake[0][1] = 1
  105. if snake[0] in snake[1:]: # GAME LOST ;-(
  106. win.addstr(9, 18, ' SORRY: GAME OVER !!!')
  107. win.addstr(19, 4, '| PyAISnake -> MUTATION: '+str(evol)+' [IDEA: TRYING AGAIN!] |')
  108. if score >= record: # NEW record!
  109. record = score
  110. if moves >= max_moves: # NEW max moves!
  111. max_moves = moves -1
  112. event = win.getch()
  113. time.sleep(2)
  114. evol += 1
  115. init(evol, record, max_moves)
  116. if snake[0] == food:
  117. food = []
  118. score += 1
  119. while food == []:
  120. food = [random.randint(1, 18), random.randint(1, 58)]
  121. if food in snake: food = []
  122. win.addch(food[0], food[1], '*')
  123. else:
  124. last = snake.pop()
  125. try:
  126. win.addch(last[0], last[1], ' ')
  127. except:
  128. pass
  129. try:
  130. win.addch(snake[0][0], snake[0][1], '#')
  131. except:
  132. pass
  133. init(0, 0, 0) # start a new GAME ;-)