Skip to content

Commit

Permalink
Deploying to gh-pages from @ aa3f050 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-We committed May 3, 2024
1 parent 2e3d5ee commit cb305a0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
33 changes: 23 additions & 10 deletions _modules/tetris_gymnasium/envs/tetris/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ <h1>Source code for tetris_gymnasium.envs.tetris</h1><div class="highlight"><pre
<span class="kn">from</span> <span class="nn">gymnasium.core</span> <span class="kn">import</span> <span class="n">ActType</span><span class="p">,</span> <span class="n">RenderFrame</span>
<span class="kn">from</span> <span class="nn">gymnasium.spaces</span> <span class="kn">import</span> <span class="n">Box</span><span class="p">,</span> <span class="n">Discrete</span>

<span class="kn">from</span> <span class="nn">tetris_gymnasium.components.holder</span> <span class="kn">import</span> <span class="n">Holder</span>
<span class="kn">from</span> <span class="nn">tetris_gymnasium.components.randomizer</span> <span class="kn">import</span> <span class="n">BagRandomizer</span><span class="p">,</span> <span class="n">Randomizer</span>
<span class="kn">from</span> <span class="nn">tetris_gymnasium.util.tetrominoes</span> <span class="kn">import</span> <span class="n">STANDARD_COLORS</span><span class="p">,</span> <span class="n">STANDARD_TETROMINOES</span>

Expand All @@ -263,6 +264,7 @@ <h1>Source code for tetris_gymnasium.envs.tetris</h1><div class="highlight"><pre
<span class="s2">&quot;rotate_clockwise&quot;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
<span class="s2">&quot;rotate_counterclockwise&quot;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
<span class="s2">&quot;hard_drop&quot;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span>
<span class="s2">&quot;swap&quot;</span><span class="p">:</span> <span class="mi">6</span><span class="p">,</span>
<span class="p">}</span>


Expand All @@ -283,7 +285,8 @@ <h1>Source code for tetris_gymnasium.envs.tetris</h1><div class="highlight"><pre
<span class="n">width</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="n">height</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span>
<span class="n">tetrominoes</span><span class="o">=</span><span class="n">STANDARD_TETROMINOES</span><span class="p">,</span>
<span class="n">randomizer</span><span class="o">=</span><span class="n">BagRandomizer</span><span class="p">,</span>
<span class="n">randomizer</span><span class="p">:</span> <span class="n">Randomizer</span> <span class="o">=</span> <span class="n">BagRandomizer</span><span class="p">,</span>
<span class="n">holder</span><span class="p">:</span> <span class="n">Holder</span> <span class="o">=</span> <span class="n">Holder</span><span class="p">,</span>
<span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Creates a new Tetris environment.</span>

Expand All @@ -293,13 +296,15 @@ <h1>Source code for tetris_gymnasium.envs.tetris</h1><div class="highlight"><pre
<span class="sd"> height: The height of the game board.</span>
<span class="sd"> tetrominoes: A list of numpy arrays representing the tetrominoes to use.</span>
<span class="sd"> randomizer: The randomizer to use for selecting tetrominoes.</span>
<span class="sd"> holder: The holder to use for storing tetrominoes.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># Dimensions</span>
<span class="bp">self</span><span class="o">.</span><span class="n">height</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">height</span>
<span class="bp">self</span><span class="o">.</span><span class="n">width</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">width</span>

