Skip to content

Commit

Permalink
Created the function to play the game with pretrained bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightbro committed Aug 5, 2024
1 parent 6ddb413 commit d2f1be9
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions snake/play_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' # needs to be set to 0 to avoid an error with the model loading

import numpy as np
import tensorflow as tf
from snake_game import SnakeGame
from agent import DQNAgent

def play_with_bot(model_path='dqn_model.keras'):
game = SnakeGame()
agent = DQNAgent()

# Error with loading the model
agent.model = tf.keras.models.load_model(model_path, custom_objects={'mse': tf.keras.losses.MeanSquaredError()})

game_over = False
state = np.reshape(game.reset(), [1, 1, 6])

while not game_over:
action = agent.act(state)
next_state, _, game_over = game.step(action)
state = np.reshape(next_state, [1, 1, 6])
game.render()
game.clock.tick(1000) # faster training speed

game.close()

if __name__ == "__main__":
play_with_bot()

0 comments on commit d2f1be9

Please sign in to comment.