diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2022-11-01 17:20:36 +0100 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2022-11-01 17:20:36 +0100 |
| commit | 69c5d709a459abc9e99c04de3d45e719381838bd (patch) | |
| tree | 6404246b4432cc6be6d7a45341f26fb8c7d01988 /qlearning.py | |
| parent | e386840172a60085219770c0daeb780685011b60 (diff) | |
Minor changes
Diffstat (limited to 'qlearning.py')
| -rwxr-xr-x | qlearning.py | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/qlearning.py b/qlearning.py index bbe9127..e35d778 100755 --- a/qlearning.py +++ b/qlearning.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -import sys +import sys,random import numpy as np # Import snake game @@ -69,9 +69,14 @@ def event_handler(game,event): # This come from me I do not now if it is the best way to identify a state state=2**11*snake_go_up+2**10*snake_go_right+2**9*snake_go_down+2**8*snake_go_left+2**7*apple_up+2**6*apple_right+2**5*apple_down+2**4*apple_left+2**3*obstacle_up+2**2*obstacle_right+2**1*obstacle_down+obstacle_left - - if game.snake[0][0]==10: - game.direction=6 + + + if np.max(qtable[state]) > 0: + action = np.argmax(qtable[state]) + else: + action=random.choice((12,3,6,9)) + + game.direction=action for i in range(0,10): score=game.run(event_handler=event_handler) |
