-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris_reward.py
47 lines (35 loc) · 1.23 KB
/
tetris_reward.py
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
40
41
42
43
44
45
46
#Collaborators: Max Boonbandansook
#Primary goal of this script is to run pygame/tetris in scripts (headless mode no windows)
#Limitation: halts the computation once reward gets below -100
import pygame
import sys
import os
from tetris_engine import *
from game_agent import *
import statistics
os.environ["SDL_VIDEODRIVER"] = "dummy"
def game_start(AGENT_TYPE):
totalrewards = []
for i in range(3):
pygame.init()
game = GameState()
agent = AGENT_TYPE()
rewards = []
reward = 0
while not game.stop:
action = Action.IDLE
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
elif event.type == pygame.KEYDOWN:
if event.key in action_lookup:
action = action_lookup[event.key]
game.update(agent.get_move(game))
board = game.game_board
reward = game.get_reward()
rewards.append(reward)
totalrewards.append(statistics.mean(rewards))
return totalrewards
print("BruteAgent rewards",game_start(BruteAgent))
print("RandomAgent rewards",game_start(RandomAgent))