C Language Implementation Hanoi game _c language

Source: Internet
Author: User

Operation is: A B number A on the top of the tower on the number B. such as 1 (space) 3 back to the car.  So, can someone add my C code to the QT interface framework? Code writing is not good, maintenance is not enough, can only play 8 layers, after writing found it difficult to expand, software engineering, design pattern to be improved ....
Inside the hint input grade B used, did not achieve, we casually input a single digit can play.

Stackfunc.c

#include "STACK.h" #include <stdio.h> extern ceng ceng[size];
  Data into stack void Push_stack (stack*p,int number) {p->arr[p->head]=number;
p->head++;
  }///Initialize stack 1 void init_stack1 (stack*p) {p->head=0;
  Push_stack (p,1);
  Push_stack (p,2);
  Push_stack (p,3);
  Push_stack (p,4);
  Push_stack (p,5);
  Push_stack (p,6);
  Push_stack (p,7);
Push_stack (p,8);
  }///Initialize stack 2 3 void Init_stack2_3 (stack* p1,stack* p2) {p1->head=0;
  p1->arr[p1->head]=0;
  p2->head=0;
p2->arr[p2->head]=0;
  }//pop-up stack top element int pop_stack (stack* p) {p->head--;
Return p->arr[p->head]; }//access stack top element int top_stack (stack* p) {return p->arr[p->head-1];}//Compare size of two stack top elements int sizecmp_stack (stack* p1,stack* p
  2) {if (p1->arr[p1->head-1]>p2->arr[p2->head-1]) return 1;
  else if (p1->arr[p1->head-1]<p2->arr[p2->head-1]) return-1;
else return 0;
 
}///measure the height of the stack int high_stack (stack* p) {return p->head;} is an empty stack int empty_stack (stack* p) {return P>head==0;
 
}//whether stack full int full_stack (stack* p) {return p->head==size;}
  Initialization layer 1 void init_ceng1 (ceng* p) {p->number=1;
  p->row=size-1;
p->col=0;
  }//Initialization layer 2 void Init_ceng2 (ceng* p) {p->number=2;
  p->row=size-2;
p->col=0;
  }//Initialization layer 3 void Init_ceng3 (ceng* p) {p->number=3;
  p->row=size-3;
p->col=0;
  }//Initialization layer 4 void Init_ceng4 (ceng* p) {p->number=4;
  p->row=size-4;
p->col=0;
  }//Initialization layer 5 void Init_ceng5 (ceng*p) {p->number=5;
  p->row=size-5;
p->col=0;
  }//Initialization layer 6 void Init_ceng6 (ceng*p) {p->number=6;
  p->row=size-6;
  p->col=0;
  }//Initialization layer 7 void Init_ceng7 (ceng*p) {p->number=7;
  p->row=size-7;
p->col=0;
  }//Initialization layer 8 void Init_ceng8 (ceng*p) {p->number=8;
  p->row=size-8;
p->col=0;
   }//Move layer void Move_ceng (int level,int *nrow,int *ncol,stack*p1,stack* p2,stack* p3,int) {Stdec stack*];
   ARR[0]=P1;
   ARR[1]=P2;
  ARR[2]=P3;
  *nrow=level-1-high_stack (Arr[stdec]); *ncol=stdec;
  }//Show picture void Show_map (void) {int i,j; for (i=0;i<size;i++) {for (j=0;j<3;j++) {if (ceng[0].row==i&&ceng[0].col==j) {p    rintf ("|  1 |
     ");   }else if (ceng[1].row==i&&ceng[1].col==j) {printf ("|  2 |
     ");   }else if (ceng[2].row==i&&ceng[2].col==j) {printf ("|   3 |
     ");  }else if (ceng[3].row==i&&ceng[3].col==j) {printf ("|   4 |
     ");  }else if (ceng[4].row==i&&ceng[4].col==j) {printf ("|    5 |
     "); }else if (ceng[5].row==i&&ceng[5].col==j) {printf ("|    6 |
     "); }else if (ceng[6].row==i&&ceng[6].col==j) {printf ("|     9 2
     ");
  }else if (ceng[7].row==i&&ceng[7].col==j) {printf ("|8|");
    }else printf ("");
  printf ("\ n");
printf ("=====================================================================\n"); }

main.c