<span class="c1"># Tetrominoes &amp; Schedule</span>
<span class="bp">self</span><span class="o">.</span><span class="n">randomizer</span><span class="p">:</span> <span class="n">Randomizer</span> <span class="o">=</span> <span class="n">randomizer</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">tetrominoes</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">holder</span> <span class="o">=</span> <span class="n">holder</span>
<span class="bp">self</span><span class="o">.</span><span class="n">tetrominoes</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]</span> <span class="o">=</span> <span class="n">tetrominoes</span>
<span class="bp">self</span><span class="o">.</span><span class="n">active_tetromino</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tetrominoes</span><span class="p">[</span>
<span class="bp">self</span><span class="o">.</span><span class="n">randomizer</span><span class="o">.</span><span class="n">get_next_tetromino</span><span class="p">()</span>
Expand Down Expand Up @@ -330,15 +335,7 @@ <h1>Source code for tetris_gymnasium.envs.tetris</h1><div class="highlight"><pre
<span class="k">assert</span> <span class="n">render_mode</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">render_mode</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;render_modes&quot;</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">render_mode</span> <span class="o">=</span> <span class="n">render_mode</span>

<span class="c1"># &quot;&quot;&quot;</span>
<span class="c1"># If human-rendering is used, `self.window` will be a reference</span>
<span class="c1"># to the window that we draw to. `self.clock` will be a clock that is used</span>
<span class="c1"># to ensure that the environment is rendered at the correct framerate in</span>
<span class="c1"># human-mode. They will remain `None` until human-mode is used for the</span>
<span class="c1"># first time.</span>
<span class="c1"># &quot;&quot;&quot;</span>
<span class="c1"># self.window = None</span>
<span class="c1"># self.clock = None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_swapped</span> <span class="o">=</span> <span class="kc">False</span>

<div class="viewcode-block" id="Tetris.step">
<a class="viewcode-back" href="../../../../environments/tetris/#tetris_gymnasium.envs.tetris.Tetris.step">[docs]</a>
Expand Down Expand Up @@ -381,6 +378,17 @@ <h1>Source code for tetris_gymnasium.envs.tetris</h1><div class="highlight"><pre
<span class="bp">self</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">active_tetromino</span><span class="p">,</span> <span class="kc">False</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span>
<span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">active_tetromino</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">active_tetromino</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">action</span> <span class="o">==</span> <span class="n">ACTIONS</span><span class="p">[</span><span class="s2">&quot;swap&quot;</span><span class="p">]:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_swapped</span><span class="p">:</span>
<span class="c1"># Swap the active tetromino with the one in the holder</span>
<span class="bp">self</span><span class="o">.</span><span class="n">active_tetromino</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">holder</span><span class="o">.</span><span class="n">swap</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">active_tetromino</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_swapped</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">active_tetromino</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="c1"># If the holder is empty, spawn the next tetromino</span>
<span class="c1"># No need for collision check, as the holder is only empty at the start</span>
<span class="bp">self</span><span class="o">.</span><span class="n">spawn_tetromino</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">reset_tetromino_position</span><span class="p">()</span>
<span class="k">elif</span> <span class="n">action</span> <span class="o">==</span> <span class="n">ACTIONS</span><span class="p">[</span><span class="s2">&quot;hard_drop&quot;</span><span class="p">]:</span>
<span class="c1"># 1. Drop the tetromino and lock it in place</span>
<span class="bp">self</span><span class="o">.</span><span class="n">drop_active_tetromino</span><span class="p">()</span>
Expand All @@ -392,6 +400,9 @@ <h1>Source code for tetris_gymnasium.envs.tetris</h1><div class="highlight"><pre
<span class="k">if</span> <span class="n">game_over</span><span class="p">:</span>
<span class="n">reward</span> <span class="o">=</span> <span class="n">REWARDS</span><span class="p">[</span><span class="s2">&quot;game_over&quot;</span><span class="p">]</span>

<span class="c1"># 3. Reset the swap flag (agent can swap once per tetromino)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">has_swapped</span> <span class="o">=</span> <span class="kc">False</span>

<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_obs</span><span class="p">(),</span> <span class="n">reward</span><span class="p">,</span> <span class="n">game_over</span><span class="p">,</span> <span class="n">truncated</span><span class="p">,</span> <span class="p">{}</span></div>


Expand Down Expand Up @@ -420,6 +431,8 @@ <h1>Source code for tetris_gymnasium.envs.tetris</h1><div class="highlight"><pre
<span class="bp">self</span><span class="o">.</span><span class="n">active_tetromino</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tetrominoes</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">randomizer</span><span class="o">.</span><span class="n">get_next_tetromino</span><span class="p">()]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">reset_tetromino_position</span><span class="p">()</span>

<span class="bp">self</span><span class="o">.</span><span class="n">has_swapped</span> <span class="o">=</span> <span class="kc">False</span>

<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_obs</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_info</span><span class="p">()</span></div>


Expand Down
2 changes: 1 addition & 1 deletion environments/tetris/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
<h1>Tetris<a class="headerlink" href="#tetris" title="Link to this heading">#</a></h1>
<dl class="py class">
<dt class="sig sig-object py" id="tetris_gymnasium.envs.tetris.Tetris">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tetris_gymnasium.envs.tetris.</span></span><span class="sig-name descname"><span class="pre">Tetris</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">render_mode=None,</span> <span class="pre">width=10,</span> <span class="pre">height=20,</span> <span class="pre">tetrominoes=[array([[0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[2,</span> <span class="pre">2,</span> <span class="pre">2,</span> <span class="pre">2],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[3,</span> <span class="pre">3],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[3,</span> <span class="pre">3]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[0,</span> <span class="pre">4,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[4,</span> <span class="pre">4,</span> <span class="pre">4],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[0,</span> <span class="pre">5,</span> <span class="pre">5],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[5,</span> <span class="pre">5,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[6,</span> <span class="pre">6,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">6,</span> <span class="pre">6],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[7,</span> <span class="pre">0,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[7,</span> <span class="pre">7,</span> <span class="pre">7],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[0,</span> <span class="pre">0,</span> <span class="pre">8],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[8,</span> <span class="pre">8,</span> <span class="pre">8],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8)],</span> <span class="pre">randomizer=&lt;class</span> <span class="pre">'tetris_gymnasium.components.randomizer.BagRandomizer'&gt;</span></em><span class="sig-paren">)</span><a class="reference internal" href="../../_modules/tetris_gymnasium/envs/tetris/#Tetris"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#tetris_gymnasium.envs.tetris.Tetris" title="Link to this definition">#</a></dt>
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tetris_gymnasium.envs.tetris.</span></span><span class="sig-name descname"><span class="pre">Tetris</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">render_mode=None,</span> <span class="pre">width=10,</span> <span class="pre">height=20,</span> <span class="pre">tetrominoes=[array([[0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[2,</span> <span class="pre">2,</span> <span class="pre">2,</span> <span class="pre">2],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[3,</span> <span class="pre">3],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[3,</span> <span class="pre">3]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[0,</span> <span class="pre">4,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[4,</span> <span class="pre">4,</span> <span class="pre">4],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[0,</span> <span class="pre">5,</span> <span class="pre">5],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[5,</span> <span class="pre">5,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[6,</span> <span class="pre">6,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">6,</span> <span class="pre">6],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[7,</span> <span class="pre">0,</span> <span class="pre">0],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[7,</span> <span class="pre">7,</span> <span class="pre">7],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8),</span> <span class="pre">array([[0,</span> <span class="pre">0,</span> <span class="pre">8],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[8,</span> <span class="pre">8,</span> <span class="pre">8],</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="pre">[0,</span> <span class="pre">0,</span> <span class="pre">0]],</span> <span class="pre">dtype=uint8)],</span> <span class="pre">randomizer:</span> <span class="pre">~tetris_gymnasium.components.randomizer.Randomizer</span> <span class="pre">=</span> <span class="pre">&lt;class</span> <span class="pre">'tetris_gymnasium.components.randomizer.BagRandomizer'&gt;,</span> <span class="pre">holder:</span> <span class="pre">~tetris_gymnasium.components.holder.Holder</span> <span class="pre">=</span> <span class="pre">&lt;class</span> <span class="pre">'tetris_gymnasium.components.holder.Holder'&gt;</span></em><span class="sig-paren">)</span><a class="reference internal" href="../../_modules/tetris_gymnasium/envs/tetris/#Tetris"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#tetris_gymnasium.envs.tetris.Tetris" title="Link to this definition">#</a></dt>
<dd><p>Tetris environment for Gymnasium.</p>
</dd></dl>

Expand Down
Loading

0 comments on commit cb305a0

Please sign in to comment.