Browse Source

Coroutines

pull/79/head
Jure Šorn 4 years ago
parent
commit
8f520620dd
2 changed files with 4 additions and 4 deletions
  1. 4
      README.md
  2. 4
      index.html

4
README.md

@ -2306,9 +2306,9 @@ async def human_controller(screen, moves):
async def model(moves, state, height, width):
while state['*'] not in {p for id_, p in state.items() if id_ != '*'}:
id_, d = await moves.get()
p = state[id_]
p = state[id_]
deltas = {D.n: P(0, -1), D.e: P(1, 0), D.s: P(0, 1), D.w: P(-1, 0)}
new_p = P(p.x + deltas[d].x, p.y + deltas[d].y)
new_p = P(p.x + deltas[d].x, p.y + deltas[d].y)
if 0 <= new_p.x < width-1 and 0 <= new_p.y < height:
state[id_] = new_p

4
index.html

@ -2014,9 +2014,9 @@ D = enum.Enum(<span class="hljs-string">'D'</span>, <span class="hljs-string">'n
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">model</span><span class="hljs-params">(moves, state, height, width)</span>:</span>
<span class="hljs-keyword">while</span> state[<span class="hljs-string">'*'</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> {p <span class="hljs-keyword">for</span> id_, p <span class="hljs-keyword">in</span> state.items() <span class="hljs-keyword">if</span> id_ != <span class="hljs-string">'*'</span>}:
id_, d = <span class="hljs-keyword">await</span> moves.get()
p = state[id_]
p = state[id_]
deltas = {D.n: P(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>), D.e: P(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), D.s: P(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>), D.w: P(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>)}
new_p = P(p.x + deltas[d].x, p.y + deltas[d].y)
new_p = P(p.x + deltas[d].x, p.y + deltas[d].y)
<span class="hljs-keyword">if</span> <span class="hljs-number">0</span> &lt;= new_p.x &lt; width<span class="hljs-number">-1</span> <span class="hljs-keyword">and</span> <span class="hljs-number">0</span> &lt;= new_p.y &lt; height:
state[id_] = new_p

Loading…
Cancel
Save