实训第三天--数字迷宫

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <conio.h>
# include <time.h>
# include "windows.h"

char c=1;             
int x,y,t1,t2;               
int count=7; 
int code[17][24]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,8,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,
      0,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,-1,0,0,0,0,0,-1,-1,-1,0,0,
      0,-1,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,-1,0,0,0,0,
      0,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,0,-1,0,-1,-1,-1,-1,-1,-1,-1,0,0,
      0,-1,0,0,-1,0,-1,0,0,0,0,-1,0,-1,-1,-1,0,0,0,0,0,0,0,0,
         0,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0,0,-1,0,0,0,0,0,0,0,0,
      0,-1,0,0,0,0,0,0,0,0,0,-1,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,0,
      0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,-1,0,0,0,0,
      0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,-1,0,0,-1,0,0,0,0,
      0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,-1,-1,-1,-1,0,-1,-1,0,
      0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0,
      0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,
      0,0,-1,-1,-1,-1,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,
      0,-1,-1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,
      0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 

void SetNum()                                                   
{
    int i,x,y;
    srand((unsigned)time(NULL));            
 for(i=0;i<7;i++)
 {
  x=1+rand()%15;            
  y=1+rand()%22;                                           
  if(code[x][y]==-1)            
  {
   code[x][y]=i+1;
  }
  else            
  {
   i--;
  }
    }
}

void SetCode(int num)                                           
{
 if(num==0)                                                   
  printf("■");             
 else if(num==-1||num==8)        
  printf("  ");
 else
  printf("%d ",num);
}

void GoToXY(int x, int y)
{                                         
 COORD pos={x,y};                                              
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);                
 SetConsoleCursorPosition(hOut, pos);                         
 CONSOLE_CURSOR_INFO cursor_info = {1, 0};          
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
 
void GoToXYSpace(int x, int y)          
{
 GoToXY(x,y);
 printf("  ");
}

void SetWindow()                                                 
{
 int i,j;
 for(i=0;i<17;i++)
 {
  for(j=0;j<24;j++)
  {
   SetCode(code[i][j]);
  }
  printf("\n");
 }
 GoToXY(2,1);             
 printf("%c ",c);
}

void UpKey()               
{
 t1=x;             
 t2=y;
 x=x;
 y=y-1;
 if(code[y][x/2]==0)             
 {
  x=t1;
  y=t2;
 }
 else
 {
  GoToXYSpace(t1,t2);
  GoToXY(x,y);
  printf("%c ",c);
  if(code[y][x/2]!=-1)           
  {
   count--;
   code[y][x/2]=-1;
  }
 }
}
void DownKey()               
{
 t1=x;
 t2=y;
 x=x;
 y=y+1;
 if(code[y][x/2]==0)
 {
  x=t1;
  y=t2;
 }
 else
 {
  GoToXYSpace(t1,t2);
  GoToXY(x,y);
  printf("%c ",c);
  if(code[y][x/2]!=-1)
  {
   count--;
   code[y][x/2]=-1;
  }
 }
}

void LeftKey()                
{
 t1=x;
 t2=y;
 x=x-2;
 y=y;
 if(code[y][x/2]==0)
 {
  x=t1;
  y=t2;
 }
 else
 {
  GoToXYSpace(t1,t2);
  GoToXY(x,y);
  printf("%c ",c);
  if(code[y][x/2]!=-1)
  {
   count--;
   code[y][x/2]=-1;
  }
 }
}
void RightKey()               
{  
 t1=x;
 t2=y;
 x=x+2;
 y=y;
 if(code[y][x/2]==0)
 {
  x=t1;
  y=t2;
 }
 else
 {
  GoToXYSpace(t1,t2);
  GoToXY(x,y);
  printf("%c ",c);
  if(code[y][x/2]!=-1)
  {
   count--;
   code[y][x/2]=-1;
  }
 }
}
int CheckLeftNum(int count)             
{
 if(count>=0)
  return count;
}

void Move()                 
{
 char ch;
 x=2,y=1,count=7;             
 while(ch=getch())
 {
  if(ch==27)               
  {
   count=100;
   break;  
  }
  if(ch==72)        
   UpKey();
  else if(ch==80)
   DownKey();
  else if(ch==75)
   LeftKey();
  else if(ch==77)
   RightKey();
  CheckLeftNum(count);
  if(count==0)                
  {
   count=7;
   system("cls");           
   break; 
  }
 }
}

int main()                
{
 char ch;
 while(count==7)              
 {
  GoToXY(30,20);
  printf("按上下左右移动\n");
  GoToXY(30,21);
  printf("按ESC退出\n");
  GoToXY(0,0);
  SetNum();             
  SetWindow();                  
  Move();               
 }
 GoToXY(0,17);
 return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值