templeos-info/public/Wb/Demo/Games/Maze.HC.HTML

144 lines
12 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<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;12+</span><span class=cF3>WHITE</span><span class=cF0>&lt;&lt;8)
<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="/Wb/Adam/Gr/GrGlbls.HC.HTML#l3"><span class=cF4>gr.text_base</span></a><span class=cF2> gets clear 30fps, so we must use our own permanent text array.</span><span class=cF0>
<a name="l9"></a> </span><span class=cF5>MemCpy</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>-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>// I64 i,j;</span><span class=cF0>
<a name="l13"></a> </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,0,0,ATTR&gt;&gt;8,</span><span class=cF6>&quot;Draw a maze with left bttn.&quot;</span><span class=cF0>);
<a name="l18"></a> </span><span class=cF5>TextPrint</span><span class=cF0>(task,0,1,ATTR&gt;&gt;8,</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> STK_SIZE 2048
<a name="l22"></a></span><span class=cF2>//We would put these as local vars</span><span class=cF0>
<a name="l23"></a></span><span class=cF2>//in SolveMaze() but the system stk 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 TempleOS 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> stk_ptr,
<a name="l28"></a> stk_x [STK_SIZE],
<a name="l29"></a> stk_y [STK_SIZE],
<a name="l30"></a> stk_dir[STK_SIZE];
<a name="l31"></a>
<a name="l32"></a></span><span class=cF2>//Four directions:</span><span class=cF0>
<a name="l33"></a></span><span class=cF2>// 0=Up,1=right,2=down,3=left</span><span class=cF0>
<a name="l34"></a></span><span class=cF9>I64</span><span class=cF0> dir_x[4]={ 0,+1, 0,-1}, </span><span class=cF2>// Could use </span><a href="/Wb/Adam/Gr/GrMath.HC.HTML#l5"><span class=cF4>gr_x_offsets2</span></a><span class=cF2>,</span><a href="/Wb/Adam/Gr/GrMath.HC.HTML#l6"><span class=cF4>gr_y_offsets2</span></a><span class=cF0>
<a name="l35"></a> dir_y[4]={+1, 0,-1, 0};
<a name="l36"></a>
<a name="l37"></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="l38"></a>{
<a name="l39"></a> </span><span class=cF9>I64</span><span class=cF0> dir=0;
<a name="l40"></a> stk_ptr=0;
<a name="l41"></a> stk_x[stk_ptr]=x;
<a name="l42"></a> stk_y[stk_ptr]=y;
<a name="l43"></a> stk_dir[stk_ptr++]=dir;
<a name="l44"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l45"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>0&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="l46"></a> !</span><span class=cF7>(</span><span class=cF0>0&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="l47"></a> </span><span class=cF5>Beep</span><span class=cF0>;</span><span class=cF5>Beep</span><span class=cF0>;
<a name="l48"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l49"></a> }
<a name="l50"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cFB>text</span><span class=cF0>[y][x].u8[0])
<a name="l51"></a> </span><span class=cFB>text</span><span class=cF0>[y][x]=</span><span class=cF6>'.'</span><span class=cF0>+ATTR;
<a name="l52"></a> x+=dir_x[dir];
<a name="l53"></a> y+=dir_y[dir];
<a name="l54"></a></span><span class=cF2>//u8.[0] is the ASCII</span><span class=cF0>
<a name="l55"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>text</span><span class=cF0>[y][x].u8[0]) {
<a name="l56"></a> x-=dir_x[dir];
<a name="l57"></a> y-=dir_y[dir];
<a name="l58"></a> </span><span class=cF1>if</span><span class=cF0> (++dir==4) </span><span class=cF7>{</span><span class=cF0>
<a name="l59"></a> </span><span class=cF1>if</span><span class=cF0> (--stk_ptr&lt;0) </span><span class=cF1>return</span><span class=cF0>;
<a name="l60"></a> x=stk_x[stk_ptr];
<a name="l61"></a> y=stk_y[stk_ptr];
<a name="l62"></a> dir=stk_dir[stk_ptr];
<a name="l63"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l64"></a> } </span><span class=cF1>else</span><span class=cF0> {
<a name="l65"></a> dir=0;
<a name="l66"></a> stk_x[stk_ptr]=x;
<a name="l67"></a> stk_y[stk_ptr]=y;
<a name="l68"></a> stk_dir[stk_ptr++]=dir;
<a name="l69"></a> </span><span class=cF1>if</span><span class=cF0> (stk_ptr==STK_SIZE) </span><span class=cF1>return</span><span class=cF0>;
<a name="l70"></a> </span><span class=cF5>Sleep</span><span class=cF0>(100);
<a name="l71"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>ScanChar</span><span class=cF0>)
<a name="l72"></a> </span><span class=cF5>throw</span><span class=cF0>;
<a name="l73"></a> }
<a name="l74"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l75"></a>}
<a name="l76"></a>
<a name="l77"></a></span><span class=cF1>U0</span><span class=cF0> Maze()
<a name="l78"></a>{
<a name="l79"></a> </span><span class=cF9>I64</span><span class=cF0> ch,x,y;
<a name="l80"></a>
<a name="l81"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="/Wb/Adam/TaskSettings.HC.HTML#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
<a name="l82"></a> </span><span class=cF5>AutoComplete</span><span class=cF0>;
<a name="l83"></a> </span><span class=cF5>WinBorder</span><span class=cF0>;
<a name="l84"></a> </span><span class=cF5>WinMax</span><span class=cF0>;
<a name="l85"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
<a name="l86"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l87"></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="l88"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;win_inhibit=</span><span class=cF3>WIG_TASK_DFT</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="l89"></a>
<a name="l90"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l91"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l92"></a> </span><span class=cF5>MemSet</span><span class=cF0>(</span><span class=cFB>text</span><span class=cF0>,0,</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cFB>text</span><span class=cF7>)</span><span class=cF0>);
<a name="l93"></a> </span><span class=cF1>while</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ch=</span><span class=cF5>ScanChar</span><span class=cF7>)</span><span class=cF0>) {
<a name="l94"></a> x=</span><span class=cFB>ms</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="l95"></a> y=</span><span class=cFB>ms</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="l96"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>ms</span><span class=cF0>.lb&amp;&amp;!</span><span class=cFB>winmgr</span><span class=cF0>.grab_scroll)
<a name="l97"></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="l98"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>ms</span><span class=cF0>.rb&amp;&amp;!</span><span class=cFB>winmgr</span><span class=cF0>.grab_scroll) </span><span class=cF7>{</span><span class=cF0>
<a name="l99"></a> </span><span class=cFB>text</span><span class=cF0>[y][x]=</span><span class=cF6>'*'</span><span class=cF0>+ATTR;
<a name="l100"></a> SolveMaze(x,y);
<a name="l101"></a> ch=</span><span class=cF5>GetChar</span><span class=cF0>;
<a name="l102"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l103"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l104"></a> </span><span class=cF5>Refresh</span><span class=cF0>;
<a name="l105"></a> }
<a name="l106"></a> </span><span class=cF7>}</span><span class=cF0> </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="l107"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l108"></a> </span><span class=cF5>PutExcept</span><span class=cF0>;
<a name="l109"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l110"></a>}
<a name="l111"></a>
<a name="l112"></a>Maze;
</span></pre></body>
</html>