ZealOS/docs/Demo/Games/Maze.CC.html
2021-12-04 05:03:02 -05:00

155 lines
15 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V1.06">
<style type="text/css">
body {background-color:#1f1f1f;}
.cF0{color:#e3e3e3;background-color:#1f1f1f;}
.cF1{color:#4f84a6;background-color:#1f1f1f;}
.cF2{color:#73a255;background-color:#1f1f1f;}
.cF3{color:#297582;background-color:#1f1f1f;}
.cF4{color:#b34f4b;background-color:#1f1f1f;}
.cF5{color:#8a52c3;background-color:#1f1f1f;}
.cF6{color:#b7822f;background-color:#1f1f1f;}
.cF7{color:#444444;background-color:#1f1f1f;}
.cF8{color:#6d6d6d;background-color:#1f1f1f;}
.cF9{color:#94bfde;background-color:#1f1f1f;}
.cFA{color:#a1ce97;background-color:#1f1f1f;}
.cFB{color:#6db4be;background-color:#1f1f1f;}
.cFC{color:#e88e88;background-color:#1f1f1f;}
.cFD{color:#ca94e8;background-color:#1f1f1f;}
.cFE{color:#d4b475;background-color:#1f1f1f;}
.cFF{color:#1f1f1f;background-color:#1f1f1f;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF2>//See </span><span class=cF4><u>TextBase Layer</u></span><span class=cF2>.</span><span class=cF0>
<a name="l2"></a>
<a name="l3"></a>#</span><span class=cF1>define</span><span class=cF0> ATTR (</span><span class=cF3>BLACK</span><span class=cF0> &lt;&lt; </span><span class=cFE>12</span><span class=cF0> + </span><span class=cF3>WHITE</span><span class=cF0> &lt;&lt; </span><span class=cFE>8</span><span class=cF0>)
<a name="l4"></a>
<a name="l5"></a></span><span class=cF9>U32</span><span class=cF0> </span><span class=cFB>text</span><span class=cF0>[</span><span class=cF3>TEXT_ROWS</span><span class=cF0>][</span><span class=cF3>TEXT_COLS</span><span class=cF0>];
<a name="l6"></a>
<a name="l7"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DrawIt</span><span class=cF0>(</span><span class=cF9>CTask</span><span class=cF0> *task, </span><span class=cF9>CDC</span><span class=cF0> *)
<a name="l8"></a>{ </span><span class=cF2>//</span><a href="https://zeal-operating-system.github.io/ZealOS/System/Gr/GrGlobals.CC.html#l3"><span class=cF4>gr.text_base</span></a><span class=cF2> gets clear 60fps, so we must use our own permanent text array.</span><span class=cF0>
<a name="l9"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(</span><span class=cFB>gr</span><span class=cF0>.text_base + </span><span class=cF3>TEXT_COLS</span><span class=cF0>, </span><span class=cFB>text</span><span class=cF0>, </span><span class=cF7>(</span><span class=cF3>TEXT_ROWS</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF3>TEXT_COLS</span><span class=cF0> * </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>U32</span><span class=cF7>)</span><span class=cF0>);
<a name="l10"></a>
<a name="l11"></a> </span><span class=cF2>// You can copy it this way, if you like:</span><span class=cF0>
<a name="l12"></a> </span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>I64 i, j;</span><span class=cF0>
<a name="l13"></a> </span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>for (j = 0; j &lt; TEXT_ROWS; j++)</span><span class=cF0>
<a name="l14"></a> </span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>for (i = 0; i &lt; TEXT_COLS; i++)</span><span class=cF0>
<a name="l15"></a> </span><span class=cF2>//</span><span class=cF0> </span><span class=cF2> TextChar(task,, i, j, text[j][i]);</span><span class=cF0>
<a name="l16"></a>
<a name="l17"></a> </span><span class=cF5>TextPrint</span><span class=cF0>(task, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF0>, ATTR &gt;&gt; </span><span class=cFE>8</span><span class=cF0>, </span><span class=cF6>&quot;Draw a maze with left button.&quot;</span><span class=cF0>);
<a name="l18"></a> </span><span class=cF5>TextPrint</span><span class=cF0>(task, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cFE>1</span><span class=cF0>, ATTR &gt;&gt; </span><span class=cFE>8</span><span class=cF0>, </span><span class=cF6>&quot;Solve maze starting at right click.&quot;</span><span class=cF0>);
<a name="l19"></a>}
<a name="l20"></a>
<a name="l21"></a>#</span><span class=cF1>define</span><span class=cF0> STACK_SIZE </span><span class=cFE>2048</span><span class=cF0>
<a name="l22"></a></span><span class=cF2>//We would put these as local variables</span><span class=cF0>
<a name="l23"></a></span><span class=cF2>//in SolveMaze() but the system stack size</span><span class=cF0>
<a name="l24"></a></span><span class=cF2>//is limited, so it's a bad habit. The heap</span><span class=cF0>
<a name="l25"></a></span><span class=cF2>//is the normal ZealOS technique, but</span><span class=cF0>
<a name="l26"></a></span><span class=cF2>//it's a pain in this case.</span><span class=cF0>
<a name="l27"></a></span><span class=cF9>I64</span><span class=cF0> stack_ptr, stack_x [STACK_SIZE],
<a name="l28"></a> stack_y [STACK_SIZE],
<a name="l29"></a> stack_dir[STACK_SIZE];
<a name="l30"></a>
<a name="l31"></a></span><span class=cF2>//Four directions:</span><span class=cF0>
<a name="l32"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>0=Up, 1=right,2=down,3=left</span><span class=cF0>
<a name="l33"></a></span><span class=cF9>I64</span><span class=cF0> dir_x[</span><span class=cFE>4</span><span class=cF0>] = { </span><span class=cFE>0</span><span class=cF0>, +</span><span class=cFE>1</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF0>, -</span><span class=cFE>1</span><span class=cF0>}, </span><span class=cF2>// Could use </span><a href="https://zeal-operating-system.github.io/ZealOS/System/Gr/GrMath.CC.html#l5"><span class=cF4>gr_x_offsets2</span></a><span class=cF2>,</span><a href="https://zeal-operating-system.github.io/ZealOS/System/Gr/GrMath.CC.html#l6"><span class=cF4>gr_y_offsets2</span></a><span class=cF0>
<a name="l34"></a> dir_y[</span><span class=cFE>4</span><span class=cF0>] = {+</span><span class=cFE>1</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF0>, -</span><span class=cFE>1</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF0>};
<a name="l35"></a>
<a name="l36"></a></span><span class=cF1>U0</span><span class=cF0> SolveMaze(</span><span class=cF9>I64</span><span class=cF0> x, </span><span class=cF9>I64</span><span class=cF0> y)
<a name="l37"></a>{
<a name="l38"></a> </span><span class=cF9>I64</span><span class=cF0> dir = </span><span class=cFE>0</span><span class=cF0>;
<a name="l39"></a>
<a name="l40"></a> stack_ptr = </span><span class=cFE>0</span><span class=cF0>;
<a name="l41"></a> stack_x[stack_ptr] = x;
<a name="l42"></a> stack_y[stack_ptr] = y;
<a name="l43"></a> stack_dir[stack_ptr++] = dir;
<a name="l44"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l45"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l46"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cFE>0</span><span class=cF0> &lt;= x &lt; </span><span class=cF5>MinI64</span><span class=cF0>(</span><span class=cF5>Fs</span><span class=cF0>-&gt;win_width, </span><span class=cF3>TEXT_COLS</span><span class=cF0>)</span><span class=cF7>)</span><span class=cF0> ||
<a name="l47"></a> !</span><span class=cF7>(</span><span class=cFE>0</span><span class=cF0> &lt;= y &lt; </span><span class=cF5>MinI64</span><span class=cF0>(</span><span class=cF5>Fs</span><span class=cF0>-&gt;win_height, </span><span class=cF3>TEXT_ROWS</span><span class=cF0>)</span><span class=cF7>)</span><span class=cF0> )
<a name="l48"></a> {
<a name="l49"></a> </span><span class=cF5>Beep</span><span class=cF0>;
<a name="l50"></a> </span><span class=cF5>Beep</span><span class=cF0>;
<a name="l51"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l52"></a> }
<a name="l53"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cFB>text</span><span class=cF0>[y][x].u8[</span><span class=cFE>0</span><span class=cF0>])
<a name="l54"></a> </span><span class=cFB>text</span><span class=cF0>[y][x] = </span><span class=cF6>'.'</span><span class=cF0> + ATTR;
<a name="l55"></a> x += dir_x[dir];
<a name="l56"></a> y += dir_y[dir];
<a name="l57"></a></span><span class=cF2>//u8.[0] is the ASCII</span><span class=cF0>
<a name="l58"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>text</span><span class=cF0>[y][x].u8[</span><span class=cFE>0</span><span class=cF0>])
<a name="l59"></a> {
<a name="l60"></a> x -= dir_x[dir];
<a name="l61"></a> y -= dir_y[dir];
<a name="l62"></a> </span><span class=cF1>if</span><span class=cF0> (++dir == </span><span class=cFE>4</span><span class=cF0>)
<a name="l63"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l64"></a> </span><span class=cF1>if</span><span class=cF0> (--stack_ptr &lt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l65"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l66"></a> x = stack_x[stack_ptr];
<a name="l67"></a> y = stack_y[stack_ptr];
<a name="l68"></a> dir = stack_dir[stack_ptr];
<a name="l69"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l70"></a> }
<a name="l71"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l72"></a> {
<a name="l73"></a> dir = </span><span class=cFE>0</span><span class=cF0>;
<a name="l74"></a> stack_x[stack_ptr] = x;
<a name="l75"></a> stack_y[stack_ptr] = y;
<a name="l76"></a> stack_dir[stack_ptr++] = dir;
<a name="l77"></a> </span><span class=cF1>if</span><span class=cF0> (stack_ptr == STACK_SIZE)
<a name="l78"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l79"></a> </span><span class=cF5>Sleep</span><span class=cF0>(</span><span class=cFE>100</span><span class=cF0>);
<a name="l80"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>CharScan</span><span class=cF0>)
<a name="l81"></a> </span><span class=cF5>throw</span><span class=cF0>;
<a name="l82"></a> }
<a name="l83"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l84"></a>}
<a name="l85"></a>
<a name="l86"></a></span><span class=cF1>U0</span><span class=cF0> Maze()
<a name="l87"></a>{
<a name="l88"></a> </span><span class=cF9>I64</span><span class=cF0> ch, x, y;
<a name="l89"></a>
<a name="l90"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="https://zeal-operating-system.github.io/ZealOS/System/TaskSettings.CC.html#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
<a name="l91"></a> </span><span class=cF5>AutoComplete</span><span class=cF0>;
<a name="l92"></a> </span><span class=cF5>WinBorder</span><span class=cF0>;
<a name="l93"></a> </span><span class=cF5>WinMax</span><span class=cF0>;
<a name="l94"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
<a name="l95"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l96"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;draw_it = &amp;</span><span class=cF5>DrawIt</span><span class=cF0>;
<a name="l97"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;win_inhibit = </span><span class=cF3>WIG_TASK_DEFAULT</span><span class=cF0> - </span><span class=cF3>WIF_SELF_FOCUS</span><span class=cF0> - </span><span class=cF3>WIF_SELF_BORDER</span><span class=cF0>;
<a name="l98"></a>
<a name="l99"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l100"></a> </span><span class=cF1>do</span><span class=cF0>
<a name="l101"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l102"></a> </span><span class=cF5>MemSet</span><span class=cF0>(</span><span class=cFB>text</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cFB>text</span><span class=cF7>)</span><span class=cF0>);
<a name="l103"></a> </span><span class=cF1>while</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ch = </span><span class=cF5>CharScan</span><span class=cF7>)</span><span class=cF0>)
<a name="l104"></a> {
<a name="l105"></a> x = </span><span class=cFB>mouse</span><span class=cF0>.pos_text.x - </span><span class=cF5>Fs</span><span class=cF0>-&gt;win_left - </span><span class=cF5>Fs</span><span class=cF0>-&gt;scroll_x / </span><span class=cF3>FONT_WIDTH</span><span class=cF0>;
<a name="l106"></a> y = </span><span class=cFB>mouse</span><span class=cF0>.pos_text.y - </span><span class=cF5>Fs</span><span class=cF0>-&gt;win_top - </span><span class=cF5>Fs</span><span class=cF0>-&gt;scroll_y / </span><span class=cF3>FONT_HEIGHT</span><span class=cF0>;
<a name="l107"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>mouse</span><span class=cF0>.lb &amp;&amp; !</span><span class=cFB>winmgr</span><span class=cF0>.grab_scroll)
<a name="l108"></a> </span><span class=cFB>text</span><span class=cF0>[y][x] = </span><span class=cF3>CH_SPACE</span><span class=cF0> + </span><span class=cF3>ATTRF_INVERT</span><span class=cF0> + ATTR;
<a name="l109"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>mouse</span><span class=cF0>.rb &amp;&amp; !</span><span class=cFB>winmgr</span><span class=cF0>.grab_scroll)
<a name="l110"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l111"></a> </span><span class=cFB>text</span><span class=cF0>[y][x] = </span><span class=cF6>'*'</span><span class=cF0>+ATTR;
<a name="l112"></a> SolveMaze(x, y);
<a name="l113"></a> ch = </span><span class=cF5>CharGet</span><span class=cF0>;
<a name="l114"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l115"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l116"></a> </span><span class=cF5>Refresh</span><span class=cF0>;
<a name="l117"></a> }
<a name="l118"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l119"></a> </span><span class=cF1>while</span><span class=cF0> (ch != </span><span class=cF3>CH_SHIFT_ESC</span><span class=cF0> &amp;&amp; ch != </span><span class=cF3>CH_ESC</span><span class=cF0>);
<a name="l120"></a>
<a name="l121"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l122"></a> </span><span class=cF5>PutExcept</span><span class=cF0>;
<a name="l123"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l124"></a>}
<a name="l125"></a>
<a name="l126"></a>Maze;
</span></pre></body>
</html>