海龟图

来源:互联网 发布:天狼50软件怎么样 编辑:程序博客网 时间:2024/05/04 12:19

 

#include <stdio.h>
void move();
void print();
int floor[20][20]={0};
int px=0,py=0,pen=0,detection=0;
int main()
{
 int input;
 printf("please input number:");
 scanf("%d",&input);
 while(input!=9)
 {switch(input)
 {
  case 1 :
   pen=0;break;
  case 2 :
   pen=1;break;
  case 3 :
   detection=(detection+1)%4;break;
  case 4 : 
   detection=(detection+3)%4;break;
  case 5 :
   move();break;
  case 6 :
   print();break;
  case 9 :
   break;
  default :
   printf("error");
   break;
 }
 printf("pleasr input number again:");
 scanf("%d",&input);
 }
 return 0;
}
void print()
{
 int i,j;
 for(i=0;i<20;i++)
 {
  for(j=0;j<20;j++)
  {
   if(px==i&&py==j)
    printf("#");
   else
   { if(floor[i][j]==0)
     printf(" ");
    if(floor[i][j]==1)
     printf("*");
   }
  }
  printf("\n");
 }
}
void move()
{
 int i,n;
 printf("please inpur N:\n");
 scanf("%d",&n);
 if(detection==0)
 {
  for(i=0;i<n&&py<19;i++)
  { py++;
   if(pen==1)
    floor[px][py-1]=1;
  }
 }
 else if(1==detection)
 {
  for(i=0;i<n&&px<19;i++)
  { px++;
   if(pen==1)
    floor[px-1][py]=1;
  }
 }
 else if(2==detection)
 {
  for(i=0;i<n&&py>0;i++)
  { py--;
   if(pen==1)
    floor[px][py+1]=1;
  }
 }
 else if(3==detection)
 {
  for(i=0;i<n&&px>0;i++)
  { px--;
  
   if(pen==1)
    floor[px+1][py]=1;
  }
 }
}

原创粉丝点击