实训第三天--数字迷宫

来源:互联网 发布:网游明星知乎 编辑:程序博客网 时间:2024/06/05 04:09

# 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
原创粉丝点击