home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Vectronix 2
/
VECTRONIX2.iso
/
FILES_01
/
LATTIC_3.LZH
/
EXAMPLES
/
HANOI.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-11
|
3KB
|
132 lines
/*
* The Towers of Hanoi program in Lattice C
*
* lc -csf -v -w -rr -O -Lavg hanoi.c
*
* Copyright (c) 1990 HiSoft
*/
#include <aes.h>
#include <vdi.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define max_rings 25
#define left 0
#define middle 1
#define right 2
#define space 50
#define gap 10
/* v_opnvwk input array */
short work_in[11]={1,1,1,1,1,1,1,1,1,1,2};
/* v_opnvwk output array */
short work_out[57];
short handle; /* virtual workstation handle */
unsigned int ring_height,full_height,max_width,pole[3];
short highest[3];
unsigned int (*poles)[3];
void draw_ring(unsigned int which_pole,unsigned int size,enum {DRAW=2,ERASE=0} type,unsigned int start)
{
unsigned int xstart,ystart;
short xy[4];
vsf_interior(handle,(int)type);
xstart=pole[which_pole]-size/2;
ystart=full_height-space-start*ring_height;
xy[0]=xstart;
xy[1]=ystart;
xy[2]=xstart+size;
xy[3]=ystart+ring_height-2;
if (type==ERASE)
vr_recfl(handle,xy);
else
v_bar(handle,xy);
}
void realmove(unsigned int source, unsigned int destination)
{
unsigned int ring_width;
ring_width=poles[highest[source]-1][source];
/* erase source ring */
draw_ring(source,ring_width,ERASE,highest[source]);
poles[highest[source]-1][source]=0;
highest[source]--;
/* draw destination ring */
highest[destination]++;
poles[highest[destination]-1][destination]=ring_width;
vsf_style(handle,highest[destination]);
draw_ring(destination,ring_width,DRAW,highest[destination]);
}
void move(unsigned int howmany, unsigned int source, unsigned int work, unsigned int destination)
{
if (howmany<=1)
realmove(source,destination);
else
{
move(howmany-1,source,destination,work);
realmove(source,destination);
move(howmany-1,work,source,destination);
}
}
int main(void)
{
short junk; /* unused variable */
unsigned int i,num_rings;
clock_t tm;
appl_init(); /* start AES */
handle=graf_handle(&junk,&junk,&junk,&junk); /* find AES handle */
v_opnvwk(work_in,&handle,work_out); /* open workstation */
max_width=(work_out[0]-gap*4)/3;
full_height=work_out[1];
for (i=0; i<3; i++)
pole[i]=(max_width+gap)*i+max_width/2+gap;
do
{
printf("Number of rings to move: ");
scanf("%d",&num_rings);
}
while (num_rings<=1 || num_rings>max_rings);
poles=malloc(max_rings*sizeof(*poles));
ring_height=(full_height-2*space)/max_rings;
graf_mouse(M_OFF,NULL);
v_clrwk(handle);
vsf_color(handle,BLACK);
/* initialise first pole */
for (i=0; i<num_rings; i++)
poles[i][0]=max_width-i*(max_width/num_rings);
for (i=0; i<num_rings; i++)
{
vsf_style(handle,i+1);
highest[0]=i+1;
draw_ring(0,poles[i][0],DRAW,highest[0]);
}
tm=clock();
move(num_rings,left,middle,right);
tm=clock()-tm;
printf("%d rings moved in %ld.%03ld seconds\n",num_rings,(long)(tm/CLK_TCK),(long)(((tm%CLK_TCK)*1000)/CLK_TCK));
evnt_keybd();
v_clsvwk(handle);
graf_mouse(M_ON,NULL);
return appl_exit();
}