@@ -124,14 +124,14 @@ const MATCH_EXAMPLE =
124
124
const COROUTINES =
125
125
'<span class="hljs-keyword">import</span> asyncio, collections, curses, curses.textpad, enum, random, time\n' +
126
126
'\n' +
127
- 'P = collections.namedtuple(<span class="hljs-string">\'P\'</span>, <span class="hljs-string">\'x y\'</span>) <span class="hljs-comment"># Position</span>\n' +
128
- 'D = enum.Enum(<span class="hljs-string">\'D\'</span>, <span class="hljs-string">\'n e s w\'</span>) <span class="hljs-comment"># Direction</span>\n' +
129
- 'W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>\n' +
127
+ 'P = collections.namedtuple(<span class="hljs-string">\'P\'</span>, <span class="hljs-string">\'x y\'</span>) <span class="hljs-comment"># Position</span>\n' +
128
+ 'D = enum.Enum(<span class="hljs-string">\'D\'</span>, <span class="hljs-string">\'n e s w\'</span>) <span class="hljs-comment"># Direction</span>\n' +
129
+ 'W, H = <span class="hljs-number">15</span>, <span class="hljs-number">7</span> <span class="hljs-comment"># Width, Height</span>\n' +
130
130
'\n' +
131
131
'<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">(screen)</span>:</span>\n' +
132
- ' curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>\n' +
133
- ' screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>\n' +
134
- ' asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>\n' +
132
+ ' curses.curs_set(<span class="hljs-number">0</span>) <span class="hljs-comment"># Makes cursor invisible.</span>\n' +
133
+ ' screen.nodelay(<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Makes getch() non-blocking.</span>\n' +
134
+ ' asyncio.run(main_coroutine(screen)) <span class="hljs-comment"># Starts running asyncio code.</span>\n' +
135
135
'\n' +
136
136
'<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main_coroutine</span><span class="hljs-params">(screen)</span>:</span>\n' +
137
137
' moves = asyncio.Queue()\n' +
@@ -150,37 +150,29 @@ const COROUTINES =
150
150
'<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">human_controller</span><span class="hljs-params">(screen, moves)</span>:</span>\n' +
151
151
' <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:\n' +
152
152
' key_mappings = {<span class="hljs-number">258</span>: D.s, <span class="hljs-number">259</span>: D.n, <span class="hljs-number">260</span>: D.w, <span class="hljs-number">261</span>: D.e}\n' +
153
- ' ch = screen.getch()\n' +
154
- ' <span class="hljs-keyword">if</span> d := key_mappings.get(ch):\n' +
153
+ ' <span class="hljs-keyword">if</span> d := key_mappings.get(screen.getch()):\n' +
155
154
' moves.put_nowait((<span class="hljs-string">\'*\'</span>, d))\n' +
156
155
' <span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)\n' +
157
156
'\n' +
158
157
'<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)</span>:</span>\n' +
159
158
' <span class="hljs-keyword">while</span> state[<span class="hljs-string">\'*\'</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (state[id_] <span class="hljs-keyword">for</span> id_ <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)):\n' +
160
159
' id_, d = <span class="hljs-keyword">await</span> moves.get()\n' +
161
- ' x, y = state[id_]\n' +
162
160
' 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>)}\n' +
163
- ' dx, dy = deltas[d]\n' +
164
- ' state[id_] = P((x + dx) % W, (y + dy) % H)\n' +
161
+ ' state[id_] = P((state[id_].x + deltas[d].x) % W, (state[id_].y + deltas[d].y) % H)\n' +
165
162
'\n' +
166
163
'<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">view</span><span class="hljs-params">(state, screen)</span>:</span>\n' +
167
164
' offset = P(curses.COLS//<span class="hljs-number">2</span> - W//<span class="hljs-number">2</span>, curses.LINES//<span class="hljs-number">2</span> - H//<span class="hljs-number">2</span>)\n' +
168
165
' <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:\n' +
169
166
' screen.erase()\n' +
170
167
' curses.textpad.rectangle(screen, offset.y-<span class="hljs-number">1</span>, offset.x-<span class="hljs-number">1</span>, offset.y+H, offset.x+W)\n' +
171
168
' <span class="hljs-keyword">for</span> id_, p <span class="hljs-keyword">in</span> state.items():\n' +
172
- ' screen.addstr(\n' +
173
- ' offset.y + (p.y - state[<span class="hljs-string">\'*\'</span>].y + H//<span class="hljs-number">2</span>) % H,\n' +
174
- ' offset.x + (p.x - state[<span class="hljs-string">\'*\'</span>].x + W//<span class="hljs-number">2</span>) % W,\n' +
175
- ' str(id_)\n' +
176
- ' )\n' +
169
+ ' screen.addstr(offset.y + (p.y - state[<span class="hljs-string">\'*\'</span>].y + H//<span class="hljs-number">2</span>) % H,\n' +
170
+ ' offset.x + (p.x - state[<span class="hljs-string">\'*\'</span>].x + W//<span class="hljs-number">2</span>) % W, str(id_))\n' +
177
171
' screen.refresh()\n' +
178
172
' <span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">0.005</span>)\n' +
179
173
'\n' +
180
174
'<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">\'__main__\'</span>:\n' +
181
- ' start_time = time.perf_counter()\n' +
182
- ' curses.wrapper(main)\n' +
183
- ' print(<span class="hljs-string">f\'You survived <span class="hljs-subst">{time.perf_counter() - start_time:<span class="hljs-number">.2</span>f}</span> seconds.\'</span>)\n' ;
175
+ ' curses.wrapper(main)\n' ;
184
176
185
177
const CURSES =
186
178
'<span class="hljs-comment"># $ pip3 install windows-curses</span>\n' +
@@ -829,7 +821,7 @@ function fixHighlights() {
829
821
$ ( `code:contains(\'<n>s\')` ) . html ( STRUCT_FORMAT ) ;
830
822
$ ( `code:contains(match <object/expression>:)` ) . html ( MATCH ) ;
831
823
$ ( `code:contains(>>> match Path)` ) . html ( MATCH_EXAMPLE ) ;
832
- // $(`code:contains(import asyncio, collections, curses, curses.textpad, enum, random)`).html(COROUTINES);
824
+ $ ( `code:contains(import asyncio, collections, curses, curses.textpad, enum, random)` ) . html ( COROUTINES ) ;
833
825
$ ( `code:contains(import curses, os)` ) . html ( CURSES ) ;
834
826
$ ( `code:contains(pip3 install tqdm)` ) . html ( PROGRESS_BAR ) ;
835
827
$ ( `code:contains(>>> logging.basicConfig()` ) . html ( LOGGING_EXAMPLE ) ;
0 commit comments