#include "STACK.h" #include <stdio.h> ceng ceng[size];
  int main () {int res=1,dec=1;
  Char con;
  int NewRow;
  int newcol;
  int step=0;
 int level=0;
    while (1) {if (level==0) {do{printf ("Please enter game level: \ n");
    scanf ("%d", &level); if (level<=0| |
    level>8) printf ("Rank range error, reenter \ n"); }while (level<=0| |
  LEVEL&GT;8);
  } level=8;
  newrow=0,newcol=0;
  Stack stack[3];
  Initialization of 3 stack init_stack1 (&stack[0]);
  Init_stack2_3 (&stack[1],&stack[2]);
  Initialize 8 layers of init_ceng1 (&ceng[0]);
  Init_ceng2 (&ceng[1]);
  Init_ceng3 (&ceng[2]);
  Init_ceng4 (&ceng[3]);
  Init_ceng5 (&ceng[4]);
  Init_ceng6 (&ceng[5]);
  Init_ceng7 (&ceng[6]);
Init_ceng8 (&ceng[7]);
  while (1) {//PRINT screen switch (level) {case 1:show_map ();
  Case 2:show_map ();
  Case 3:show_map ();
  Case 4:show_map ();
  Case 5:show_map ();
  Case 6:show_map ();
  Case 7:show_map ();
  Case 8:show_map (); while (1) {
 printf ("Game Rating:%d\n", level);
  printf ("highest level of source stack is%d ... \ n", Top_stack (&stack[res]));
  printf ("A number of height%d", stack[0].head);
  printf ("Height%d of number second", stack[1].head);
  printf ("Height%d\n of number third", stack[2].head);
  printf ("\ n the number of steps already taken is%d \ n", step);
  Select source Target scanf ("%d", &res);
  scanf ("%d", &dec);
  res--;
  dec--; if (!) ( res>=0&&res<3&&dec>=0&&dec<3))/*| |
  (Empty_stack (&stack[res]))/{printf ("\ n \ nthe input range exceeds \ n");
    }else if (Empty_stack (&stack[res)) ==1) {printf ("%d\n", Stack[0].head);
  printf ("\ n \ nthe source stack is empty \ n");
  else if (Sizecmp_stack (&stack[res],&stack[dec]) <0) {printf ("\ n \ nthe chunk cannot be placed above the small block \ n");
    }else {if (dec!=res) {printf ("\n\n\n moving layer block ... \ n");
    step++;
    Move_ceng (LEVEL,&AMP;NEWROW,&AMP;NEWCOL,&AMP;STACK[0],&AMP;STACK[1],&AMP;STACK[2],DEC);
    Ceng[top_stack (&stack[res]) -1].row=newrow;
    Ceng[top_stack (&stack[res]) -1].col=newcol; Push_stack (&stack[dec],pop_stack (&stack[rES]));
    Break
    }else {printf ("\n\n\n on easy \ \ \ n");
 
  }break;
 } show_map (); } if (Full_stack (stack+1) ==1| |
    Full_stack (stack+2) ==1) {printf ("Hanoi!\n completed");
    level++;
  Break
  } show_map ();
  printf ("Do you want to continue the game?" continue to enter Y, otherwise press the other key \ n ");
  scanf ("%*[^\n]");
  scanf ("%*c");
  scanf ("%c", &con);
if (con!= ' Y ') break;
return 0; }

STACK.h

#ifndef     __stack_h__
#define     __stack_h__
#define     SIZE    8
typedef struct stack{
  int Head;
  int arr[size];
} stack;
 
typedef struct ceng{
  int number;
  int row;
  int col;
} CEng;
 
extern CEng ceng[size];
 
into the stack
void Push_stack (stack*,int);
Initialize stack 1
void Init_stack1 (stack*);
Initialize stack 2 and 3
void Init_stack2_3 (stack*,stack*);
Out Stack
int pop_stack (stack*);
Access stack top element
int top_stack (stack*);
Compare stack top element size-1 1
int sizecmp_stack (stack*,stack*);
Judge the height of the stack
int high_stack (stack*);
Determine whether the stack is empty 1 0
int empty_stack (stack*);
Determine whether the stack is full 1 0
int full_stack (stack*);
 
Initialization layer 1
void init_ceng1 (ceng*);
Initialization Layer 2
void init_ceng2 (ceng*);
Initialization Layer 3
void Init_ceng3 (ceng*);
Initialization layer 4
void Init_ceng4 (ceng*);
Initialization layer 5
void init_ceng5 (ceng*);
Initialization layer 6
void Init_ceng6 (ceng*);
Initialization layer 7
void Init_ceng7 (ceng*);
Initialization layer 8
void Init_ceng8 (ceng*);
Move the layer block
void Move_ceng (int, int * A,int *b,stack*,stack*,stack*,int k);
Print picture
void Show_map ();

Demo pictures

The above is the entire content of this article, I hope that we can learn the C language recursive algorithm to help

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.