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
|
perf=0
|
||||||
last_state=None
|
last_state=None
|
||||||
last_action=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")
|
qtable=QTable("qtable.txt")
|
||||||
|
while True:
|
||||||
for i in range(0,10000):
|
|
||||||
result=0
|
result=0
|
||||||
stuck=0
|
stuck=0
|
||||||
state=qtable.get_state(game)
|
state=qtable.get_state(game)
|
||||||
|
@ -144,7 +143,7 @@ for i in range(0,10000):
|
||||||
new_state=qtable.get_state(game)
|
new_state=qtable.get_state(game)
|
||||||
|
|
||||||
# Agent is stuck
|
# Agent is stuck
|
||||||
if stuck>=game.grid_width*game.grid_height:
|
if stuck>=(game.grid_width*game.grid_height)/2:
|
||||||
game.new_game()
|
game.new_game()
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
8192
qtable.txt
Normal file
8192
qtable.txt
Normal file
File diff suppressed because it is too large
Load diff
19
snake.py
19
snake.py
|
@ -129,7 +129,7 @@ class Snake:
|
||||||
self.draw_infos()
|
self.draw_infos()
|
||||||
pygame.display.flip()
|
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)
|
Play using wall clock directions (12=up, 3=right, 6=down and 9=left)
|
||||||
"""
|
"""
|
||||||
|
@ -151,10 +151,11 @@ class Snake:
|
||||||
self.draw()
|
self.draw()
|
||||||
self.clock.tick(self.fps)
|
self.clock.tick(self.fps)
|
||||||
# Ensure we not quit
|
# Ensure we not quit
|
||||||
for event in pygame.event.get():
|
if handle_quit:
|
||||||
if event.type == pygame.QUIT:
|
for event in pygame.event.get():
|
||||||
pygame.quit()
|
if event.type == pygame.QUIT:
|
||||||
sys.exit()
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
return(code)
|
return(code)
|
||||||
|
|
||||||
def play2(self,direction):
|
def play2(self,direction):
|
||||||
|
@ -190,7 +191,10 @@ class Snake:
|
||||||
while True:
|
while True:
|
||||||
# Check inputs
|
# Check inputs
|
||||||
for event in pygame.event.get():
|
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:
|
if event.key == pygame.K_LEFT and self.direction != 3:
|
||||||
self.direction=9
|
self.direction=9
|
||||||
break
|
break
|
||||||
|
@ -204,5 +208,6 @@ class Snake:
|
||||||
self.direction=6
|
self.direction=6
|
||||||
break
|
break
|
||||||
|
|
||||||
if self.play(self.direction) <0:
|
if self.play(self.direction,handle_quit=False) <0:
|
||||||
break
|
break
|
||||||
|
|
Loading…
Add table
Reference in a new issue