Repository navigation
Expand file tree
/
Copy pathtest_game_runner.py
More file actions
39 lines (37 loc) · 1.55 KB
/
Copy pathtest_game_runner.py
File metadata and controls
39 lines (37 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import threading
import queue
import time
from snake_game import SnakeGameAI
from agent_core import Agent
from game_runner import run_game_thread
def test_snake_game_loop():
game_command_queue = queue.Queue()
plot_command_queue = queue.Queue()
plot_scores = []
plot_records = []
plot_mean_scores = []
shutdown_flag = threading.Event()
game = SnakeGameAI()
agent = Agent(game.get_state_shape(), game.get_action_space_size(), game.get_state_is_image())
def thread_wrapper():
try:
run_game_thread(game, agent, game_command_queue, plot_command_queue, plot_scores, plot_records, plot_mean_scores, shutdown_flag)
except Exception as e:
with open('debug_log.txt', 'a') as f:
f.write(f"[Test] Exception in game thread: {e}\n")
game_thread = threading.Thread(target=thread_wrapper)
with open('debug_log.txt', 'a') as f:
f.write("[Test] Starting game thread...\n")
game_thread.start()
with open('debug_log.txt', 'a') as f:
f.write(f"[Test] Game thread started. Alive: {game_thread.is_alive()}\n")
time.sleep(5) # Let the game run for 5 seconds
with open('debug_log.txt', 'a') as f:
f.write(f"[Test] Setting shutdown flag.\n")
shutdown_flag.set()
game_thread.join()
with open('debug_log.txt', 'a') as f:
f.write(f"[Test] Game thread joined. Alive: {game_thread.is_alive()} Shutdown flag: {shutdown_flag.is_set()}\n")
print("Test complete. Check debug_log.txt for output.")
if __name__ == "__main__":
test_snake_game_loop()