#include <conio.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <windows.h>
//以32为基数,
//0==空, 1==墙,2==箱子,4==箱子到达的地点
char qp[9][12][12] = {//初始地图(作者习惯叫“初始棋盘”)
{//1
{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33},
{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 34, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 36, 33},
{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33}
},
{//2
{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33},
{33, 32, 32, 33, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 33, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 33, 33, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 33, 32, 32, 32, 32, 32, 32, 32, 32, 33},
{33, 32, 32, 32, 32, 34, 33, 32, 32, 32, 32, 33},
{33, 32, 33, 32, 32, 32, 33, 32, 33, 33, 32, 33},
{33, 32, 33, 32, 33, 33, 33, 32, 32, 33, 32, 33},
{33, 32, 33, 32, 32, 32, 32, 32, 32, 33, 32, 33},
{33, 32, 33, 32, 33, 33, 32, 32, 33, 33, 32, 33},
{33, 32, 33, 32, 33, 32, 32, 32, 32, 36, 32, 33},
{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33}
},
};
char nowqp[12][12];//目前地图(作者习惯叫“目前棋盘”)
int nowxy[9][3] = {
//{横轴,纵轴,箱子数量(暂时没用)}
{1, 1, 1},//1
{1, 10, 1},//2
};
int now = 0;//目前关卡
int nowx = nowxy[now][1], nowy = nowxy[now][2];//目前横轴、目前纵轴
void start(){//开始(载入地图)
for (int i = 0; i < 12; i++){
for (int j = 0; j < 12; j++){
nowqp[i][j] = qp[now][i][j];
}
}
}
inline void restart(){//重新开始(载入地图)
start();
}
void init1(){//随机地图
srand(time(0));
int i = 0, j = 0;
for (; i < 12; i++){
for (; j < 12; j++){
if (i == 0 || j == 0 || i == 11 || j == 11){
nowqp[i][j] = 33;
}
else{
nowqp[i][j] = rand() % 2 + 32;
}
}
}
do{
i = rand() % 10 + 1; j = rand() % 10 + 1;
}while(nowqp[i][j] == 33);
nowqp[i][j] = 34;
do{
i = rand() % 10 + 1; j = rand() % 10 + 1;
}while(nowqp[i][j] == 33 || nowqp[i][j] == 34);
nowqp[i][j] = 36;
do{
i = rand() % 10 + 1; j = rand() % 10 + 1;
}while(nowqp[i][j] == 33 || nowqp[i][j] == 34 || nowqp[i][j] == 36);
nowx = i;
nowy = j;
}
void printqp(){//打印棋盘
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, 15/*0xf=0x1+0x2+0x4+0x8*/);
system("cls");
if (now < 9){
printf("第 %d关\n", now + 1);
}
else{
puts("随机关");
}
for (int i = 0; i < 12; i++){
for (int j = 0; j < 12; j++){
if (nowx == i && nowy == j){
SetConsoleTextAttribute(handle, 15/*0xf=0x1+0x2+0x4+0x8*/);
printf("♀");//你
}
else if (nowqp[i][j] == 32){
printf(" ");//空格
}
else if (nowqp[i][j] == 33){
SetConsoleTextAttribute(handle, 15/*0xf=0x1+0x2+0x4+0x8*/);
printf("▇");//墙
}
else if (nowqp[i][j] == 34){
SetConsoleTextAttribute(handle, 10/*0xa=0x2+0x8*/);
printf("●");//箱子
}
else if (nowqp[i][j] == 36){
SetConsoleTextAttribute(handle, 6/*0x6=0x2+0x4*/);
printf("☆");//没有箱子的目的地
}
else if (nowqp[i][j] == 38){
SetConsoleTextAttribute(handle, 14/*0xe=0x2+0x4+0x8*/);
printf("★");//有箱子的目的地
}
}
puts("");
}
}
void scanqp(){//输入
char inputchar;
inscan: inputchar = getch();//读取用户输入字符
if (inputchar == 'd'){//右
if (nowqp[nowx][nowy + 1] != 33){
if (nowqp[nowx][nowy + 1] == 34 ||
nowqp[nowx][nowy + 1] == 38){
if (nowqp[nowx][nowy + 2] != 33 &&
nowqp[nowx][nowy + 2] != 34 &&
nowqp[nowx][nowy + 2] != 38){
nowqp[nowx][nowy + 2] = nowqp[nowx][nowy + 2] + 2;
nowqp[nowx][nowy + 1] = nowqp[nowx][nowy + 1] - 2;
nowy++;
}
}
else{
nowy++;
}
}
}
else if (inputchar == 's'){//下
if (nowqp[nowx + 1][nowy] != 33){
if (nowqp[nowx + 1][nowy] == 34 ||
nowqp[nowx + 1][nowy] == 38){
if (nowqp[nowx + 2][nowy] != 33 &&
nowqp[nowx + 2][nowy] != 34 &&
nowqp[nowx + 2][nowy] != 38){
nowqp[nowx + 2][nowy] = nowqp[nowx + 2][nowy] + 2;
nowqp[nowx + 1][nowy] = nowqp[nowx + 1][nowy] - 2;
nowx++;
}
}
else{
nowx++;
}
}
}
else if (inputchar == 'a'){//左
if (nowqp[nowx][nowy - 1] != 33){
if (nowqp[nowx][nowy - 1] == 34 ||
nowqp[nowx][nowy - 1] == 38){
if (nowqp[nowx][nowy - 2] != 33 &&
nowqp[nowx][nowy - 2] != 34 &&
nowqp[nowx][nowy - 2] != 38){
nowqp[nowx][nowy - 2] = nowqp[nowx][nowy - 2] + 2;
nowqp[nowx][nowy - 1] = nowqp[nowx][nowy - 1] - 2;
nowy--;
}
}
else{
nowy--;
}
}
}
else if (inputchar == 'w'){//上
if (nowqp[nowx - 1][nowy] != 33){
if (nowqp[nowx - 1][nowy] == 34 ||
nowqp[nowx - 1][nowy] == 38){
if (nowqp[nowx - 2][nowy] != 33 &&
nowqp[nowx - 2][nowy] != 34 &&
nowqp[nowx - 2][nowy] != 38){
nowqp[nowx - 2][nowy] = nowqp[nowx - 2][nowy] + 2;
nowqp[nowx - 1][nowy] = nowqp[nowx - 1][nowy] - 2;
nowx--;
}
}
else{
nowx--;
}
}
}
else if (inputchar > 48 && inputchar < 51/*58*/){//'1'~'9',代表关卡
if (now + 49 == inputchar){
nowx = nowxy[now][1];
nowy = nowxy[now][2];
restart();
}
else if (now + 49 != inputchar){
now = inputchar - 49;
nowx = nowxy[now][1];
nowy = nowxy[now][2];
start();
}
}
else if (inputchar == 48){//'0',代表随机关卡(可能无解)
init1();
}
else if (inputchar == 'q'){//退出
exit(0);
}
else{
goto inscan;
}
}
int main(){
start();
while (1){
printqp();
scanqp();
}
return 0;
}
推箱子2.1.1非文件版
最新推荐文章于 2024-07-12 10:45:15 发布