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,以此類推。


程序還有很多問題,但是大致已經可以運行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章