add hack-a-round for language specified markdown code blocks
When language is specified in a markdown code block such as:
````
```c
printf("Hello, world!\n");
```
````
is used, the resulting HTML as produced by `pandoc(1)` combined
with our revised `ioccc.css` is not intent the code block well.
A temporary "_hack-a-round_" is to turn the "markdown code language
specifier" into an HTML comment. For example:
````
```<!---c-->
printf("Hello, world!\n");
```
````
This commits turns off existing markdown code language specifiers
for now, while preserving our ability to restore them in the future
when a better "_hack-a-round_" to the "markdown code language
specifier" problem is found.
The HTML impact if this "_hack-a-round_" is to produce HTML that
looks like this:
```
<pre class="<!---c-->"><code> .. </code></pre>
```
We might be able to create a complex set of classes for each markdown
code language (there won't be very many) in `ioccc.css`, or we might
find a more general CSS (or maybe `pandoc(1)`?) solution.
For now, this hack-a-round will have to do.
This commit is contained in:
1 parent
3919df50fb
commit
b66c79254a
700 files changed
+7052
-7056
No files matched your search
+23
-23
@@ -1,6 +1,6 @@
|
||||
## To build:
|
||||
|
||||
```sh
|
||||
```<!---sh-->
|
||||
make
|
||||
```
|
||||
|
||||
@@ -18,20 +18,20 @@ For more detailed information see [2020 carlini bugs](../../bugs.html#2020_carli
|
||||
|
||||
## To use:
|
||||
|
||||
```sh
|
||||
```<!---sh-->
|
||||
./prog
|
||||
```
|
||||
|
||||
|
||||
## Try:
|
||||
|
||||
```sh
|
||||
```<!---sh-->
|
||||
./try.sh
|
||||
```
|
||||
|
||||
Or if you actually want to play:
|
||||
|
||||
```sh
|
||||
```<!---sh-->
|
||||
# Play with a friend, real or imagined, P1 and P2, squares are numbered 1..9
|
||||
./prog
|
||||
```
|
||||
@@ -51,7 +51,7 @@ HOW ABOUT A NICE GAME OF <strike>CHESS</strike>TIC-TAC-TOE?
|
||||
|
||||
### USAGE
|
||||
|
||||
```sh
|
||||
```<!---sh-->
|
||||
cc -o prog prog.c
|
||||
./prog
|
||||
```
|
||||
@@ -92,7 +92,7 @@ compilers unhappy, and others slow. (For example, passing GCC the flags `-Wall
|
||||
|
||||
Under some terminals the program may exit early. The following should always work:
|
||||
|
||||
```sh
|
||||
```<!---sh-->
|
||||
echo "1 2 3 4 5 6 7" | ./prog
|
||||
```
|
||||
|
||||
@@ -100,7 +100,7 @@ Under some terminals the program may exit early. The following should always wor
|
||||
|
||||
The entirety of the program consists of a single iterated call to `printf(3)`.
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
int main() {
|
||||
while(*d) printf(fmt, arg);
|
||||
}
|
||||
@@ -144,13 +144,13 @@ Format specifiers can take extra "arguments".
|
||||
|
||||
For example, the following expression
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
printf("%1$.*2$d%3$hhn", 5, 10, &x)
|
||||
```
|
||||
|
||||
will have the same effect as if we had written
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
x = 10;
|
||||
```
|
||||
|
||||
@@ -174,37 +174,37 @@ We can use format strings to compute the OR/NOT of arbitrary "bits".
|
||||
|
||||
We'll start with the simplest, OR:
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
printf("%1$s%2$s%3$hhn", a, b, c)
|
||||
```
|
||||
|
||||
will compute
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
*c = strlen(a) + strlen(b)
|
||||
```
|
||||
|
||||
but given that `strlen(x)` is `1` for a 1-bit and `0` for a 0-bit, we have
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
*c = a | b
|
||||
```
|
||||
|
||||
Computing the NOT of a single value is also easy:
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
printf("%1$255d%1$s%hhn", a, b)
|
||||
```
|
||||
|
||||
will compute
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
*b = (strlen(a)+255)%256 = strlen(a)-1
|
||||
```
|
||||
|
||||
and again, because `strlen(x)` is either `1` or `0` we have
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
*c = !b
|
||||
```
|
||||
|
||||
@@ -221,7 +221,7 @@ To detect who has won, we implement the following logic. Let `A`, `B`, and `C` b
|
||||
pointers to three squares in a row to test, and `D` be where to save if there is a
|
||||
win or not.
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
"%A$s%B$s%C$s%1$253d%11$hhn" // r11 = !(*A & *B & *C)
|
||||
ZERO
|
||||
"%11$s%1$255d%11hhn" // r11 = !r11
|
||||
@@ -235,7 +235,7 @@ possible three-in-a-row configurations, for both players.
|
||||
The ZERO macro ensures that the number of bytes written out is 0 mod 256 with
|
||||
the following expression
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
"%1$hhn%1$s" (repeated 256 times)
|
||||
```
|
||||
|
||||
@@ -254,13 +254,13 @@ bits to Xs and Os to print out. This is actually rather straightforward. Given
|
||||
in `1$` the pointer to player 1's square, and `2$` the pointer to player 2's, and
|
||||
in `3$` the pointer to the board string, we can compute
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
"%1$s" (repeated 47 times) "%2$s" (repeated 56 times) %1$32d%3$hhn"
|
||||
```
|
||||
|
||||
which will, in effect, compute
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
*r3 = (*r1) * 47 + (*r2) * 56 + 32
|
||||
```
|
||||
|
||||
@@ -273,7 +273,7 @@ which will output `' '` if neither are true, `'X'` if `r1` is true, or `'O'` if
|
||||
In order to be able to finally display the board, while still only using one
|
||||
`printf(3)` statement, we finish the statement with
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
"\n\033[2J\n%26$s"
|
||||
```
|
||||
|
||||
@@ -299,7 +299,7 @@ won, or the game is over and it is a draw.
|
||||
This turns out not to be as hard as it might look. Using the same trick as
|
||||
before, we set byte four to be
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
*byte4 = is_win * 'W' + is_tie * 'T'
|
||||
```
|
||||
|
||||
@@ -313,7 +313,7 @@ ends. It should just exit.
|
||||
|
||||
One option would be to implement the logic as
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
printf()
|
||||
while (*ok) {
|
||||
scanf();
|
||||
@@ -324,7 +324,7 @@ One option would be to implement the logic as
|
||||
but this would DOUBLE the number of calls to `printf(3)` we require. So instead we
|
||||
implement it like this
|
||||
|
||||
```c
|
||||
```<!---c-->
|
||||
while (*ok) {
|
||||
scanf();
|
||||
printf();
|
||||
|
||||
+38
-38
@@ -141,18 +141,18 @@ Location: <a href="../../location.html#US">US</a> - <em>United States of America
|
||||
<!-- START: this line starts content for HTML phase 21 by: bin/pandoc-wrapper.sh via bin/md2html.sh -->
|
||||
|
||||
<h2 id="to-build">To build:</h2>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a> <span class="fu">make</span></span></code></pre></div>
|
||||
<pre class="<!---sh-->"><code> make</code></pre>
|
||||
<h3 id="bugs-and-misfeatures">Bugs and (Mis)features:</h3>
|
||||
<p>The current status of this entry is:</p>
|
||||
<pre><code> STATUS: INABIAF - please **DO NOT** fix</code></pre>
|
||||
<p>For more detailed information see <a href="../../bugs.html#2020_carlini">2020 carlini bugs</a>.</p>
|
||||
<h2 id="to-use">To use:</h2>
|
||||
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a> <span class="ex">./prog</span></span></code></pre></div>
|
||||
<pre class="<!---sh-->"><code> ./prog</code></pre>
|
||||
<h2 id="try">Try:</h2>
|
||||
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a> <span class="ex">./try.sh</span></span></code></pre></div>
|
||||
<pre class="<!---sh-->"><code> ./try.sh</code></pre>
|
||||
<p>Or if you actually want to play:</p>
|
||||
<div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a> <span class="co"># Play with a friend, real or imagined, P1 and P2, squares are numbered 1..9</span></span>
|
||||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="ex">./prog</span></span></code></pre></div>
|
||||
<pre class="<!---sh-->"><code> # Play with a friend, real or imagined, P1 and P2, squares are numbered 1..9
|
||||
./prog</code></pre>
|
||||
<h2 id="judges-remarks">Judges’ remarks:</h2>
|
||||
<p>This program consists of a single <code>printf(3)</code> statement wrapped in a <code>while</code>
|
||||
loop. You would not think that this would amount to much, but you would be very,
|
||||
@@ -161,8 +161,8 @@ ASCII art of the program source.</p>
|
||||
<h2 id="authors-remarks">Author’s remarks:</h2>
|
||||
<p>HOW ABOUT A NICE GAME OF <strike>CHESS</strike>TIC-TAC-TOE?</p>
|
||||
<h3 id="usage">USAGE</h3>
|
||||
<div class="sourceCode" id="cb6"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a> <span class="fu">cc</span> <span class="at">-o</span> prog prog.c</span>
|
||||
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="ex">./prog</span></span></code></pre></div>
|
||||
<pre class="<!---sh-->"><code> cc -o prog prog.c
|
||||
./prog</code></pre>
|
||||
<p>Alternates between P1 and P2. Enter a digit [1-9] to move:</p>
|
||||
<pre><code> 1 | 2 | 3
|
||||
---------
|
||||
@@ -187,12 +187,12 @@ patient.</p>
|
||||
<p>After macro expansion the format string is roughly 100K. This makes some
|
||||
compilers unhappy, and others slow. (For example, passing GCC the flags <code>-Wall -Wextra -pedantic</code> will result in a warning about oversized strings.)</p>
|
||||
<p>Under some terminals the program may exit early. The following should always work:</p>
|
||||
<div class="sourceCode" id="cb8"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a> <span class="bu">echo</span> <span class="st">"1 2 3 4 5 6 7"</span> <span class="kw">|</span> <span class="ex">./prog</span></span></code></pre></div>
|
||||
<pre class="<!---sh-->"><code> echo "1 2 3 4 5 6 7" | ./prog</code></pre>
|
||||
<h3 id="obfuscation">OBFUSCATION</h3>
|
||||
<p>The entirety of the program consists of a single iterated call to <code>printf(3)</code>.</p>
|
||||
<div class="sourceCode" id="cb9"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a> <span class="dt">int</span> main<span class="op">()</span> <span class="op">{</span></span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span><span class="op">(*</span>d<span class="op">)</span> printf<span class="op">(</span>fmt<span class="op">,</span> arg<span class="op">);</span></span>
|
||||
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> int main() {
|
||||
while(*d) printf(fmt, arg);
|
||||
}</code></pre>
|
||||
<p>Here, <code>fmt</code> is a single string, and <code>arg</code> is a series of arguments to
|
||||
<code>printf(3)</code>.</p>
|
||||
<p>While its primary purpose is to serve as The One True Debugger, <code>printf(3)</code> also happens
|
||||
@@ -218,9 +218,9 @@ this one <code>printf(3)</code> call (and a call to <code>scanf(3)</code> to rea
|
||||
<li><p><code>"%3$.*4$d"</code>: print argument 3 to <code>printf(3)</code> with as many zeros as in argument 4.</p></li>
|
||||
</ul>
|
||||
<p>For example, the following expression</p>
|
||||
<div class="sourceCode" id="cb10"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a> printf<span class="op">(</span><span class="st">"%1$.*2$d%3$hhn"</span><span class="op">,</span> <span class="dv">5</span><span class="op">,</span> <span class="dv">10</span><span class="op">,</span> <span class="op">&</span>x<span class="op">)</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> printf("%1$.*2$d%3$hhn", 5, 10, &x)</code></pre>
|
||||
<p>will have the same effect as if we had written</p>
|
||||
<div class="sourceCode" id="cb11"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> <span class="dv">10</span><span class="op">;</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> x = 10;</code></pre>
|
||||
<p>because it will print out <code>0000000005</code> (5 padded to size 10) and then write the
|
||||
number of bytes written to <code>x</code>.</p>
|
||||
<h4 id="printf3-oriented-programming"><code>printf(3)</code> Oriented Programming</h4>
|
||||
@@ -233,17 +233,17 @@ array–one bit per pair of bytes:</p>
|
||||
</ul>
|
||||
<p>We can use format strings to compute the OR/NOT of arbitrary “bits”.</p>
|
||||
<p>We’ll start with the simplest, OR:</p>
|
||||
<div class="sourceCode" id="cb12"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a> printf<span class="op">(</span><span class="st">"%1$s%2$s%3$hhn"</span><span class="op">,</span> a<span class="op">,</span> b<span class="op">,</span> c<span class="op">)</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> printf("%1$s%2$s%3$hhn", a, b, c)</code></pre>
|
||||
<p>will compute</p>
|
||||
<div class="sourceCode" id="cb13"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>c <span class="op">=</span> strlen<span class="op">(</span>a<span class="op">)</span> <span class="op">+</span> strlen<span class="op">(</span>b<span class="op">)</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> *c = strlen(a) + strlen(b)</code></pre>
|
||||
<p>but given that <code>strlen(x)</code> is <code>1</code> for a 1-bit and <code>0</code> for a 0-bit, we have</p>
|
||||
<div class="sourceCode" id="cb14"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>c <span class="op">=</span> a <span class="op">|</span> b</span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> *c = a | b</code></pre>
|
||||
<p>Computing the NOT of a single value is also easy:</p>
|
||||
<div class="sourceCode" id="cb15"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a> printf<span class="op">(</span><span class="st">"%1$255d%1$s</span><span class="sc">%hhn</span><span class="st">"</span><span class="op">,</span> a<span class="op">,</span> b<span class="op">)</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> printf("%1$255d%1$s%hhn", a, b)</code></pre>
|
||||
<p>will compute</p>
|
||||
<div class="sourceCode" id="cb16"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>b <span class="op">=</span> <span class="op">(</span>strlen<span class="op">(</span>a<span class="op">)+</span><span class="dv">255</span><span class="op">)%</span><span class="dv">256</span> <span class="op">=</span> strlen<span class="op">(</span>a<span class="op">)-</span><span class="dv">1</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> *b = (strlen(a)+255)%256 = strlen(a)-1</code></pre>
|
||||
<p>and again, because <code>strlen(x)</code> is either <code>1</code> or <code>0</code> we have</p>
|
||||
<div class="sourceCode" id="cb17"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>c <span class="op">=</span> <span class="op">!</span>b</span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> *c = !b</code></pre>
|
||||
<p>From here we can compute any binary circuit. Doing something efficient,
|
||||
though, still takes work.</p>
|
||||
<h4 id="tic-tac-toe">Tic-Tac-Toe</h4>
|
||||
@@ -252,16 +252,16 @@ with a turn counter that alternates between player 1 and player 2.</p>
|
||||
<p>To detect who has won, we implement the following logic. Let <code>A</code>, <code>B</code>, and <code>C</code> be
|
||||
pointers to three squares in a row to test, and <code>D</code> be where to save if there is a
|
||||
win or not.</p>
|
||||
<div class="sourceCode" id="cb18"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a> <span class="st">"</span><span class="sc">%A</span><span class="st">$s%B$s%C$s%1$253d%11$hhn"</span> <span class="co">// r11 = !(*A & *B & *C)</span></span>
|
||||
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a> ZERO</span>
|
||||
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"%11$s%1$255d</span><span class="sc">%11hhn</span><span class="st">"</span> <span class="co">// r11 = !r11</span></span>
|
||||
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a> ZERO</span>
|
||||
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"%11$s%D$s%D$hhn"</span> <span class="co">// *D = *D | r11</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> "%A$s%B$s%C$s%1$253d%11$hhn" // r11 = !(*A & *B & *C)
|
||||
ZERO
|
||||
"%11$s%1$255d%11hhn" // r11 = !r11
|
||||
ZERO
|
||||
"%11$s%D$s%D$hhn" // *D = *D | r11</code></pre>
|
||||
<p>That is, we set <code>*D</code> to <code>1</code> if there is a three-in-a-row. We repeat this for all
|
||||
possible three-in-a-row configurations, for both players.</p>
|
||||
<p>The ZERO macro ensures that the number of bytes written out is 0 mod 256 with
|
||||
the following expression</p>
|
||||
<div class="sourceCode" id="cb19"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a> <span class="st">"%1$hhn%1$s"</span> <span class="op">(</span>repeated <span class="dv">256</span> times<span class="op">)</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> "%1$hhn%1$s" (repeated 256 times)</code></pre>
|
||||
<p>where argument 1 is a pointer to a temporary variable followed by a NUL byte.</p>
|
||||
<p>This works because if the current count is 0 mod 256, then <code>"%1$hhn"</code> will write
|
||||
zero to argument 1 and then <code>"%1$s"</code> will never emit any text. If, on the other
|
||||
@@ -273,15 +273,15 @@ times we’re eventually going to reach 0 mod 256.</p>
|
||||
bits to Xs and Os to print out. This is actually rather straightforward. Given
|
||||
in <code>1$</code> the pointer to player 1’s square, and <code>2$</code> the pointer to player 2’s, and
|
||||
in <code>3$</code> the pointer to the board string, we can compute</p>
|
||||
<div class="sourceCode" id="cb20"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a> <span class="st">"%1$s"</span> <span class="op">(</span>repeated <span class="dv">47</span> times<span class="op">)</span> <span class="st">"%2$s"</span> <span class="op">(</span>repeated <span class="dv">56</span> times<span class="op">)</span> <span class="op">%</span><span class="dv">1</span>$<span class="dv">32</span><span class="er">d</span><span class="op">%</span><span class="dv">3</span>$hhn<span class="st">"</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> "%1$s" (repeated 47 times) "%2$s" (repeated 56 times) %1$32d%3$hhn"</code></pre>
|
||||
<p>which will, in effect, compute</p>
|
||||
<div class="sourceCode" id="cb21"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>r3 <span class="op">=</span> <span class="op">(*</span>r1<span class="op">)</span> <span class="op">*</span> <span class="dv">47</span> <span class="op">+</span> <span class="op">(*</span>r2<span class="op">)</span> <span class="op">*</span> <span class="dv">56</span> <span class="op">+</span> <span class="dv">32</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> *r3 = (*r1) * 47 + (*r2) * 56 + 32</code></pre>
|
||||
<p>which will output <code>' '</code> if neither are true, <code>'X'</code> if <code>r1</code> is true, or <code>'O'</code> if
|
||||
<code>r2</code> is true.</p>
|
||||
<h4 id="further-obfuscations">Further Obfuscations</h4>
|
||||
<p>In order to be able to finally display the board, while still only using one
|
||||
<code>printf(3)</code> statement, we finish the statement with</p>
|
||||
<div class="sourceCode" id="cb22"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a> <span class="st">"</span><span class="sc">\n\033</span><span class="st">[2J</span><span class="sc">\n</span><span class="st">%26$s"</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> "\n\033[2J\n%26$s"</code></pre>
|
||||
<p>which is the escape sequence to clear the screen, and then prints argument 26.
|
||||
Argument 26 is a pointer to a <code>char*</code> in memory, that initially is undefined,
|
||||
but within the <code>printf(3)</code> statement we will construct this string to look like a
|
||||
@@ -297,7 +297,7 @@ tic-tac-toe board.</p>
|
||||
won, or the game is over and it is a draw.</p>
|
||||
<p>This turns out not to be as hard as it might look. Using the same trick as
|
||||
before, we set byte four to be</p>
|
||||
<div class="sourceCode" id="cb24"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>byte4 <span class="op">=</span> is_win <span class="op">*</span> <span class="ch">'W'</span> <span class="op">+</span> is_tie <span class="op">*</span> <span class="ch">'T'</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> *byte4 = is_win * 'W' + is_tie * 'T'</code></pre>
|
||||
<p>The byte <code>'I'</code> and <code>'S'</code> can always be the same, and we do the same for <code>'E'</code>/<code>'N'</code>.</p>
|
||||
<p>We do this same on-the-fly creation of the <code>scanf(3)</code> format string, but for a
|
||||
different reason. We first want to run <code>printf(3)</code> to show the first board, and
|
||||
@@ -305,17 +305,17 @@ then alternate between runs to <code>scanf(3)</code> and <code>printf(3)</code>
|
||||
displaying moves. Importantly, we <em>do not</em> want a final <code>scanf(3)</code> when the game
|
||||
ends. It should just exit.</p>
|
||||
<p>One option would be to implement the logic as</p>
|
||||
<div class="sourceCode" id="cb25"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a> printf<span class="op">()</span></span>
|
||||
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span> <span class="op">(*</span>ok<span class="op">)</span> <span class="op">{</span></span>
|
||||
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> scanf<span class="op">();</span></span>
|
||||
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a> printf<span class="op">();</span></span>
|
||||
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> printf()
|
||||
while (*ok) {
|
||||
scanf();
|
||||
printf();
|
||||
}</code></pre>
|
||||
<p>but this would DOUBLE the number of calls to <code>printf(3)</code> we require. So instead we
|
||||
implement it like this</p>
|
||||
<div class="sourceCode" id="cb26"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span> <span class="op">(*</span>ok<span class="op">)</span> <span class="op">{</span></span>
|
||||
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> scanf<span class="op">();</span></span>
|
||||
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> printf<span class="op">();</span></span>
|
||||
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span></code></pre></div>
|
||||
<pre class="<!---c-->"><code> while (*ok) {
|
||||
scanf();
|
||||
printf();
|
||||
}</code></pre>
|
||||
<p>(In reality we actually pass <code>scanf(3)</code> as an argument to avoid the extra
|
||||
statement, but it has the same effect.)</p>
|
||||
<p>Notice there is now no initial <code>printf(3)</code>. In order make sure the program doesn’t
|
||||
|
||||
Reference in new issue
Block a user