推箱子,非常辣鸡,且不再会更新,只能在Windows上运行(但第一行似乎显而易见 …)。
欢迎 Ctrl+C & Ctrl+V
//Sokoban.cpp
#include<windows.h>
#include<iostream>
#include<cstring>
#include<fstream>
#include<conio.h>
#include<string>
#include<cstdio>
#include<ctime>
#define random(n) (rand()*rand()%n)
#define long long ll
using namespace std;
int Map[30][30],SideXLength,SideYLength,PlayerX,PlayerY;
void Gotoxy(short x=0,short y=0){COORD coord={x,y};SetConsoleCursorPosition(HANDLE(GetStdHandle(STD_OUTPUT_HANDLE)),coord);}
void HideCursor(bool hide){CONSOLE_CURSOR_INFO cursor_info={1,hide?0:1};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);}
void Read()
{
int i,j,sum(0),ans(0),cnt(0);
PlayerX=-1;
PlayerY=-1;
char t;
string line;
ifstream infile("map.txt",ios::in);
infile>>SideXLength>>SideYLength;
if(SideXLength<=0||SideYLength<=0||SideXLength>=40||SideYLength>=40)
{
printf("The file is not compliant\n");
printf("[ERROR]:The map size is not reasonable(0 < mapsize < 40)\n");
while(getch()!=9);
exit(0);
}
for(i=0;i<=SideXLength;++i)
{
getline(infile,line);
if(i==0) continue;
for(j=0;j<SideYLength;++j)
{
switch(line[j])
{
case ' ':Map[i][j+1]=0;break;
case 'E':Map[i][j+1]=1;sum++;break;
case 'B':Map[i][j+1]=2;ans++;break;
case 'W':Map[i][j+1]=3;break;
case 'S':Map[i][j+1]=4;break;
case 'P':Map[i][j+1]=0;PlayerX=i;PlayerY=j+1;cnt++;break;
default:
printf("The file is not compliant\n");
printf("[ERROR]:Found unknown char < %c > at %d %d",line[j],i,j+1);
while(getch()!=9);
exit(0);
}
}
}
if(sum!=ans)
{
printf("The file is not compliant\n");
printf("[ERROR]:The number of boxes does not match the number of endpoints,There are %d box(es) but there are %d endpoint(s)\n",ans,sum);
while(getch()!=9);
exit(0);
}
if(PlayerX==-1||PlayerY==-1)
{
printf("The file is not compliant\n");
printf("[ERROR]:Found no player in the map\n");
while(getch()!=9);
exit(0);
}
if(cnt>1)
{
printf("The file is not compliant\n");
printf("[ERROR]:Found %d players in the map\n",cnt);
while(getch()!=9);
exit(0);
}
return;
}
void Display(bool first)
{
int i,j,k;
if(!first)
{
Gotoxy(2,2);
for(i=1;i<=SideXLength;++i)
{
Gotoxy(2,i);
for(j=1;j<=SideYLength;++j)
{
if(i==PlayerX&&j==PlayerY) printf("♀");
else switch(Map[i][j])
{
case 0:printf(" ");break;
case 1:printf("×");break;
case 2:printf("∷");break;
case 3:printf("■");break;
case 4:printf("※");break;
default:printf(" ");
}
}
}
return;
}
Gotoxy(0,0);
printf("┏");
for(i=1;i<=SideYLength;++i) printf("━");
printf("┓\n");
for(i=1;i<=SideXLength;++i)
{
printf("┃");
for(j=1;j<=SideYLength;++j)
{
if(i==PlayerX&&j==PlayerY) printf("♀");
else switch(Map[i][j])
{
case 0:printf(" ");break;
case 1:printf("×");break;
case 2:printf("∷");break;
case 3:printf("■");break;
case 4:printf("※");break;
default:printf(" ");
}
}
printf("┃\n");
}
printf("┗");
for(i=1;i<=SideYLength;++i) printf("━");
printf("┛\n");
printf("┏━━━━┳━━━━┳━━━━┳━━━━┳━━━━━━┓\n");
printf("┃W:↑ ┃A:← ┃S:↓ ┃D:→ ┃R:Reset ┃\n");
printf("┣━━━━┻━┳━━┻┳━━━┻━━━━┻━┳━━━━┫\n");
printf("┃×:Endpoint ┃∷:Box┃※:Endpoint with Box┃■:Wall ┃\n");
printf("┗━━━━━━┻━━━┻━━━━━━━━━━┻━━━━┛\n");
}
bool Move(short facing)
{
int first,second;
switch(facing)
{
case 1:first=Map[PlayerX-1][PlayerY];second=Map[PlayerX-2][PlayerY];break;
case 2:first=Map[PlayerX][PlayerY-1];second=Map[PlayerX][PlayerY-2];break;
case 3:first=Map[PlayerX+1][PlayerY];second=Map[PlayerX+2][PlayerY];break;
case 4:first=Map[PlayerX][PlayerY+1];second=Map[PlayerX][PlayerY+2];break;
}
switch(facing)
{
case 1:
if(first==0||first==1) PlayerX--;
else if(first==2&&second==0){PlayerX--;swap(Map[PlayerX][PlayerY],Map[PlayerX-1][PlayerY]);}
else if(first==2&&second==1){PlayerX--;Map[PlayerX][PlayerY]=0;Map[PlayerX-1][PlayerY]=4;}
else if(first==4&&second==0){PlayerX--;Map[PlayerX][PlayerY]=1;Map[PlayerX-1][PlayerY]=2;}
else if(first==4&&second==1){PlayerX--;swap(Map[PlayerX][PlayerY],Map[PlayerX-1][PlayerY]);}
else return true;
break;
case 2:
if(first==0||first==1) PlayerY--;
else if(first==2&&second==0){PlayerY--;swap(Map[PlayerX][PlayerY],Map[PlayerX][PlayerY-1]);}
else if(first==2&&second==1){PlayerY--;Map[PlayerX][PlayerY]=0;Map[PlayerX][PlayerY-1]=4;}
else if(first==4&&second==0){PlayerY--;Map[PlayerX][PlayerY]=1;Map[PlayerX][PlayerY-1]=2;}
else if(first==4&&second==1){PlayerY--;swap(Map[PlayerX][PlayerY],Map[PlayerX][PlayerY-1]);}
else return true;
break;
case 3:
if(first==0||first==1) PlayerX++;
else if(first==2&&second==0){PlayerX++;swap(Map[PlayerX][PlayerY],Map[PlayerX+1][PlayerY]);}
else if(first==2&&second==1){PlayerX++;Map[PlayerX][PlayerY]=0;Map[PlayerX+1][PlayerY]=4;}
else if(first==4&&second==0){PlayerX++;Map[PlayerX][PlayerY]=1;Map[PlayerX+1][PlayerY]=2;}
else if(first==4&&second==1){PlayerX++;swap(Map[PlayerX][PlayerY],Map[PlayerX+1][PlayerY]);}
else return true;
break;
case 4:
if(first==0||first==1) PlayerY++;
else if(first==2&&second==0){PlayerY++;swap(Map[PlayerX][PlayerY],Map[PlayerX][PlayerY+1]);}
else if(first==2&&second==1){PlayerY++;Map[PlayerX][PlayerY]=0;Map[PlayerX][PlayerY+1]=4;}
else if(first==4&&second==0){PlayerY++;Map[PlayerX][PlayerY]=1;Map[PlayerX][PlayerY+1]=2;}
else if(first==4&&second==1){PlayerY++;swap(Map[PlayerX][PlayerY],Map[PlayerX][PlayerY+1]);}
else return true;
break;
}
}
bool Judge()
{
int i,j,BoxSum(0);
for(i=1;i<=SideXLength;++i) for(j=1;j<=SideYLength;++j) if(Map[i][j]==1) BoxSum++;
return !BoxSum;
}
void Win()
{
Display(false);
Gotoxy(0,SideXLength+7);
printf("You are win!Please press <space> to play again\n");
while(getch()!=' ');
Read();
system("cls");
Display(true);
}
int main()
{
bool flag(false);
char t;
HideCursor(true);
SetConsoleTitle("The Sokoban - By DecemberFox");
Read();
Display(true);
while(true)
{
if(!flag) Display(false);
flag=false;
if(kbhit())
{
t=getch();
if(t>='A'&&t<='Z') t+=32;
switch(t)
{
case 'w':flag=Move(1);break;
case 'a':flag=Move(2);break;
case 's':flag=Move(3);break;
case 'd':flag=Move(4);break;
case 'r':Read();Display(false);break;
}
}
if(Judge()) Win();
}
return 0;
}另外,游玩还需要一个配置文件,即地图文件,可以按照下面的步骤构建(有点麻烦勿喷):
-
新建文本文档并命名为 map.txt(txt 为文件格式)。
-
打开文本文档,按照如下格式输入地图。
-
第一行两个数字,分别为地图的长 n 和宽 m ,用空格隔开,均不能超过 40(超过 40 游玩会返回错误信息)。
-
第二行之后开始输入地图,输入特定的字符即可代表不同的方块,其中大小为 n*m ,超过此大小会导致没有事情发生。
-
其中 W(Wall,墙),P(Player,玩家,且数量必须为一个),B(Box,箱子,且要与终点的数量相同),E(Endpoint,终点,且要与箱子的数量相同),S(Endpoint with Box,终点和箱子,即终点和箱子重叠的情况)以及空格(Air,空气),并且如果没有用墙做一个边框会导致玩家可以跑出去…
(这叫特性)。 -
未按照上述过程可能会导致错误的情况发生,其中 3,5 的错误可以通过运行程序后返回的错误提示信息得知。
-
下面为一个例子,操作说明在程序中已经有了。
9 9
WWWWWWWWW
W W W
W P W
W B W
W E W
W S W
W W
W W
WWWWWWWWW半全机翻勿喷,英语差勿喷,辣鸡大作勿喷
随便乱写的程序,不过还是能够勉强凑合玩玩的。