Hanoi tower, problem with number of elements

paweuuu's Avatar, Join Date: Jan 2012
Newbie Member
hello! I have some problems with Hanoi Tower. I want to do that user can define number of elements (i tried printf->scanf but it doesnt work). There is only possibility to change number of elements in code but thats useless for me. Can you help me? Here is code:
Code:
#include <stdio.h> 
#include <conio.h> 
//#include <dos.h> 

#include <windows.h> 

HANDLE g_hConsol = GetStdHandle(STD_OUTPUT_HANDLE); 

void gotoxy(const int x, const int y) 
{ 
COORD coord = {x, y}; 
SetConsoleCursorPosition(g_hConsol, coord); 
} 


void clrscr() 
{ 
COORD ZEROPOINT={0,0}; 
DWORD written; 

FillConsoleOutputCharacter (GetStdHandle 
(STD_OUTPUT_HANDLE), ' ', 
2000, ZEROPOINT, &written); 
gotoxy (1, 1); 
} 


typedef unsigned int uint; 
//#define NUMB_OF_ELEM 10 


//int towers[NUMB_OF_ELEM][3]; 

void main2() { 
//int NUMB_OF_ELEM = 10; 
//int z; 
//scanf ("%d", &z); 

#define NUMB_OF_ELEM 6 

} 
int towers[NUMB_OF_ELEM][3]; 
void prnt() 
{ 
int j, k, posx, posy; 
int i; 

clrscr(); 

for(j = 0; j < 3; j++) { 
for(i = NUMB_OF_ELEM-1; i >= 0; i--) { 
posy = 20 - i; 
posx = 20 + (j * 20); 
for(k = 1; k <= towers[i][j]; k++) { 
gotoxy(posx-k, posy); 
putch('-'); 
gotoxy(posx+k, posy); 
putch('-'); 
} 
gotoxy(posx, posy); 
if(towers[i][j]) 
printf("%d", towers[i][j]); 
else 

putch('|'); 
} 
} 
} 



void move(uint s1, uint dst) 
{ 
int i, is, id; 

for(i = NUMB_OF_ELEM-1; i >= 0; i--) { 
if(towers[i][s1]) { 
is = i; 
break; 
} 
} 

for(i = 0; i < NUMB_OF_ELEM; i++) { 
if(!towers[i][dst]) { 
id = i; 
break; 
} 
} 

towers[id][dst] = towers[is][s1]; 
towers[is][s1] = 0; 

prnt(); 
gotoxy(1,1); 
printf("Move disk %u from %u to %u.", towers[id][dst], s1, dst); 
getch(); 
} 


int mover(uint p1, uint s1, uint dst) 
{ 
int i, is, id; 

for(i = NUMB_OF_ELEM-1; i >= 0; i--) { 
if(towers[i][s1]) { 
is = i; 
break; 
} 
} 

for(i = NUMB_OF_ELEM-1; i >= 0; i--) { 
if(towers[i][dst]) { 
id = i; 
break; 

} 
} 

return (towers[is][s1] == p1) 
&& ((towers[0][dst] == 0) 
|| towers[id][dst] > towers[is][s1]); 

} 


uint o1(uint p1) 
{ 
int i, j; 
for(j = 0; j < 3; j++) { 
for(i = NUMB_OF_ELEM-1; i >= 0; i--) { 

if(towers[i][j] == p1) { 
return j; 
} 
} 
} 
return -1; 
} 


void ranoi(uint n, uint s1, uint dst) 
{ 
uint i, j, jog = 0, s2, dtemp, p1,auxtemp; 
int t1 = 1; 

if(s1 > 2 || dst > 2 || s1 == dst || n == 0) 
return; 


for(i = 0; i < n; i++) { 
jog = (2 * jog) + 1; 
} 

if((n % 2) == 0) 
t1 = -1; 

p1 = 1; 

for(i = 0; i < jog; i++) { 
s2 = o1(p1); 
if(p1 % 2) { 

dtemp = ((3 + s2) + t1) % 3; 
} else { 
if(mover(p1, s2, (s2 + 1)%3)) 
dtemp = (s2 + 1)%3; 
else 
dtemp = (s2 + 2)%3; 
} 

move(s2, dtemp); 

p1 = (p1 % n) + 1; 
s2 = o1(p1); 
while(!mover(p1, s2, (s2 + 1)%3) && !mover(p1, s2, (s2 + 2)%3)) { 

p1 = (p1 % n) + 1; 
s2 = o1(p1); 
} 
} 
} 



int main() 
{ 
main2(); 
uint i, j, s1 = 0, dst = 1; 

for(j = 0; j < 3; j++) { 
for(i = 0; i < NUMB_OF_ELEM; i++) { 
if(j == s1) 

towers[i][j] = NUMB_OF_ELEM - i; 
else 
towers[i][j] = 0; 
} 
} 

prnt(); 
gotoxy(1,1); 
printf("Press any key", s1, dst); 
getch(); 
ranoi(NUMB_OF_ELEM, s1, dst); 
gotoxy(1,1); 
printf("Ready!", s1, dst); 

getch(); 

return 0; 
}
0
Scripting's Avatar, Join Date: Jun 2010
John Hoder
I can't see that exact line, can you point me exact code you have troubles with?