C language to achieve the push box game

  • 2020-06-12 10:02:43
  • OfStack

This article is an example of C language to share the implementation of the push box game specific code for your reference, the specific content is as follows


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 1000
/*
r destination  
o The box  
i people  
x wall  
  Road (space)  
w on  
a On the left  
d right  
s Under the  
*/
void menu();
int level1();
int level2();
void swap(char*,char*);
int opera(char *p,int *ren,int xzs);
void dituprint();
 
 
void menu()
{
 printf(" tuixiangzi \n");
 printf("1. A new game  2. Choose close \n");
 printf("0. exit \n");
}
 
void xuanguan()
{
 int choice_level;
 while(1)
 {
 printf(" Please select levels: \n");
 scanf("%d",&choice_level);
 switch(choice_level)
 {
 case 1:
 level1();
 break;
 case 2:
 level2();
 break;
 default:
 printf(" Input error, please re-select the level! "); 
 
 }
 }
}
 
 void swap(char *a,char *b)
 {
 
 char ch;
 ch=*a;
 *a=*b;
 *b=ch;
 }
 
 void dituprint(char *p)
 {
 
 for(int i=0;i<9;i++)
 {
  for(int j=0;j<9;j++)
  printf("%3c",p[i*9+j]);
  printf("\n");
  }
 }
 int opera(char *p,int *ren,int xzs)
 {
 dituprint(p);
 while(1)
 {
 for(int i=1,panduan=0;i<=xzs;i++)
 {
 if(p[ren[i]]=='o')
  {
  panduan+=1;
  printf("panduan=%d\n",panduan);
  }
 if(panduan==xzs)
  {
  printf(" Congratulations, you're through! ");
  getchar();
  return 0; 
  }
 }
 char ch;
 printf(" Your button is: ");
 scanf("%c",&ch);
 switch(ch)
 {
 case 'w':
 {
 if(p[ren[0]]=='r')
  {
  if(p[ren[0]-9]=='r')
  {
  ren[0]-=9;
  p[ren[0]='i'];
  p[ren[0]+9]='r';
  break;
  }
  if(p[ren[0]-9]=='x')
  {
  printf(" The man touched the wall   Switch to another key 1 Wow! >>>>>");
  break;
  }
  if(p[ren[0]-9]==' ')
  {
  ren[0]-=9;
  p[ren[0]='i'];
  p[ren[0]-9]='r';
  break;
  }
  if(p[ren[0]-9]=='o')
  {
  if(p[ren[0]-18]=='x'||'o')
  printf(" The box hits   Switch to another key 1 Wow! >>>>>");break;
  if(p[ren[0]-18]=='r')
   {
   p[ren[0]-18]='o';
   p[ren[0]-9]='i';
   p[ren[0]]='r';
   ren[0]-=9;
   break;      
   }
  if(p[ren[0]-18]=='o')
   printf(" I don't move   Switch to another key 1 Wow! >>>>>");break;
  if(p[ren[0]-18]==' ')
   {
   swap(&p[ren[0]-9],&p[ren[0]-18]);
   swap(&p[ren[0]],&p[ren[0]-9]);
   p[ren[0]]='r';
   ren[0]-=9;
   break;
   }  
  }  
  }
  else
  {
  if(p[ren[0]-9]=='x')
  {
   printf(" The man touched the wall   Switch to another key 2>>>>>");
   break;
  }
  if(p[ren[0]-9]==' ')
  {
   swap(&p[ren[0]],&p[ren[0]-9]);
   ren[0]-=9;
   break;    
  }
  if(p[ren[0]-9]=='o')
  {
   if(p[ren[0]-18]=='o'||p[ren[0]-18]=='x')
   {
   printf(" Go any farther   Switch to another button. Wow... \n");
   break;
   }
   if(p[ren[0]-18]=='r')
   {
   p[ren[0]-18]='o';
   p[ren[0]-9]='i';
   p[ren[0]]=' ';
   ren[0]-=9;
   break;
   }
  }  
  }
  }
  break;
 case 'a':
  {
  if(p[ren[0]]=='r')
  {
  if(p[ren[0]-1]=='r')
  {
   ren[0]-=1;
   p[ren[0]]='i';
   p[ren[0]+1]='r';
   break;
   }
  if(p[ren[0]-1]=='x')
  {
   printf(" The man touched the wall   Switch to another key 1 Wow! >>>>>");
   break;
   }
  if(p[ren[0]-1]==' ')
  {
   ren[0]-=1;
   p[ren[0]]='i';
   p[ren[0]+9]='r';
   break;
   }
  if(p[ren[0]-1]=='o')
  {
   if(p[ren[0]-2]=='o'||p[ren[0]-2]=='x')
   {
    printf(" Go any farther   Switch to another key doll... \n");
    break;
   }
   if(p[ren[0]-2]=='r')
   {
     p[ren[0]-2]='o';
     p[ren[0]-1]='i';
     p[ren[0]]=' ';
     ren[0]-=1;
     break;
     }
   if(p[ren[0]-2]==' ')
   {
    p[ren[0]-2]='o';
    p[ren[0]-1]='i';
    p[ren[0]]=' ';
    ren[0]-=1;
    break;
    }
   } 
   } 
  }
  break;
  case 's':
  {
   if(p[ren[0]]=='r')
   {
   if(p[ren[0]+9]=='r')
   {
     ren[0]+=9;
     p[ren[0]]='i';
     p[ren[0]-9]='r';
     break;
     }
   if(p[ren[0]+9]=='x')
   {
     printf(" The man touched the wall   Switch to another key 1 Wow! >>>>>");
     break;
     }
   if(p[ren[0]+9]==' ')
   {
     ren[0]+=9;
     p[ren[0]]='i';
     p[ren[0]+9]='r';
     break;
     }
   if(p[ren[0]+9]=='o')
   {
     if(p[ren[0]+18]=='x'||'o')
     {
     printf(" The box hit the wall   Switch to another key 1 Wow! >>>>>");break;
     if(p[ren[0]+18]=='r')
     {
     p[ren[0]+18]='o';
     p[ren[0]+9]='i';
     p[ren[0]]='r';
     ren[0]+=9;
     break; 
     }
     if(p[ren[0]+18]=='o')
     printf(" I don't move ah   Switch to another key 1 Wow! >>>>>");break;
     if(p[ren[0]+18]==' ')
     {
     swap(&p[ren[0]+9],&p[ren[0]+18]);
     swap(&p[ren[0]],&p[ren[0]-9]);
     p[ren[0]]='r';
     ren[0]+=9;
     break;
     }
     }
     }
   else
   {
    if(p[ren[0]+9]=='x')
    {
     printf(" People hit the wall, change the other button wow 2>>>>>");
     break;
     }
    if(p[ren[0]+9]==' ')
    {
     swap(&p[ren[0]],&p[ren[0]+9]);
     ren[0]+=9;
     break;
     }
    if(p[ren[0]+9]=='o')
    {
     if(p[ren[0]+18]=='o'||p[ren[0]+18]=='x')
     {
     printf(" Can't move, switch to other buttons wow... \n");
     break;   
     }
     if(p[ren[0]+18]=='r')
     {
     p[ren[0]+18]='o';
     p[ren[0]+9]='i';
     p[ren[0]]=' ';
     ren[0]+=9;
     break; 
     }
     if(p[ren[0]+18]==' ')
     {
     p[ren[0]+18]='o';
     p[ren[0]+9]='i';
     p[ren[0]]=' ';
     ren[0]+=9;
     break; 
     }
     }
    }
    }
    break;
    case'd':
    {
     if(p[ren[0]]=='r')
     {
     if(p[ren[0]+1]=='r')
     {
      ren[0]+=1;
      p[ren[0]]='i';
      p[ren[0]-1]='r';
      break;
     }
     if(p[ren[0]+1]=='x')
     {
      printf(" The man touched the wall   Switch to another key 1 Wow! >>>>>");
      break;
     }
     if(p[ren[0]+1]==' ')
     {
      ren[0]+=1;
      p[ren[0]]='i';
      p[ren[0]-1]='r';
      break;
     }
     if(p[ren[0]+1]=='o')
     {
      if(p[ren[0]+2]=='x'||'o')
      printf(" The box hit the wall   Switch to another key 1 Wow! >>>>>>");
      break;
      if(p[ren[0]+2]=='r')
      {
      p[ren[0]+2]='o';
      p[ren[0]+1]='i';
      p[ren[0]]='r';
      p[0]+=1;
      break;
      }
      if(p[ren[0]+2]=='o')
      printf(" I don't move   Switch to another key 1 Wow! >>>>>");break;
      if(p[ren[0]+2]==' ')
      {
      swap(&p[ren[0]+1],&p[ren[0]+2]);
      swap(&p[ren[0]],&p[ren[0]-1]);
      p[ren[0]]='r';
      ren[0]+=1;
      break; 
      } 
     }
     }
     else
     {
     if(p[ren[0]+1]=='x')
     {
      printf(" People hit the wall, change the other button wow 2>>>>>");
      break;
     }
     if(p[ren[0]+1]==' ')
     {
      swap(&p[ren[0]],&p[ren[0]+1]);
      ren[0]+=1;
      break;
     }
     if(p[ren[0]+1]=='o')
     {
     if(p[ren[0]+2]=='o'||p[ren[0]+2]=='x')
     {
      printf(" I can't go any further! Switch to another button. Wow... ");
      break; 
     }
     if(p[ren[0]+2]=='r')
     {
      p[ren[0]+2]='o';
      p[ren[0]+1]='i';
      p[ren[0]]=' ';
      ren[0]+=1;
      break; 
     }
     if(p[ren[0]+2]==' ')
     {
      p[ren[0]+2]='o';
      p[ren[0]+1]='i';
      p[ren[0]]=' ';
      ren[0]+=1;
      break; 
     }
      }
     }
    }
    break;
    default:printf(" Operation error! Please confirm and then re-operate... "); 
   }
   dituprint(p);
  } 
  /*int panduan=0;
  for(int i=1;i<xzs;i++)
  {
  if(p[ren[0]]=='o')panduan++;
  }
  if(panduan=(xzs-1)){getchar();break;}*/
  }
  return 0;
 }
 
 int level1()
 {
 printf(" The first 1 guan \n");
 char ditu1[9*9]={
 '0','1','2','3','4','5','6','7','8',
 '1',' ',' ','x','x','x',' ',' ',' ',
 '2',' ',' ','x','r','x',' ',' ',' ',
 '3',' ',' ','x',' ','x','x','x','x',
 '4','x','x','x','o','x','o','r','x',
 '5','x','r',' ','o','i','x','x','x',
 '6','x','x','x','x','o','x',' ',' ',
 '7',' ',' ',' ','x','r','x',' ',' ',
 '8',' ',' ',' ','x','x','x',' ',' ', 
  };
 int ren1[5]={50,22,47,43,68};
 opera(ditu1,ren1,4);
 level2();
 return 0;
 }
 
 int level2()
 {
 printf(" Welcome to No. 2 guan \n");
 char ditu1[9*9]={
 '0','1','2','3','4','5','6','7','8',
 '1',' ',' ','x','x','x',' ',' ',' ',
 '2',' ',' ','x','r','x',' ',' ',' ',
 '3',' ',' ','x',' ','x','x','x','x',
 '4','x','x','x','o','x','o','r','x',
 '5','x','r',' ','o','i','x','x','x',
 '6','x','x','x','x','o','x',' ',' ',
 '7',' ',' ',' ','x','r','x',' ',' ',
 '8',' ',' ',' ','x','x','x',' ',' ', 
  };
 int ren1[5]={50,22,47,43,68};
 opera(ditu1,ren1,4);
 level1();
 return 0; 
 }
 
int main()
 {
 int choice;
 menu();
 while(1)
 {
  scanf("%d",&choice);
  switch(choice)
  {
  case 0:
  exit(0);
  case 1:
  {
  level1();
  break;
  }
  case 2:
  {
  xuanguan();
  break;
  }
  default:
  {
  printf(" Operation error! Please confirm the correct operation after... \n");
  } 
  }
 }
  getchar();
  return 0;
 } 

Related articles: