<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=US-ASCII"> <meta name="generator" content="ZealOS V1.08"> <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=cF0>#</span><span class=cF1>define</span><span class=cF0> DISKS_NUM </span><span class=cFE>6</span><span class=cF0> <a name="l2"></a>#</span><span class=cF1>define</span><span class=cF0> PEDESTAL_HEIGHT </span><span class=cFE>20</span><span class=cF0> <a name="l3"></a>#</span><span class=cF1>define</span><span class=cF0> DISK_HEIGHT </span><span class=cFE>7</span><span class=cF0> <a name="l4"></a>#</span><span class=cF1>define</span><span class=cF0> DISK_UNIT_WIDTH </span><span class=cFE>5</span><span class=cF0> <a name="l5"></a> <a name="l6"></a></span><span class=cF9>I64</span><span class=cF0> poles_x[</span><span class=cFE>3</span><span class=cF0>]; <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> </span><span class=cFE>3</span><span class=cF0> - 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> <a name="l18"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < DISKS_NUM; i++) <a name="l19"></a> </span><span class=cF1>if</span><span class=cF0> (disks_pole[i] == pole) <a name="l20"></a> </span><span class=cF1>return</span><span class=cF0> i; <a name="l21"></a> <a name="l22"></a> </span><span class=cF1>return</span><span class=cF0> -</span><span class=cFE>1</span><span class=cF0>; <a name="l23"></a>} <a name="l24"></a> <a name="l25"></a></span><span class=cF9>I64</span><span class=cF0> PosInStack(</span><span class=cF9>I64</span><span class=cF0> pole, </span><span class=cF9>I64</span><span class=cF0> disk) <a name="l26"></a>{ <a name="l27"></a> </span><span class=cF9>I64</span><span class=cF0> res = </span><span class=cFE>0</span><span class=cF0>, i; <a name="l28"></a> <a name="l29"></a> </span><span class=cF1>for</span><span class=cF0> (i = DISKS_NUM - </span><span class=cFE>1</span><span class=cF0>; i > disk; i--) <a name="l30"></a> </span><span class=cF1>if</span><span class=cF0> (disks_pole[i] == pole) <a name="l31"></a> res++; <a name="l32"></a> <a name="l33"></a> </span><span class=cF1>return</span><span class=cF0> res; <a name="l34"></a>} <a name="l35"></a> <a name="l36"></a></span><span class=cF1>U0</span><span class=cF0> SetDisksRestXY() <a name="l37"></a>{ <a name="l38"></a> </span><span class=cF9>I64</span><span class=cF0> i; <a name="l39"></a> <a name="l40"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < DISKS_NUM; i++) <a name="l41"></a> </span><span class=cF7>{</span><span class=cF0> <a name="l42"></a> disks_x[i] = poles_x[disks_pole[i]]; <a name="l43"></a> disks_y[i] = </span><span class=cF5>Fs</span><span class=cF0>->pix_height - <a name="l44"></a> PEDESTAL_HEIGHT - (DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF0>) / </span><span class=cFE>2</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0> - (DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF0>) * PosInStack(disks_pole[i], i); <a name="l45"></a> </span><span class=cF7>}</span><span class=cF0> <a name="l46"></a>} <a name="l47"></a> <a name="l48"></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="l49"></a>{ <a name="l50"></a> </span><span class=cF9>I64</span><span class=cF0> i; <a name="l51"></a> <a name="l52"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < </span><span class=cFE>3</span><span class=cF0>; i++) <a name="l53"></a> poles_x[i] = (</span><span class=cFE>1</span><span class=cF0> + i) * task->pix_width / </span><span class=cFE>4</span><span class=cF0>; <a name="l54"></a> <a name="l55"></a> dc->color = </span><span class=cF3>BLACK</span><span class=cF0>; <a name="l56"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc, poles_x[</span><span class=cFE>0</span><span class=cF0>] - </span><span class=cFE>50</span><span class=cF0>, task->pix_height - PEDESTAL_HEIGHT, <a name="l57"></a> poles_x[</span><span class=cFE>2</span><span class=cF0>] - poles_x[</span><span class=cFE>0</span><span class=cF0>] + </span><span class=cFE>100</span><span class=cF0>, PEDESTAL_HEIGHT - </span><span class=cF3>FONT_HEIGHT</span><span class=cF0>); <a name="l58"></a> dc->color = </span><span class=cF3>DKGRAY</span><span class=cF0>; <a name="l59"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc, poles_x[</span><span class=cFE>0</span><span class=cF0>] - </span><span class=cFE>49</span><span class=cF0>, task->pix_height - PEDESTAL_HEIGHT + </span><span class=cFE>1</span><span class=cF0>, <a name="l60"></a> poles_x[</span><span class=cFE>2</span><span class=cF0>] - poles_x[</span><span class=cFE>0</span><span class=cF0>] + </span><span class=cFE>98</span><span class=cF0>, PEDESTAL_HEIGHT - </span><span class=cF3>FONT_HEIGHT</span><span class=cF0> - </span><span class=cFE>2</span><span class=cF0>); <a name="l61"></a> <a name="l62"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < </span><span class=cFE>3</span><span class=cF0>; i++) <a name="l63"></a> </span><span class=cF7>{</span><span class=cF0> <a name="l64"></a> dc->color = </span><span class=cF3>BLACK</span><span class=cF0>; <a name="l65"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc, poles_x[i] - </span><span class=cFE>3</span><span class=cF0>, <a name="l66"></a> task->pix_height - PEDESTAL_HEIGHT - </span><span class=cF7>(</span><span class=cF0>DISKS_NUM + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0>, <a name="l67"></a> </span><span class=cFE>7</span><span class=cF0>, </span><span class=cF7>(</span><span class=cF0>DISKS_NUM + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0>); <a name="l68"></a> dc->color = </span><span class=cF3>YELLOW</span><span class=cF0>; <a name="l69"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc, poles_x[i] - </span><span class=cFE>2</span><span class=cF0>, <a name="l70"></a> task->pix_height - PEDESTAL_HEIGHT + </span><span class=cFE>1</span><span class=cF0> - </span><span class=cF7>(</span><span class=cF0>DISKS_NUM + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0>, <a name="l71"></a> </span><span class=cFE>5</span><span class=cF0>, </span><span class=cF7>(</span><span class=cF0>DISKS_NUM + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0>); <a name="l72"></a> </span><span class=cF7>}</span><span class=cF0> <a name="l73"></a> <a name="l74"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < DISKS_NUM; i++) <a name="l75"></a> </span><span class=cF7>{</span><span class=cF0> <a name="l76"></a> dc->color = </span><span class=cF3>BLACK</span><span class=cF0>; <a name="l77"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc, disks_x[i] - </span><span class=cF7>(</span><span class=cF0>i + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * DISK_UNIT_WIDTH, <a name="l78"></a> disks_y[i] - DISK_HEIGHT / </span><span class=cFE>2</span><span class=cF0>, </span><span class=cF7>(</span><span class=cF0>i + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF7>(</span><span class=cF0>DISK_UNIT_WIDTH * </span><span class=cFE>2</span><span class=cF7>)</span><span class=cF0> + </span><span class=cFE>1</span><span class=cF0>, DISK_HEIGHT); <a name="l79"></a> dc->color = </span><span class=cFB>gr_rainbow_10</span><span class=cF0>[i]; <a name="l80"></a> </span><span class=cF5>GrRect</span><span class=cF0>(dc, disks_x[i] - </span><span class=cF7>(</span><span class=cF0>i + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * DISK_UNIT_WIDTH + </span><span class=cFE>1</span><span class=cF0>, <a name="l81"></a> disks_y[i] - DISK_HEIGHT / </span><span class=cFE>2</span><span class=cF0> + </span><span class=cFE>1</span><span class=cF0>, </span><span class=cF7>(</span><span class=cF0>i + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF7>(</span><span class=cF0>DISK_UNIT_WIDTH * </span><span class=cFE>2</span><span class=cF7>)</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0>, DISK_HEIGHT - </span><span class=cFE>2</span><span class=cF0>); <a name="l82"></a> </span><span class=cF7>}</span><span class=cF0> <a name="l83"></a>} <a name="l84"></a> <a name="l85"></a></span><span class=cF1>U0</span><span class=cF0> MySleep() <a name="l86"></a>{ <a name="l87"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>CharScan</span><span class=cF0>) <a name="l88"></a> </span><span class=cF5>throw</span><span class=cF0>; <a name="l89"></a> </span><span class=cF5>Sleep</span><span class=cF0>(</span><span class=cFE>3</span><span class=cF0>); <a name="l90"></a>} <a name="l91"></a> <a name="l92"></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="l93"></a>{ <a name="l94"></a> </span><span class=cF9>I64</span><span class=cF0> top, x, y; <a name="l95"></a> <a name="l96"></a> </span><span class=cF1>if</span><span class=cF0> (num > </span><span class=cFE>1</span><span class=cF0>) <a name="l97"></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 - </span><span class=cFE>1</span><span class=cF0>); <a name="l98"></a> </span><span class=cF5>DocClear</span><span class=cF0>; <a name="l99"></a> </span><span class=cF6>"$CM+BY,0,0$Disk:%d from %d to %d\n"</span><span class=cF0>, TopDisk(src_pole), src_pole, dst_pole; <a name="l100"></a> <a name="l101"></a> top = TopDisk(src_pole); <a name="l102"></a> </span><span class=cF1>for</span><span class=cF0> (y = disks_y[top]; <a name="l103"></a> y > </span><span class=cF5>Fs</span><span class=cF0>->pix_height - PEDESTAL_HEIGHT - </span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> / </span><span class=cFE>2</span><span class=cF0> - </span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF7>(</span><span class=cF0>DISKS_NUM + </span><span class=cFE>2</span><span class=cF7>)</span><span class=cF0>; <a name="l104"></a> y--) <a name="l105"></a> </span><span class=cF7>{</span><span class=cF0> <a name="l106"></a> disks_y[top] = y; <a name="l107"></a> MySleep; <a name="l108"></a> </span><span class=cF7>}</span><span class=cF0> <a name="l109"></a> </span><span class=cF1>if</span><span class=cF0> (src_pole < dst_pole) <a name="l110"></a> </span><span class=cF1>for</span><span class=cF0> (x = poles_x[src_pole]; x <= poles_x[dst_pole]; x++) <a name="l111"></a> </span><span class=cF7>{</span><span class=cF0> <a name="l112"></a> disks_x[top] = x; <a name="l113"></a> MySleep; <a name="l114"></a> </span><span class=cF7>}</span><span class=cF0> <a name="l115"></a> </span><span class=cF1>else</span><span class=cF0> <a name="l116"></a> </span><span class=cF1>for</span><span class=cF0> (x = poles_x[src_pole]; x >= poles_x[dst_pole]; x--) <a name="l117"></a> </span><span class=cF7>{</span><span class=cF0> <a name="l118"></a> disks_x[top] = x; <a name="l119"></a> MySleep; <a name="l120"></a> </span><span class=cF7>}</span><span class=cF0> <a name="l121"></a> <a name="l122"></a> disks_pole[top] = dst_pole; <a name="l123"></a> </span><span class=cF1>for</span><span class=cF0> (y = disks_y[top]; <a name="l124"></a> y < </span><span class=cF5>Fs</span><span class=cF0>->pix_height - PEDESTAL_HEIGHT - </span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> / </span><span class=cFE>2</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0> - </span><span class=cF7>(</span><span class=cF0>DISK_HEIGHT + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * PosInStack</span><span class=cF7>(</span><span class=cF0>dst_pole, top</span><span class=cF7>)</span><span class=cF0>; <a name="l125"></a> y++) <a name="l126"></a> </span><span class=cF7>{</span><span class=cF0> <a name="l127"></a> disks_y[top] = y; <a name="l128"></a> MySleep; <a name="l129"></a> </span><span class=cF7>}</span><span class=cF0> <a name="l130"></a> SetDisksRestXY; <a name="l131"></a> </span><span class=cF1>if</span><span class=cF0> (num > </span><span class=cFE>1</span><span class=cF0>) <a name="l132"></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 - </span><span class=cFE>1</span><span class=cF0>); <a name="l133"></a>} <a name="l134"></a> <a name="l135"></a></span><span class=cF1>U0</span><span class=cF0> Init() <a name="l136"></a>{ <a name="l137"></a> </span><span class=cF9>I64</span><span class=cF0> i; <a name="l138"></a> <a name="l139"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < </span><span class=cFE>3</span><span class=cF0>; i++) <a name="l140"></a> poles_x[i] = (</span><span class=cFE>1</span><span class=cF0> + i) * </span><span class=cF5>Fs</span><span class=cF0>->pix_width / </span><span class=cFE>4</span><span class=cF0>; <a name="l141"></a> <a name="l142"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < DISKS_NUM; i++) <a name="l143"></a> disks_pole[i] = </span><span class=cFE>0</span><span class=cF0>; <a name="l144"></a> <a name="l145"></a> SetDisksRestXY; <a name="l146"></a>} <a name="l147"></a> <a name="l148"></a></span><span class=cF1>U0</span><span class=cF0> Hanoi() <a name="l149"></a>{ <a name="l150"></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.ZC.html#l3"><span class=cF4>SettingsPush</span></a><span class=cF0> <a name="l151"></a> Init; <a name="l152"></a> </span><span class=cF5>DocClear</span><span class=cF0>; <a name="l153"></a> </span><span class=cF5>Fs</span><span class=cF0>->draw_it = &</span><span class=cF5>DrawIt</span><span class=cF0>; <a name="l154"></a> <a name="l155"></a> </span><span class=cF5>Sleep</span><span class=cF0>(</span><span class=cFE>1000</span><span class=cF0>); <a name="l156"></a> </span><span class=cF1>try</span><span class=cF0> <a name="l157"></a> </span><span class=cF7>{</span><span class=cF0> <a name="l158"></a> MoveDisks(</span><span class=cFE>0</span><span class=cF0>, </span><span class=cFE>2</span><span class=cF0>, DISKS_NUM); <a name="l159"></a> </span><span class=cF5>Beep</span><span class=cF0>; </span><span class=cF5>Beep</span><span class=cF0>; <a name="l160"></a> </span><span class=cF5>DocClear</span><span class=cF0>; <a name="l161"></a> </span><span class=cF5>DocBottom</span><span class=cF0>; <a name="l162"></a> </span><span class=cF5>PressAKey</span><span class=cF0>; <a name="l163"></a> </span><span class=cF7>}</span><span class=cF0> <a name="l164"></a> </span><span class=cF1>catch</span><span class=cF0> <a name="l165"></a> </span><span class=cF5>PutExcept</span><span class=cF0>; <a name="l166"></a> </span><span class=cF5>DocClear</span><span class=cF0>; <a name="l167"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>; <a name="l168"></a>} <a name="l169"></a> <a name="l170"></a>Hanoi; </span></pre></body> </html>