forked from 404name/C-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path汉诺塔演示程序
159 lines (148 loc) · 3.86 KB
/
汉诺塔演示程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*------------------------------------
project : 汉诺塔演示软件;
language: C语言
author: CTGU_LLZ
------------------------------------0-*/
#include<stdio.h>
#include<windows.h>
int len,width,left,mid,right,time;
char map[20][1000];
int next[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; //上0下1左2右3;
int turn[2][3] = { {0,3,1}, //a -> b b -> c a -> c 上右下
{0,2,1}}; //b -> a c -> b c -> a 上左下
void gotoxy(int x,int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
/*void show(){
//system("cls"); //清屏效果慢
gotoxy(0,0); //容易出现东西卡顿
for(int i = 0; i <= width; i++){
for(int j = 0; j <= len; j++)
printf("%c",map[i][j]);
printf("\n");
}
Sleep(time);
}*/
int init(){
system("color 30");
printf("请输入你要递归的\n汉诺塔数目_");
int n;
scanf("%d",&n);
time = 100 / n;
left = 1,mid = 2 * (n + 1),right = mid + 2 * n + 1;
len = 3 * (2 * n + 1);
width = n + 1;
for( int i = 0; i <= width; i++){
for( int j = 0,num = i; j <= len; j++){
if( i == 0 || j == 0 || i == width || j == len)
map[i][j] = '#';
if( (i > 1 && i < width) &&( j == mid - 1 || j == right - 1))
map[i][j] = '|';
if( num && (i >= 1 && i <= width - 1) && j % 2!=0 && j < mid){
map[i][j] = -95;
map[i][j+1] = -10; //打印 ■ ■占2个字节可以拆开来
num--;
}
}
}
gotoxy(0,0); //容易出现东西卡顿
for(int i = 0; i <= width; i++){
for(int j = 0; j <= len; j++)
printf("%c",map[i][j]);
printf("\n");
}
return n;
}
void play(int x,int y){
int turn_0,n = 0,i,j,k,tx,ty,flag = 0;
if((x == mid && y == right)||(x == left && y == mid) ||(x == left && y == right))
turn_0 = 0; //往右
else if((x == mid && y == left)||(x == right && y == left)||(x == right && y == mid))
turn_0 = 1; //往左
for(i = 1,j = y;i <= width; i++){ //目的地
if(map[i][j]!=0){
tx = i - 1;
ty = j;
break;
}
}
for(i = 1,j = x;i <= width; i++){ //出发点
if(map[i][j]!=0){
break;
}
}
while(1){
while(( i!=1 || j!= x) &&(i!=1 || j!= y) && (i!= tx || j != ty)){
if(turn_0 == 0)
for( k = mid - 3; k >= 0 ; k--){
map[ i + next[turn[turn_0][n]][0] ][ j + next[turn[turn_0][n]][1] + k] = map[i][j + k];
map[i][j + k] = 0;
}
else
for( k = 0; k < mid - 2 ; k++){
map[ i + next[turn[turn_0][n]][0] ][ j + next[turn[turn_0][n]][1] + k] = map[i][j + k];
map[i][j + k] = 0;
}
gotoxy(j,i);
Sleep(time);
for( k = 0; k < mid - 2 ; k++ ){
printf(" ");
}
i = i + next[turn[turn_0][n]][0];
j = j + next[turn[turn_0][n]][1];
gotoxy(j,i);
Sleep(time);
for( k = 0; k < mid - 2 ; k++ ){
printf("%c",map[i][j + k]);
}
}
n++; //改变方向;
if( i == tx && j == ty)
return;
if(turn_0 == 0)
for( k = mid - 3; k >= 0 ; k--){
map[ i + next[turn[turn_0][n]][0] ][ j + next[turn[turn_0][n]][1] + k] = map[i][j + k];
map[i][j + k] = 0;
}
else
for( k = 0; k < mid - 2 ; k++){
map[ i + next[turn[turn_0][n]][0] ][ j + next[turn[turn_0][n]][1] + k] = map[i][j + k];
map[i][j + k] = 0;
}
gotoxy(j,i);
for( k = 0; k < mid - 2 ; k++ ){
printf(" ");
}
Sleep(time);
i = i + next[turn[turn_0][n]][0];
j = j + next[turn[turn_0][n]][1];
gotoxy(j,i);
for( k = 0; k < mid - 2 ; k++ )
printf("%c",map[i][j + k]);
Sleep(time);
}
}
void move(int a,int b,int c,char aa,char bb,char cc,int n){
if(n == 1){
gotoxy(0,width+1);
printf("from %c to %c",aa,cc);
play(a,c);
return;
}
move(a,c,b,aa,cc,bb,n-1);
gotoxy(0,width+1);
printf("from %c to %c",aa,cc);
play(a,c);
move(b,a,c,bb,aa,cc,n-1);
gotoxy(0,width+1);
}
int main(){
int n = init();
move(left,mid,right,'a','b','c',n);
return 0;
}