Chinaunix首页 | 论坛 | 博客
  • 博客访问: 675746
  • 博文数量: 156
  • 博客积分: 3402
  • 博客等级: 中校
  • 技术积分: 1639
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-13 14:06
个人简介

业余编程爱好者

文章分类

全部博文(156)

文章存档

2014年(1)

2013年(13)

2012年(46)

2011年(38)

2010年(58)

分类: LINUX

2012-05-29 14:52:36

    发布一个汉诺塔游戏程序,在win平台上的GCC编译通过。使用A键和D键左右移动,使用S键拿起或放下汉诺塔棋子。输入>键会显示算法步骤。因为linux上不支持getch函数,暂没能移植。

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. // #include<windows.h>
  3. #define TRUE 1
  4. #define FALSE 0
  5. #define MAX 6
  6. #define ON_A 1
  7. #define ON_B 2
  8. #define ON_C 3
  9. int a_tower[MAX+1], b_tower[MAX+1], c_tower[MAX+1];
  10. int *a_top = &a_tower[MAX], *b_top = b_tower, *c_top = c_tower;
  11. int now_status = ON_A;
  12. int catch_status = FALSE;
  13.     
  14. int main()
  15. {
  16.     char c;
  17.     int max_num = MAX;
  18.     init_hanoi(max_num);
  19.     printf("\n\n\n\n\n\n\n\n\n\n");
  20.     show_hanoi();
  21.     
  22.     while(1){
  23.         while(catch_status == FALSE) {
  24. //            Sleep(100);
  25.             c = getch();
  26.             if(c == 'd'){
  27.                 switch(now_status) {
  28.                     case ON_A: now_status = ON_B; break;
  29.                     case ON_B: now_status = ON_C; break;
  30.                     case ON_C: now_status = ON_A; break;                
  31.                 }
  32.                 show_hanoi();
  33.             }
  34.             else if(c == 'a'){
  35.                 switch(now_status) {
  36.                     case ON_A: now_status = ON_C; break;
  37.                     case ON_B: now_status = ON_A; break;
  38.                     case ON_C: now_status = ON_B; break;
  39.                 }
  40.                 show_hanoi();
  41.             }
  42.             else if(c == 's') {
  43.                 catch_status = TRUE;
  44.                 show_hanoi();
  45.                 break;
  46.             }
  47.             else if(c == '>') {
  48.                 dohanio(max_num,'A','B','C');
  49.                 printf("\n\n\n\n\n\n\n\n");
  50.                 break;
  51.             }
  52.         }
  53.     
  54.         while(catch_status == TRUE) {
  55. //            Sleep(100);
  56.             c = getch();        
  57.             if(c == 'd'){
  58.                 switch(now_status) {
  59.                     case ON_A: a_to_b(); now_status = ON_B; break;
  60.                     case ON_B: b_to_c(); now_status = ON_C; break;
  61.                     case ON_C: c_to_a(); now_status = ON_A; break;
  62.                 }
  63.                 show_hanoi();
  64.             }
  65.             else if(c == 'a'){
  66.                 switch(now_status) {
  67.                     case ON_A: a_to_c(); now_status = ON_C; break;
  68.                     case ON_B: b_to_a(); now_status = ON_A; break;
  69.                     case ON_C: c_to_b(); now_status = ON_B; break;
  70.                 }
  71.                 show_hanoi();
  72.             }
  73.             else if(c == 's') {
  74.                 catch_status = FALSE;
  75.                 show_hanoi();
  76.                 break;
  77.             }
  78.             else if(c == '>') {
  79.                 dohanio(max_num,'A','B','C');
  80.                 printf("\n\n\n\n\n\n\n\n");
  81.                 break;
  82.             }
  83.         }

  84.     }
  85.     return 0;    
  86. }

  87. int a_to_b()
  88. {
  89.     if((a_top == &a_tower[0]) || (b_top == &b_tower[MAX]) || (*b_top < *a_top)) {
  90.         catch_status = FALSE;
  91.         return 0;
  92.     }
  93.     b_top++;
  94.     *b_top = *a_top;
  95.     *a_top = 0;
  96.     a_top--;
  97.     return 0;
  98. }

  99. int a_to_c()
  100. {
  101.     if((a_top == &a_tower[0]) || (c_top == &c_tower[MAX]) || (*c_top < *a_top)) {
  102.         catch_status = FALSE;
  103.         return 0;
  104.     }
  105.     c_top++;
  106.     *c_top = *a_top;
  107.     *a_top = 0;
  108.     a_top--;
  109.     return 0;
  110. }

  111. int b_to_a()
  112. {
  113.     if((b_top == &b_tower[0]) || (a_top == &a_tower[MAX]) || (*a_top < *b_top)) {
  114.         catch_status = FALSE;
  115.         return 0;
  116.     }
  117.     a_top++;
  118.     *a_top = *b_top;
  119.     *b_top = 0;
  120.     b_top--;
  121.     return 0;
  122. }
  123. int b_to_c()
  124. {
  125.     if((b_top == &b_tower[0]) || (c_top == &c_tower[MAX]) || (*c_top < *b_top)) {
  126.         catch_status = FALSE;
  127.         return 0;
  128.     }
  129.     c_top++;
  130.     *c_top = *b_top;
  131.     *b_top = 0;
  132.     b_top--;
  133.     return 0;
  134. }

  135. int c_to_a()
  136. {
  137.     if((c_top == &c_tower[0]) || (a_top == &a_tower[MAX]) || (*a_top < *c_top)) {
  138.         catch_status = FALSE;
  139.         return 0;
  140.     }
  141.     a_top++;
  142.     *a_top = *c_top;
  143.     *c_top = 0;
  144.     c_top--;
  145.     return 0;
  146. }

  147. int c_to_b()
  148. {
  149.     if((c_top == &c_tower[0]) || (b_top == &b_tower[MAX]) || (*b_top < *c_top)) {
  150.         catch_status = FALSE;
  151.         return 0;
  152.     }
  153.     b_top++;
  154.     *b_top = *c_top;
  155.     *c_top = 0;
  156.     c_top--;
  157.     return 0;
  158. }

  159. int show_hanoi()
  160. {
  161.     int i;
  162. //    printf("\t\t\t\t------Hanoi------\n");
  163.     for(i=1; i<now_status; i++)
  164.         printf("\t");
  165.     if(catch_status == FALSE)
  166.         printf("\t\t\t\t*\n");
  167.     else
  168.         printf("\t\t\t\t!\n");
  169.     for(i=MAX; i>0; i--)
  170.         printf("\t\t\t\t%d\t%d\t%d\n",a_tower[i], b_tower[i], c_tower[i]);
  171.     printf("\t\t\t\tA\tB\tC");
  172.     printf("\n\n\n\n\n\n\n\n\n\n\n");
  173.     return 0;
  174. }

  175. int init_hanoi(int max_num)
  176. {
  177.     int i;
  178.     for(i=1; i<=MAX; i++,max_num--)
  179.         a_tower[i] = max_num;
  180.     a_tower[0] = b_tower[0] = c_tower[0] = 100;
  181.     return 0;
  182. }

  183. int dohanio(int n,int a,int b,int c)
  184. {
  185.     if(n==1)
  186.         printf("%c->%c ",a,c);
  187.     else {
  188.         dohanio(n-1,a,c,b);
  189.         printf("%c->%c ",a,c);
  190.         dohanio(n-1,b,a,c);
  191.     }
  192.     return 0;
  193. }

阅读(946) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~