summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2022-11-01 14:48:41 +0100
committerLoic Guegan <manzerbredes@mailbox.org>2022-11-01 14:48:41 +0100
commitb3b8478a9cd1799cfa73c864ac994c5d54a78369 (patch)
treef2732b4c75bf3a86edede896527160163b086ee7
parenta98fcc25ffa986f9f6fbb45dcfb6e68366ca6678 (diff)
Minor changes
-rwxr-xr-xqlearning.py49
-rwxr-xr-xsnake.py10
2 files changed, 47 insertions, 12 deletions
diff --git a/qlearning.py b/qlearning.py
index 7834f6f..2273b7e 100755
--- a/qlearning.py
+++ b/qlearning.py
@@ -8,16 +8,61 @@ from snake import Snake
# Setup QTable
-qtable=np.zeros((16, 4))
+# Boolean features:
+# Snake go up?
+# Snake go right?
+# Snake go down?
+# Snake go left?
+# Apple at up?
+# Apple at right?
+# Apple at down?
+# Apple at left?
+# Obstacle at up?
+# Obstacle at right?
+# Obstacle at down?
+# Obstacle at left?
+##### Totally 12 boolean features so 2^12=4096 states
+##### Totally 4 actions for the AI (up, right,down,left)
+##### Totally 4*2^12 thus 16 384 table entries
+
+qtable=np.zeros((4096, 4))
game=Snake()
+def isWall(h,game):
+ if h[0]<0 or h[1]<0 or h[0] >= game.grid_width or h[1] >= game.grid_height:
+ return(True)
+ return(False)
+
+
def event_handler(game):
+ h=game.snake[0]
+ left=(h[0]-1,h[1])
+ right=(h[0]+1,h[1])
+ up=(h[0],h[1]-1)
+ down=(h[0],h[1]+1)
+ a=game.apple
+
+ snake_go_up=(game.direction==12)
+ snake_go_right=(game.direction==3)
+ snake_go_down=(game.direction==6)
+ snake_go_left=(game.direction==9)
+
+ apple_up=(up==a)
+ apple_right=(right==a)
+ apple_down=(down==a)
+ apple_left=(left==a)
+
+ obstacle_up=(up in game.snake or isWall(up, game))
+ obstacle_right=(right in game.snake or isWall(right, game))
+ obstacle_down=(down in game.snake or isWall(down, game))
+ obstacle_left=(left in game.snake or isWall(left, game))
+
if game.snake[0][0]==10:
game.direction=6
for i in range(0,10):
- score=game.run()
+ score=game.run(event_handler=event_handler)
print("Game ended with "+str(score)) \ No newline at end of file
diff --git a/snake.py b/snake.py
index 0a37ae4..e5db997 100755
--- a/snake.py
+++ b/snake.py
@@ -165,13 +165,3 @@ class Snake:
clock.tick(self.fps)
return(self.score)
-
-game=Snake()
-
-def event_handler(game):
- if game.snake[0][0]==10:
- game.direction=6
-
-for i in range(0,10):
- score=game.run()
- print("Game ended with "+str(score)) \ No newline at end of file