Minor changes
This commit is contained in:
parent
5b3efc4a21
commit
37308a0d4c
3 changed files with 8207 additions and 11 deletions
|
@ -131,10 +131,9 @@ class QTable:
|
|||
perf=0
|
||||
last_state=None
|
||||
last_action=None
|
||||
game=Snake(length=4,fps=300,startat=(10,10))
|
||||
game=Snake(length=1,fps=300,startat=(random.randint(0,29),random.randint(0,29)))
|
||||
qtable=QTable("qtable.txt")
|
||||
|
||||
for i in range(0,10000):
|
||||
while True:
|
||||
result=0
|
||||
stuck=0
|
||||
state=qtable.get_state(game)
|
||||
|
@ -144,7 +143,7 @@ for i in range(0,10000):
|
|||
new_state=qtable.get_state(game)
|
||||
|
||||
# Agent is stuck
|
||||
if stuck>=game.grid_width*game.grid_height:
|
||||
if stuck>=(game.grid_width*game.grid_height)/2:
|
||||
game.new_game()
|
||||
break
|
||||
|
||||
|
|
8192
qtable.txt
Normal file
8192
qtable.txt
Normal file
File diff suppressed because it is too large
Load diff
11
snake.py
11
snake.py
|
@ -129,7 +129,7 @@ class Snake:
|
|||
self.draw_infos()
|
||||
pygame.display.flip()
|
||||
|
||||
def play(self,direction):
|
||||
def play(self,direction,handle_quit=True):
|
||||
"""
|
||||
Play using wall clock directions (12=up, 3=right, 6=down and 9=left)
|
||||
"""
|
||||
|
@ -151,6 +151,7 @@ class Snake:
|
|||
self.draw()
|
||||
self.clock.tick(self.fps)
|
||||
# Ensure we not quit
|
||||
if handle_quit:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
|
@ -190,7 +191,10 @@ class Snake:
|
|||
while True:
|
||||
# Check inputs
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_LEFT and self.direction != 3:
|
||||
self.direction=9
|
||||
break
|
||||
|
@ -204,5 +208,6 @@ class Snake:
|
|||
self.direction=6
|
||||
break
|
||||
|
||||
if self.play(self.direction) <0:
|
||||
if self.play(self.direction,handle_quit=False) <0:
|
||||
break
|
||||
|
Loading…
Add table
Reference in a new issue