templeos-info/public/Wb/Demo/Graphics/Hanoi.HC.HTML

186 lines
14 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=cF0>#</span><span class=cF1>define</span><span class=cF0> DISKS_NUM 6
<a name="l2"></a>#</span><span class=cF1>define</span><span class=cF0> PEDESTAL_HEIGHT 20
<a name="l3"></a>#</span><span class=cF1>define</span><span class=cF0> DISK_HEIGHT 7
<a name="l4"></a>#</span><span class=cF1>define</span><span class=cF0> DISK_UNIT_WIDTH 5
<a name="l5"></a>
<a name="l6"></a></span><span class=cF9>I64</span><span class=cF0> poles_x[3];
<a name="l7"></a></span><span class=cF9>I64</span><span class=cF0> disks_x[DISKS_NUM],disks_y[DISKS_NUM],disks_pole[DISKS_NUM];
<a name="l8"></a>
<a name="l9"></a></span><span class=cF9>I64</span><span class=cF0> OtherPole(</span><span class=cF9>I64</span><span class=cF0> pole1,</span><span class=cF9>I64</span><span class=cF0> pole2)
<a name="l10"></a>{
<a name="l11"></a> </span><span class=cF1>return</span><span class=cF0> 3-pole1-pole2;
<a name="l12"></a>}
<a name="l13"></a>
<a name="l14"></a></span><span class=cF9>I64</span><span class=cF0> TopDisk(</span><span class=cF9>I64</span><span class=cF0> pole)
<a name="l15"></a>{
<a name="l16"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l17"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;DISKS_NUM;i++)
<a name="l18"></a> </span><span class=cF1>if</span><span class=cF0> (disks_pole[i]==pole)
<a name="l19"></a> </span><span class=cF1>return</span><span class=cF0> i;
<a name="l20"></a> </span><span class=cF1>return</span><span class=cF0> -1;
<a name="l21"></a>}
<a name="l22"></a>
<a name="l23"></a></span><span class=cF9>I64</span><span class=cF0> PosInStk(</span><span class=cF9>I64</span><span class=cF0> pole,</span><span class=cF9>I64</span><span class=cF0> disk)
<a name="l24"></a>{
<a name="l25"></a> </span><span class=cF9>I64</span><span class=cF0> res=0,i;
<a name="l26"></a> </span><span class=cF1>for</span><span class=cF0> (i=DISKS_NUM-1;i&gt;disk;i--)
<a name="l27"></a> </span><span class=cF1>if</span><span class=cF0> (disks_pole[i]==pole)
<a name="l28"></a> res++;
<a name="l29"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l30"></a>}
<a name="l31"></a>
<a name="l32"></a></span><span class=cF1>U0</span><span class=cF0> SetDisksRestXY()
<a name="l33"></a>{
<a name="l34"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l35"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;DISKS_NUM;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l36"></a> disks_x[i]=poles_x[disks_pole[i]];
<a name="l37"></a> disks_y[i]=</span><span class=cF5>Fs</span><span class=cF0>-&gt;pix_height-PEDESTAL_HEIGHT
<a name="l38"></a> -(DISK_HEIGHT+1)/2-1-(DISK_HEIGHT+1)*PosInStk(disks_pole[i],i);
<a name="l39"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l40"></a>}
<a name="l41"></a>
<a name="l42"></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> *dc)
<a name="l43"></a>{
<a name="l44"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l45"></a>
<a name="l46"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;3;i++)
<a name="l47"></a> poles_x[i]=(1+i)*task-&gt;pix_width/4;
<a name="l48"></a>
<a name="l49"></a> dc-&gt;color=</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l50"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc,poles_x[0]-50,task-&gt;pix_height-PEDESTAL_HEIGHT,
<a name="l51"></a> poles_x[2]-poles_x[0]+100,PEDESTAL_HEIGHT-</span><span class=cF3>FONT_HEIGHT</span><span class=cF0>);
<a name="l52"></a> dc-&gt;color=</span><span class=cF3>DKGRAY</span><span class=cF0>;
<a name="l53"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc,poles_x[0]-49,task-&gt;pix_height-PEDESTAL_HEIGHT+1,
<a name="l54"></a> poles_x[2]-poles_x[0]+98,PEDESTAL_HEIGHT-</span><span class=cF3>FONT_HEIGHT</span><span class=cF0>-2);
<a name="l55"></a>
<a name="l56"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;3;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l57"></a> dc-&gt;color=</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l58"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc,poles_x[i]-3,
<a name="l59"></a> task-&gt;pix_height-PEDESTAL_HEIGHT-</span><span class=cF7>(</span><span class=cF0>DISKS_NUM+1</span><span class=cF7>)</span><span class=cF0>*</span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT+1</span><span class=cF7>)</span><span class=cF0>,
<a name="l60"></a> 7,</span><span class=cF7>(</span><span class=cF0>DISKS_NUM+1</span><span class=cF7>)</span><span class=cF0>*</span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT+1</span><span class=cF7>)</span><span class=cF0>);
<a name="l61"></a> dc-&gt;color=</span><span class=cF3>YELLOW</span><span class=cF0>;
<a name="l62"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc,poles_x[i]-2,
<a name="l63"></a> task-&gt;pix_height-PEDESTAL_HEIGHT+1-</span><span class=cF7>(</span><span class=cF0>DISKS_NUM+1</span><span class=cF7>)</span><span class=cF0>*</span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT+1</span><span class=cF7>)</span><span class=cF0>,
<a name="l64"></a> 5,</span><span class=cF7>(</span><span class=cF0>DISKS_NUM+1</span><span class=cF7>)</span><span class=cF0>*</span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT+1</span><span class=cF7>)</span><span class=cF0>-1);
<a name="l65"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l66"></a>
<a name="l67"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;DISKS_NUM;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l68"></a> dc-&gt;color=</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l69"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc,disks_x[i]-</span><span class=cF7>(</span><span class=cF0>i+1</span><span class=cF7>)</span><span class=cF0>*DISK_UNIT_WIDTH,
<a name="l70"></a> disks_y[i]-DISK_HEIGHT/2,</span><span class=cF7>(</span><span class=cF0>i+1</span><span class=cF7>)</span><span class=cF0>*</span><span class=cF7>(</span><span class=cF0>DISK_UNIT_WIDTH*2</span><span class=cF7>)</span><span class=cF0>+1,DISK_HEIGHT);
<a name="l71"></a> dc-&gt;color=</span><span class=cFB>gr_rainbow_10</span><span class=cF0>[i];
<a name="l72"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc,disks_x[i]-</span><span class=cF7>(</span><span class=cF0>i+1</span><span class=cF7>)</span><span class=cF0>*DISK_UNIT_WIDTH+1,
<a name="l73"></a> disks_y[i]-DISK_HEIGHT/2+1,</span><span class=cF7>(</span><span class=cF0>i+1</span><span class=cF7>)</span><span class=cF0>*</span><span class=cF7>(</span><span class=cF0>DISK_UNIT_WIDTH*2</span><span class=cF7>)</span><span class=cF0>-1,DISK_HEIGHT-2);
<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> MySleep()
<a name="l78"></a>{
<a name="l79"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>ScanChar</span><span class=cF0>)
<a name="l80"></a> </span><span class=cF5>throw</span><span class=cF0>;
<a name="l81"></a> </span><span class=cF5>Sleep</span><span class=cF0>(3);
<a name="l82"></a>}
<a name="l83"></a>
<a name="l84"></a></span><span class=cF1>U0</span><span class=cF0> MoveDisks(</span><span class=cF9>I64</span><span class=cF0> src_pole,</span><span class=cF9>I64</span><span class=cF0> dst_pole,</span><span class=cF9>I64</span><span class=cF0> num)
<a name="l85"></a>{
<a name="l86"></a> </span><span class=cF9>I64</span><span class=cF0> top,x,y;
<a name="l87"></a> </span><span class=cF1>if</span><span class=cF0> (num&gt;1)
<a name="l88"></a> MoveDisks(src_pole,OtherPole</span><span class=cF7>(</span><span class=cF0>src_pole,dst_pole</span><span class=cF7>)</span><span class=cF0>,num-1);
<a name="l89"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l90"></a> </span><span class=cF6>&quot;$CM+BY,0,0$Disk:%d from %d to %d\n&quot;</span><span class=cF0>,TopDisk(src_pole),src_pole,dst_pole;
<a name="l91"></a>
<a name="l92"></a> top=TopDisk(src_pole);
<a name="l93"></a> </span><span class=cF1>for</span><span class=cF0> (y=disks_y[top];
<a name="l94"></a> y&gt;</span><span class=cF5>Fs</span><span class=cF0>-&gt;pix_height-PEDESTAL_HEIGHT-</span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT+1</span><span class=cF7>)</span><span class=cF0>/2
<a name="l95"></a> -</span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT+1</span><span class=cF7>)</span><span class=cF0>*</span><span class=cF7>(</span><span class=cF0>DISKS_NUM+2</span><span class=cF7>)</span><span class=cF0>;y--) </span><span class=cF7>{</span><span class=cF0>
<a name="l96"></a> disks_y[top]=y;
<a name="l97"></a> MySleep;
<a name="l98"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l99"></a> </span><span class=cF1>if</span><span class=cF0> (src_pole&lt;dst_pole)
<a name="l100"></a> </span><span class=cF1>for</span><span class=cF0> (x=poles_x[src_pole];x&lt;=poles_x[dst_pole];x++) </span><span class=cF7>{</span><span class=cF0>
<a name="l101"></a> disks_x[top]=x;
<a name="l102"></a> MySleep;
<a name="l103"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l104"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l105"></a> </span><span class=cF1>for</span><span class=cF0> (x=poles_x[src_pole];x&gt;=poles_x[dst_pole];x--) </span><span class=cF7>{</span><span class=cF0>
<a name="l106"></a> disks_x[top]=x;
<a name="l107"></a> MySleep;
<a name="l108"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l109"></a>
<a name="l110"></a> disks_pole[top]=dst_pole;
<a name="l111"></a> </span><span class=cF1>for</span><span class=cF0> (y=disks_y[top];
<a name="l112"></a> y&lt;</span><span class=cF5>Fs</span><span class=cF0>-&gt;pix_height-PEDESTAL_HEIGHT-</span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT+1</span><span class=cF7>)</span><span class=cF0>/2-1
<a name="l113"></a> -</span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT+1</span><span class=cF7>)</span><span class=cF0>*PosInStk</span><span class=cF7>(</span><span class=cF0>dst_pole,top</span><span class=cF7>)</span><span class=cF0>;y++) </span><span class=cF7>{</span><span class=cF0>
<a name="l114"></a> disks_y[top]=y;
<a name="l115"></a> MySleep;
<a name="l116"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l117"></a> SetDisksRestXY;
<a name="l118"></a> </span><span class=cF1>if</span><span class=cF0> (num&gt;1)
<a name="l119"></a> MoveDisks(OtherPole</span><span class=cF7>(</span><span class=cF0>src_pole,dst_pole</span><span class=cF7>)</span><span class=cF0>,dst_pole,num-1);
<a name="l120"></a>}
<a name="l121"></a>
<a name="l122"></a></span><span class=cF1>U0</span><span class=cF0> Init()
<a name="l123"></a>{
<a name="l124"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l125"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;3;i++)
<a name="l126"></a> poles_x[i]=(1+i)*</span><span class=cF5>Fs</span><span class=cF0>-&gt;pix_width/4;
<a name="l127"></a>
<a name="l128"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;DISKS_NUM;i++)
<a name="l129"></a> disks_pole[i]=0;
<a name="l130"></a>
<a name="l131"></a> SetDisksRestXY;
<a name="l132"></a>}
<a name="l133"></a>
<a name="l134"></a></span><span class=cF1>U0</span><span class=cF0> Hanoi()
<a name="l135"></a>{
<a name="l136"></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="l137"></a> Init;
<a name="l138"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l139"></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="l140"></a>
<a name="l141"></a> </span><span class=cF5>Sleep</span><span class=cF0>(1000);
<a name="l142"></a> </span><span class=cF1>try</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l143"></a> MoveDisks(0,2,DISKS_NUM);
<a name="l144"></a> </span><span class=cF5>Beep</span><span class=cF0>; </span><span class=cF5>Beep</span><span class=cF0>;
<a name="l145"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l146"></a> </span><span class=cF5>DocBottom</span><span class=cF0>;
<a name="l147"></a> </span><span class=cF5>PressAKey</span><span class=cF0>;
<a name="l148"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>catch</span><span class=cF0>
<a name="l149"></a> </span><span class=cF5>PutExcept</span><span class=cF0>;
<a name="l150"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l151"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l152"></a>}
<a name="l153"></a>
<a name="l154"></a>Hanoi;
</span></pre></body>
</html>