linux中汉诺塔的的c程序实验

来源:互联网 发布:方正字体侵权 淘宝 编辑:程序博客网 时间:2024/06/05 13:21

汉诺塔是一个比较简单的游戏,它的图形界面可以用3个数组构成。通过123的数字表示汉诺塔的罗盘。首先构想把a当做是左移,d是右移,s是选择,q为退出游戏。程序只是显示一个过程。

程序大致如下(有些参考网上的):

#include<stdio.h>#include<stdlib.h>#define MAX 5      /*罗盘的层数*/#define MAX_FACT 15int a_to_b();     /*数组之间的交换*/int a_to_c();int b_to_a();int b_to_c();int c_to_a();int c_to_b();int show_hanoi();     /*显示*/int dohanoi(int n,int a,int b,int c);int init_hanoi(int max_num);int is_complete( int* top );int a_tower[MAX+1], b_tower[MAX+1], c_tower[MAX+1];     /*定义数组*/int *a_top = &a_tower[MAX], *b_top = b_tower, *c_top = c_tower;     /*定义数组的指针*/int now_status = 1;int catch_status = 0;int main(){char c;int max_num = MAX;init_hanoi(max_num);printf("\n\n\n\n\n\n");printf("\t\t\t\tHanoi Game\n\t\t\t\t");printf("Enter 'q' to quit!\t\t\t'a' is left\t\t'd' is right's' is choose");show_hanoi();                                                               /*游戏之前的说明*/while(!is_complete(c_top)){while(catch_status == 0) {c = getchar();if(c == 'd'){switch(now_status) {case 1: now_status = 2; break;case 2: now_status = 3; break;case 3: now_status = 1; break;}show_hanoi();}else if(c == 'a'){switch(now_status) {case 1: now_status = 3; break;case 2: now_status = 1; break;case 3: now_status = 2; break;     /*左移或者右移,之后显示结果*/}show_hanoi();}else if(c == 's') {catch_status = 1;show_hanoi();break;}}else if(c == 'q') {printf("Thank for play!\n");exit(0);}}while(catch_status == 1) {c = getchar();if(c == 'd'){switch(now_status) {case 1: a_to_b(); now_status = 2; break;case 2: b_to_c(); now_status = 3; break;case 3: c_to_a(); now_status = 1; break;}show_hanoi();}else if(c == 'a'){switch(now_status) {case 1: a_to_c(); now_status = 3; break;case 2: b_to_a(); now_status = 1; break;case 3: c_to_b(); now_status = 2; break;}show_hanoi();}else if(c == 's') {catch_status = 0;show_hanoi();break;}else if(c == 'q') {printf("Thank for play!\n");exit(0);}}}}int a_to_b(){if((a_top == &a_tower[0]) || (b_top == &b_tower[MAX]) || (*b_top < *a_top)) {catch_status = 0;return 0;}b_top++;*b_top = *a_top;*a_top = 0;a_top--;return 0;}int a_to_c(){if((a_top == &a_tower[0]) || (c_top == &c_tower[MAX]) || (*c_top < *a_top)) {catch_status = 0;return 0;}c_top++;*c_top = *a_top;*a_top = 0;a_top--;return 0;}int b_to_a(){if((b_top == &b_tower[0]) || (a_top == &a_tower[MAX]) || (*a_top < *b_top)) {catch_status = 0;return 0;}a_top++;*a_top = *b_top;*b_top = 0;b_top--;return 0;}int b_to_c(){if((b_top == &b_tower[0]) || (c_top == &c_tower[MAX]) || (*c_top < *b_top)) {catch_status = 0;return 0;}c_top++;*c_top = *b_top;*b_top = 0;b_top--;return 0;}int c_to_a(){if((c_top == &c_tower[0]) || (a_top == &a_tower[MAX]) || (*a_top < *c_top)) {catch_status = 0;return 0;}a_top++;*a_top = *c_top;*c_top = 0;c_top--;return 0;}int c_to_b(){if((c_top == &c_tower[0]) || (b_top == &b_tower[MAX]) || (*b_top < *c_top)) {catch_status = 0;return 0;}b_top++;*b_top = *c_top;*c_top = 0;c_top--;return 0;}                                             /*a,b,c之间的变换的代码*/int show_hanoi(){int i;for(i=1; i<now_status; i++)printf("\t");if(catch_status == 0)printf("\t\t\t\t*\n");elseprintf("\t\t\t\t!\n");for(i=MAX; i>0; i--)printf("\t\t\t\t%d\t%d\t%d\n",a_tower[i], b_tower[i], c_tower[i]);printf("\t\t\t\tA\tB\tC");printf("\n\n\n\n\n\n\n\n\n\n\n");return 0;}int init_hanoi(int max_num){int i;for(i=1; i<=MAX; i++,max_num--)a_tower[i] = max_num;a_tower[0] = b_tower[0] = c_tower[0] = 100;return 0;}int is_complete( int* top ){int sum = 0;for(; top > c_tower; top-- )sum += *top;return (sum == MAX_FACT);}

程序的运行结果:

这是刚开始进入游戏的界面


先输入s,再输入d可以看到1从数组a到了数组b,以此类推。


程序还有很多问题,但是大致已经可以运行。

原创粉丝点击