Spaces:
Build error
Build error
Update tetris_env.py
Browse files- tetris_env.py +21 -40
tetris_env.py
CHANGED
|
@@ -3,7 +3,7 @@ from gym import spaces
|
|
| 3 |
import numpy as np
|
| 4 |
import pygame
|
| 5 |
import random
|
| 6 |
-
from sandtris import Tetris
|
| 7 |
import os
|
| 8 |
|
| 9 |
colors = [
|
|
@@ -17,48 +17,36 @@ colors = [
|
|
| 17 |
]
|
| 18 |
|
| 19 |
class TetrisEnv(gym.Env):
|
| 20 |
-
"""
|
| 21 |
-
Custom Environment for Tetris game compatible with OpenAI Gym
|
| 22 |
-
"""
|
| 23 |
metadata = {'render.modes': ['human', 'rgb_array']}
|
| 24 |
|
| 25 |
def __init__(self):
|
| 26 |
super(TetrisEnv, self).__init__()
|
| 27 |
-
|
| 28 |
-
# Define action space: 0=left, 1=right, 2=rotate, 3=drop, 4=noop
|
| 29 |
self.action_space = spaces.Discrete(5)
|
| 30 |
-
|
| 31 |
-
# Observation space: 2D grid representing the game board
|
| 32 |
self.height = 20
|
| 33 |
self.width = 10
|
| 34 |
self.observation_space = spaces.Box(low=0, high=6,
|
| 35 |
shape=(self.height, self.width), dtype=np.int32)
|
| 36 |
|
| 37 |
-
|
| 38 |
self.game = Tetris(self.height, self.width)
|
| 39 |
|
| 40 |
-
|
| 41 |
self.screen = None
|
| 42 |
self.zoom = 20
|
| 43 |
self.x = 100
|
| 44 |
self.y = 60
|
| 45 |
|
| 46 |
def reset(self):
|
| 47 |
-
|
| 48 |
-
Reset the game to initial state
|
| 49 |
-
"""
|
| 50 |
self.game = Tetris(self.height, self.width)
|
| 51 |
self.game.new_figure()
|
| 52 |
return self._get_obs()
|
| 53 |
|
| 54 |
def step(self, action):
|
| 55 |
-
|
| 56 |
-
Execute one time step within the environment
|
| 57 |
-
"""
|
| 58 |
done = False
|
| 59 |
reward = 0
|
| 60 |
|
| 61 |
-
# Apply action
|
| 62 |
if action == 0:
|
| 63 |
self.game.go_side(-1) # Move left
|
| 64 |
elif action == 1:
|
|
@@ -70,14 +58,14 @@ class TetrisEnv(gym.Env):
|
|
| 70 |
elif action == 4:
|
| 71 |
pass # No operation
|
| 72 |
|
| 73 |
-
|
| 74 |
self.game.go_down()
|
| 75 |
|
| 76 |
-
|
| 77 |
-
lines_cleared = self.game.score
|
| 78 |
reward += lines_cleared * 10
|
| 79 |
|
| 80 |
-
|
| 81 |
aggregate_height = self.calculate_aggregate_height()
|
| 82 |
holes = self.calculate_holes()
|
| 83 |
bumpiness = self.calculate_bumpiness()
|
|
@@ -86,35 +74,31 @@ class TetrisEnv(gym.Env):
|
|
| 86 |
|
| 87 |
if self.game.state == "gameover":
|
| 88 |
done = True
|
| 89 |
-
reward -= 10
|
| 90 |
|
| 91 |
return self._get_obs(), reward, done, {}
|
| 92 |
|
| 93 |
def _get_obs(self):
|
| 94 |
-
|
| 95 |
-
Get the current state of the game as an observation
|
| 96 |
-
"""
|
| 97 |
if self.game.field is None:
|
| 98 |
raise ValueError("The field attribute in self.game is None.")
|
| 99 |
return np.array(self.game.field)
|
| 100 |
|
| 101 |
def render(self, mode='human'):
|
| 102 |
-
|
| 103 |
-
Render the game state as an RGB array or display it.
|
| 104 |
-
"""
|
| 105 |
if mode == 'rgb_array':
|
| 106 |
if self.screen is None:
|
| 107 |
pygame.init()
|
| 108 |
size = (self.x * 2 + self.zoom * self.width, self.y * 2 + self.zoom * self.height)
|
| 109 |
self.screen = pygame.Surface(size)
|
| 110 |
|
| 111 |
-
self.screen.fill((173, 216, 230))
|
| 112 |
|
| 113 |
-
|
| 114 |
if self.game.field is None:
|
| 115 |
raise ValueError("Game field is None.")
|
| 116 |
|
| 117 |
-
|
| 118 |
for i in range(self.game.height):
|
| 119 |
for j in range(self.game.width):
|
| 120 |
rect = pygame.Rect(self.x + self.zoom * j, self.y + self.zoom * i, self.zoom, self.zoom)
|
|
@@ -124,7 +108,7 @@ class TetrisEnv(gym.Env):
|
|
| 124 |
colors[self.game.field[i][j]],
|
| 125 |
rect.inflate(-2, -2))
|
| 126 |
|
| 127 |
-
|
| 128 |
if self.game.figure is not None:
|
| 129 |
for i in range(4):
|
| 130 |
for j in range(4):
|
|
@@ -137,7 +121,7 @@ class TetrisEnv(gym.Env):
|
|
| 137 |
colors[self.game.figure.color],
|
| 138 |
rect.inflate(-2, -2))
|
| 139 |
|
| 140 |
-
|
| 141 |
return pygame.surfarray.array3d(self.screen)
|
| 142 |
|
| 143 |
elif mode == 'human':
|
|
@@ -147,9 +131,9 @@ class TetrisEnv(gym.Env):
|
|
| 147 |
self.screen = pygame.display.set_mode(size)
|
| 148 |
pygame.display.set_caption("Tetris RL")
|
| 149 |
|
| 150 |
-
self.screen.fill((173, 216, 230))
|
| 151 |
|
| 152 |
-
|
| 153 |
for i in range(self.game.height):
|
| 154 |
for j in range(self.game.width):
|
| 155 |
rect = pygame.Rect(self.x + self.zoom * j, self.y + self.zoom * i, self.zoom, self.zoom)
|
|
@@ -159,7 +143,7 @@ class TetrisEnv(gym.Env):
|
|
| 159 |
colors[self.game.field[i][j]],
|
| 160 |
rect.inflate(-2, -2))
|
| 161 |
|
| 162 |
-
|
| 163 |
if self.game.figure is not None:
|
| 164 |
for i in range(4):
|
| 165 |
for j in range(4):
|
|
@@ -175,14 +159,11 @@ class TetrisEnv(gym.Env):
|
|
| 175 |
pygame.display.flip()
|
| 176 |
|
| 177 |
def close(self):
|
| 178 |
-
|
| 179 |
-
Clean up resources
|
| 180 |
-
"""
|
| 181 |
if self.screen is not None:
|
| 182 |
pygame.display.quit()
|
| 183 |
pygame.quit()
|
| 184 |
|
| 185 |
-
# Reward shaping helper functions
|
| 186 |
def calculate_aggregate_height(self):
|
| 187 |
heights = [0 for _ in range(self.width)]
|
| 188 |
for j in range(self.width):
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import pygame
|
| 5 |
import random
|
| 6 |
+
from sandtris import Tetris
|
| 7 |
import os
|
| 8 |
|
| 9 |
colors = [
|
|
|
|
| 17 |
]
|
| 18 |
|
| 19 |
class TetrisEnv(gym.Env):
|
|
|
|
|
|
|
|
|
|
| 20 |
metadata = {'render.modes': ['human', 'rgb_array']}
|
| 21 |
|
| 22 |
def __init__(self):
|
| 23 |
super(TetrisEnv, self).__init__()
|
|
|
|
|
|
|
| 24 |
self.action_space = spaces.Discrete(5)
|
|
|
|
|
|
|
| 25 |
self.height = 20
|
| 26 |
self.width = 10
|
| 27 |
self.observation_space = spaces.Box(low=0, high=6,
|
| 28 |
shape=(self.height, self.width), dtype=np.int32)
|
| 29 |
|
| 30 |
+
|
| 31 |
self.game = Tetris(self.height, self.width)
|
| 32 |
|
| 33 |
+
|
| 34 |
self.screen = None
|
| 35 |
self.zoom = 20
|
| 36 |
self.x = 100
|
| 37 |
self.y = 60
|
| 38 |
|
| 39 |
def reset(self):
|
| 40 |
+
|
|
|
|
|
|
|
| 41 |
self.game = Tetris(self.height, self.width)
|
| 42 |
self.game.new_figure()
|
| 43 |
return self._get_obs()
|
| 44 |
|
| 45 |
def step(self, action):
|
| 46 |
+
|
|
|
|
|
|
|
| 47 |
done = False
|
| 48 |
reward = 0
|
| 49 |
|
|
|
|
| 50 |
if action == 0:
|
| 51 |
self.game.go_side(-1) # Move left
|
| 52 |
elif action == 1:
|
|
|
|
| 58 |
elif action == 4:
|
| 59 |
pass # No operation
|
| 60 |
|
| 61 |
+
|
| 62 |
self.game.go_down()
|
| 63 |
|
| 64 |
+
|
| 65 |
+
lines_cleared = self.game.score
|
| 66 |
reward += lines_cleared * 10
|
| 67 |
|
| 68 |
+
|
| 69 |
aggregate_height = self.calculate_aggregate_height()
|
| 70 |
holes = self.calculate_holes()
|
| 71 |
bumpiness = self.calculate_bumpiness()
|
|
|
|
| 74 |
|
| 75 |
if self.game.state == "gameover":
|
| 76 |
done = True
|
| 77 |
+
reward -= 10
|
| 78 |
|
| 79 |
return self._get_obs(), reward, done, {}
|
| 80 |
|
| 81 |
def _get_obs(self):
|
| 82 |
+
|
|
|
|
|
|
|
| 83 |
if self.game.field is None:
|
| 84 |
raise ValueError("The field attribute in self.game is None.")
|
| 85 |
return np.array(self.game.field)
|
| 86 |
|
| 87 |
def render(self, mode='human'):
|
| 88 |
+
|
|
|
|
|
|
|
| 89 |
if mode == 'rgb_array':
|
| 90 |
if self.screen is None:
|
| 91 |
pygame.init()
|
| 92 |
size = (self.x * 2 + self.zoom * self.width, self.y * 2 + self.zoom * self.height)
|
| 93 |
self.screen = pygame.Surface(size)
|
| 94 |
|
| 95 |
+
self.screen.fill((173, 216, 230))
|
| 96 |
|
| 97 |
+
|
| 98 |
if self.game.field is None:
|
| 99 |
raise ValueError("Game field is None.")
|
| 100 |
|
| 101 |
+
|
| 102 |
for i in range(self.game.height):
|
| 103 |
for j in range(self.game.width):
|
| 104 |
rect = pygame.Rect(self.x + self.zoom * j, self.y + self.zoom * i, self.zoom, self.zoom)
|
|
|
|
| 108 |
colors[self.game.field[i][j]],
|
| 109 |
rect.inflate(-2, -2))
|
| 110 |
|
| 111 |
+
|
| 112 |
if self.game.figure is not None:
|
| 113 |
for i in range(4):
|
| 114 |
for j in range(4):
|
|
|
|
| 121 |
colors[self.game.figure.color],
|
| 122 |
rect.inflate(-2, -2))
|
| 123 |
|
| 124 |
+
|
| 125 |
return pygame.surfarray.array3d(self.screen)
|
| 126 |
|
| 127 |
elif mode == 'human':
|
|
|
|
| 131 |
self.screen = pygame.display.set_mode(size)
|
| 132 |
pygame.display.set_caption("Tetris RL")
|
| 133 |
|
| 134 |
+
self.screen.fill((173, 216, 230))
|
| 135 |
|
| 136 |
+
|
| 137 |
for i in range(self.game.height):
|
| 138 |
for j in range(self.game.width):
|
| 139 |
rect = pygame.Rect(self.x + self.zoom * j, self.y + self.zoom * i, self.zoom, self.zoom)
|
|
|
|
| 143 |
colors[self.game.field[i][j]],
|
| 144 |
rect.inflate(-2, -2))
|
| 145 |
|
| 146 |
+
|
| 147 |
if self.game.figure is not None:
|
| 148 |
for i in range(4):
|
| 149 |
for j in range(4):
|
|
|
|
| 159 |
pygame.display.flip()
|
| 160 |
|
| 161 |
def close(self):
|
| 162 |
+
|
|
|
|
|
|
|
| 163 |
if self.screen is not None:
|
| 164 |
pygame.display.quit()
|
| 165 |
pygame.quit()
|
| 166 |
|
|
|
|
| 167 |
def calculate_aggregate_height(self):
|
| 168 |
heights = [0 for _ in range(self.width)]
|
| 169 |
for j in range(self.width):
|