在线版已生成好!(用 AI 改写)
懒人免安装版本
仅支持 windows 系统。直接编译,理论上不会识别为病毒,有些代码看着冗余了是为了尽量兼容旧版本 C++。
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int step;
struct point{
int x,y;
}mazepos,blockpos,spacepos,mepos,endpos;
point go[6]={{0,0},{0,-1},{-1,0},{1,0},{0,1}};
point operator + (point a,point b){
point c;
c.x=a.x+b.x;
c.y=a.y+b.y;
return c;
}
point operator - (point a,point b){
point c;
c.x=a.x-b.x;
c.y=a.y-b.y;
return c;
}
bool operator == (point a,point b){
return a.x==b.x&&a.y==b.y;
}
struct maze_block{
int pos,a[8][8];
}m[6][6];
void gotoxy(int x,int y){
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void hidecursor(){
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void ccolor(string str,string fore,string back=""){
int foreground=0,background=0;
if(fore=="red")
foreground=FOREGROUND_INTENSITY|FOREGROUND_RED;
if(fore=="green")
foreground=FOREGROUND_INTENSITY|FOREGROUND_GREEN;
if(fore=="blue")
foreground=FOREGROUND_INTENSITY|FOREGROUND_BLUE;
if(fore=="yellow")
foreground=FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN;
if(fore=="pink")
foreground=FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE;
if(fore=="cyan")
foreground=FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE;
if(fore=="white")
foreground=FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE;
if(back=="red")
background=BACKGROUND_INTENSITY|BACKGROUND_RED;
if(back=="green")
background=BACKGROUND_INTENSITY|BACKGROUND_GREEN;
if(back=="blue")
background=BACKGROUND_INTENSITY|BACKGROUND_BLUE;
if(back=="yellow")
background=BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN;
if(back=="pink")
background=BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_BLUE;
if(back=="cyan")
background=BACKGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE;
if(back=="white")
background=BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),foreground|background);
cout<<str;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);
}
void ccolor(int str,string fore,string back=""){
int foreground=0,background=0;
if(fore=="red")
foreground=FOREGROUND_INTENSITY|FOREGROUND_RED;
if(fore=="green")
foreground=FOREGROUND_INTENSITY|FOREGROUND_GREEN;
if(fore=="blue")
foreground=FOREGROUND_INTENSITY|FOREGROUND_BLUE;
if(fore=="yellow")
foreground=FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN;
if(fore=="pink")
foreground=FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE;
if(fore=="cyan")
foreground=FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE;
if(fore=="white")
foreground=FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE;
if(back=="red")
background=BACKGROUND_INTENSITY|BACKGROUND_RED;
if(back=="green")
background=BACKGROUND_INTENSITY|BACKGROUND_GREEN;
if(back=="blue")
background=BACKGROUND_INTENSITY|BACKGROUND_BLUE;
if(back=="yellow")
background=BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN;
if(back=="pink")
background=BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_BLUE;
if(back=="cyan")
background=BACKGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE;
if(back=="white")
background=BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),foreground|background);
cout<<str;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);
}
string chars(string ch,int times){
string s="";
for(int i=1;i<=times;i++)
s+=ch;
return s;
}
char scanc(){
char ch='\0';
ch=getch();
switch(tolower(ch)){
case 27:
return 0;
case 'w':
return 1;
case 'a':
return 2;
case 'd':
return 3;
case 's':
return 4;
case -32:
char ch2;
ch2=getch();
switch(ch2){
case 72:
return 5;
case 75:
return 6;
case 77:
return 7;
case 80:
return 8;
}
break;
case 'r':
return 9;
}
return -1;
}
void init(){
hidecursor();
system("mode con cols=34 lines=24");
SetConsoleTitle("Block Maze v1.2");
step=0;
mazepos.x=1;
mazepos.y=1;
blockpos.x=rand()%5+1;
blockpos.y=rand()%5+1;
spacepos.x=2;
spacepos.y=2;
mepos.x=3;
mepos.y=3;
endpos.x=rand()%5+1;
endpos.y=rand()%5+1;
for(int x=1;x<=3;x++)
for(int y=1;y<=3;y++)
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
m[x][y].a[i][j]=0;
for(int x=1;x<=3;x++)
for(int y=1;y<=3;y++)
if(x!=2||y!=2)
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
m[x][y].a[i][j]=rand()%2;
m[mazepos.x][mazepos.y].a[blockpos.y][blockpos.x]=1;
m[mepos.x][mepos.y].a[endpos.y][endpos.x]=1;
ccolor(chars(chars("+"+chars("-",10),3)+"+\n"+chars("|"+chars(chars(" ",10)+"|",2)+chars(" ",10)+"|\n",5),3)+chars("+"+chars("-",10),3)+"+\nStep:\nwsad控制小人\n上下左右控制迷宫\n不保证有解,无解请按r\n按esc退出","white");
}
void out(){
for(int x=1;x<=3;x++){
for(int y=1;y<=3;y++){
for(int i=1;i<=5;i++){
gotoxy((x-1)*11+1,(y-1)*6+i);
for(int j=1;j<=5;j++)
if(m[x][y].a[i][j]==1&&(spacepos.x!=x||spacepos.y!=y))
ccolor(" ","","yellow");
else
printf(" ");
}
}
}
gotoxy((mazepos.x-1)*11+(blockpos.x-1)*2+1,(mazepos.y-1)*6+blockpos.y);
ccolor(" ","","green");
gotoxy((mepos.x-1)*11+(endpos.x-1)*2+1,(mepos.y-1)*6+endpos.y);
ccolor(" ","","red");
gotoxy(5,19);
ccolor(step,"white");
}
int main(){
srand(time(0));
while(1){
init();
point nmpos,newpos;
while(1){
out();
int x=scanc();
if(x==0)
return 0;
if(x>=1&&x<=4){
nmpos=mazepos;
newpos=blockpos+go[x];
if(newpos.x==0){
newpos.x=5;
nmpos.x--;
}
if(newpos.x==6){
newpos.x=1;
nmpos.x++;
}
if(newpos.y==0){
newpos.y=5;
nmpos.y--;
}
if(newpos.y==6){
newpos.y=1;
nmpos.y++;
}
if(m[nmpos.x][nmpos.y].a[newpos.y][newpos.x]==1){
mazepos=nmpos;
blockpos=newpos;
step++;
}
}
if(x>=5&&x<=8){
nmpos=spacepos-go[x-4];
if(nmpos.x>=1&&nmpos.x<=3&&nmpos.y>=1&&nmpos.y<=3){
swap(m[spacepos.x][spacepos.y],m[nmpos.x][nmpos.y]);
if(mazepos==nmpos)
mazepos=spacepos;
if(mepos==nmpos)
mepos=spacepos;
spacepos=nmpos;
step++;
}
}
if(x==9)
init();
if(mazepos==mepos&&blockpos==endpos)
break;
}
system("cls");
ccolor("You win!\n","yellow");
ccolor("Step:","white");
ccolor(step,"white");
cout<<"\n按任意键重新开始新一局游戏";
scanc();
}
return 0;
}