Tetris
Bases: Environment[State, MultiDiscreteArray, Observation]
RL Environment for the game of Tetris. The environment has a grid where the player can place tetrominoes. The environment has the following characteristics:
- observation:
Observation
- grid: jax array (int32) of shape (num_rows, num_cols) representing the current state of the grid.
- tetromino: jax array (int32) of shape (4, 4) representing the current tetromino sampled from the tetromino list.
- action_mask: jax array (bool) of shape (4, num_cols). For each tetromino there are 4 rotations, each one corresponds to a line in the action_mask. Mask of the joint action space: True if the action (x_position and rotation degree) is feasible for the current tetromino and grid state.
-
action: multi discrete array of shape (2,)
- rotation_index: The degree index determines the rotation of the tetromino: 0 corresponds to 0 degrees, 1 corresponds to 90 degrees, 2 corresponds to 180 degrees, and 3 corresponds to 270 degrees.
- x_position: int between 0 and num_cols - 1 (included).
-
reward: The reward is 0 if no lines was cleared by the action and a convex function of the number of cleared lines otherwise.
-
episode termination: if the tetromino cannot be placed anymore (i.e., it hits the top of the grid).
1 2 3 4 5 6 7 8 |
|
Instantiates a Tetris
environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_rows
|
int
|
number of rows of the 2D grid. Defaults to 10. |
10
|
num_cols
|
int
|
number of columns of the 2D grid. Defaults to 10. |
10
|
time_limit
|
int
|
time_limit of an episode, i.e. number of environment steps before the episode ends. Defaults to 400. |
400
|
viewer
|
Optional[Viewer[State]]
|
|
None
|
Source code in jumanji/environments/packing/tetris/env.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
action_spec: specs.MultiDiscreteArray
cached
property
#
Returns the action spec. An action consists of two pieces of information: the amount of rotation (number of 90-degree rotations) and the x-position of the leftmost part of the tetromino.
Returns:
Type | Description |
---|---|
MultiDiscreteArray
|
The action spec, which is a |
observation_spec: specs.Spec[Observation]
cached
property
#
Specifications of the observation of the Tetris
environment.
Returns:
Type | Description |
---|---|
Spec[Observation]
|
Spec containing all the specifications for all the |
animate(states, interval=100, save_path=None)
#
Create an animation from a sequence of states.
Args:
states: sequence of State
corresponding to subsequent timesteps.
interval: delay between frames in milliseconds, default to 100.
save_path: the path where the animation file should be saved. If it is None, the plot
will not be saved.
Returns:
animation that can export to gif, mp4, or render with HTML.
Source code in jumanji/environments/packing/tetris/env.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
render(state)
#
Render the given state of the environment.
Args:
state: State
object containing the current environment state.
Source code in jumanji/environments/packing/tetris/env.py
232 233 234 235 236 237 |
|
reset(key)
#
Resets the environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
PRNGKey
|
needed for generating new tetrominoes. |
required |
Returns:
Name | Type | Description |
---|---|---|
state |
State
|
|
timestep |
TimeStep[Observation]
|
|
Source code in jumanji/environments/packing/tetris/env.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
step(state, action)
#
Run one timestep of the environment's dynamics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
State
|
|
required |
action
|
Array
|
|
required |
Returns:
Name | Type | Description |
---|---|---|
next_state |
State
|
|
next_timestep |
TimeStep[Observation]
|
|
Source code in jumanji/environments/packing/tetris/env.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|