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

本文详细介绍了汉诺塔游戏的基本实现,并针对现有代码进行了优化。通过使用字符交互和数组操作,玩家能够通过简单的指令控制游戏进程。文章还探讨了游戏流程控制和状态更新的关键逻辑,提供了一个互动性强且易于理解的游戏示例。

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

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

#include<stdio.h>
#include<stdlib.h>
#define MAX 5      /*罗盘的层数*/
#define MAX_FACT 15

int 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");
	else
		printf("\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,以此类推。


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值