123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- """
- PyAISnake - 2018/2020 - by psy (epsylon@riseup.net)
- You should have received a copy of the GNU General Public License along
- with PyAISnake; if not, write to the Free Software Foundation, Inc., 51
- Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- """
- import curses, random, time
- from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
- from models.AIBob import AIBob
- AIBob = AIBob()
- def extract_name(p):
- name = AIBob.extractName()
- return name
- def text_to_thought(move):
- if move == KEY_UP:
- thought = "UP"
- elif move == KEY_LEFT:
- thought = "LEFT"
- elif move == KEY_RIGHT:
- thought = "RIGHT"
- elif move == KEY_DOWN:
- thought = "DOWN"
- else:
- thought = "LEARNING..."
- return thought
- def init(evol, record, max_moves):
- curses.initscr()
- x_height = 0
- y_height = 20
- x_width = 60
- y_width = 0
- win = curses.newwin(y_height, x_width, x_height, y_width)
- win.keypad(1)
- curses.noecho()
- curses.curs_set(0)
- win.border(0)
- win.nodelay(1)
- food = [random.randint(1, 18), random.randint(1, 58)]
- win.addch(food[0], food[1], '*')
- p1_a1 = random.randint(1, 10)
- p1_a2 = random.randint(1, 10)
- p1_b1 = random.randint(1, 10)
- p1_b2 = random.randint(1, 10)
- p1_c1 = random.randint(1, 10)
- p1_c2 = random.randint(1, 10)
- snake = [[p1_a1,p1_a2], [p1_b1,p1_b2], [p1_c1,p1_c2]]
- startGame(win, food, snake, evol, record, max_moves)
- def startGame(win, food, snake, evol, record, max_moves):
- moves = 0
- score = 0
- thought = "WAKING UP..."
- name = AIBob.extractName()
- move = AIBob.makeMove(win, None, food, snake)
- moves += 1
- thought = text_to_thought(move)
-
- while move != 27:
- win.border(0)
- win.addstr(0, 4, '| Moves: '+str(moves)+' - Max: '+str(max_moves)+' | Score: '+str(score)+' - Record: '+str(record)+' |')
- win.addstr(19, 4, '| '+str(name)+' -> GENERATION: '+str(evol)+' [IDEA: '+str(thought)+'] |')
- win.timeout(150 - int((len(snake)/5) + int(len(snake)/10)%120))
- prevMove = move
- event = win.getch()
- move = AIBob.makeMove(win, prevMove, food, snake)
- moves += 1
- thought = text_to_thought(move)
- move = move if event == -1 else event
- if move != KEY_UP and move != KEY_DOWN and move != KEY_LEFT and move != KEY_RIGHT :
- move = -1
- paused = ' GAME PAUSED: PRESS -SPACEBAR- TO RESTORE'
- win.addstr(9, 9, paused)
- while move != ord(' '):
- paused = ' '
- move = win.getch()
- win.addstr(9, 9, paused)
- win.addch(food[0], food[1], '*')
- move = prevMove
- continue
- if score >= record:
- record = score
- if moves >= max_moves:
- max_moves = moves
- 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)])
- if snake[0][0] == 0:
- snake[0][0] = 18
- if snake[0][1] == 0:
- snake[0][1] = 58
- if snake[0][0] == 19:
- snake[0][0] = 1
- if snake[0][1] == 59:
- snake[0][1] = 1
- if snake[0] in snake[1:]:
- win.addstr(9, 18, ' SORRY: GAME OVER !!!')
- win.addstr(19, 4, '| PyAISnake -> MUTATION: '+str(evol)+' [IDEA: TRYING AGAIN!] |')
- if score >= record:
- record = score
- if moves >= max_moves:
- max_moves = moves -1
- event = win.getch()
- time.sleep(2)
- evol += 1
- init(evol, record, max_moves)
- if snake[0] == food:
- food = []
- score += 1
- while food == []:
- food = [random.randint(1, 18), random.randint(1, 58)]
- if food in snake: food = []
- win.addch(food[0], food[1], '*')
- else:
- last = snake.pop()
- try:
- win.addch(last[0], last[1], ' ')
- except:
- pass
- try:
- win.addch(snake[0][0], snake[0][1], '#')
- except:
- pass
- init(0, 0, 0)
|