跳到正文

算法·理论

sokoban自制版

bananamilktea约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月15日
篇幅
约 8 分钟

版本

  1. v1当前

最近发现一道有意思的模拟题,于是就仿照题目内容做了一个同款游戏,方便理解题意,也可以休闲娱乐。

C++
#include<bits/stdc++.h>
#include<windows.h>
#define	CMD_UP 1
#define	CMD_DOWN 2
#define	CMD_LEFT 4
#define	CMD_RIGHT 8
#define CMD_I 16
#define CMD_J 32
#define CMD_K 64
#define CMD_L 128
#define	CMD_W 1
#define	CMD_A 2
#define	CMD_S 4
#define	CMD_D 8
#define	CMD_NP1 32
#define	CMD_NP2 64
#define	CMD_NP3 128
#define	CMD_NP5 16
#define	CMD_XL 256
#define CMD_SHOT 512
#define CMD_HEAL 1024
#define CMD_DIE 2048

using namespace std;

const int MAXN = 1e3 + 1;

char c[MAXN][MAXN];
int now, t, n, m, a[MAXN], wx, wy;
string s;
void Color(int a)
{
    if(a==0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
    if(a==1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE);
    if(a==2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);
    if(a==3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_BLUE);
    if(a==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);
    if(a==5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
    if(a==6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN);
}
void HideCursor() {
	CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // ??????,???1,????
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); // ?????????
}
void setPos(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
map<char, int> dx, dy;

int main() {
	dx['U'] = -1, dy['U'] = 0;
	dx['D'] = 1, dy['D'] = 0;
	dx['L'] = 0, dy['L'] = -1;
	dx['R'] = 0, dy['R'] = 1;
	cout << "输入地图行与列(按照题目格式输入):\n";
	cin >> n >> m;
	cout << "输入地图:\n";
	for(int i = 2; i <= n + 1; i++) {
		for(int j = 2; j <= m + 1; j++) {
			cin >> c[i][j];
			if(c[i][j] != '.' && c[i][j] != '#' && c[i][j] != 'B' && c[i][j] != 'b' && c[i][j] != 'w' && c[i][j] != 'W' && c[i][j] != '+') {
				system("cls");
				cout << "错误:输入了不该存在于地图内的字符。";
				return 0;
			}
			if(c[i][j] == 'w' || c[i][j] == 'W') {
				wx = i, wy = j;
				c[i][j] = (c[i][j] == 'w' ? '.' : '+');
			}
		}
	}
	HideCursor();
	n = n + 2, m = m + 2;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(i == 1 || j == 1 || i == n || j == m) {
				c[i][j] = '#';
			}
		}
	}
	system("cls");
	while(1) {
		setPos(0, 0);
		int gx = 0;
		if(GetAsyncKeyState(VK_UP) & 0x8000) {
			gx |= CMD_UP;
		}
		if(GetAsyncKeyState(VK_DOWN) & 0x8000) {
			gx |= CMD_DOWN;
		}
		if(GetAsyncKeyState(VK_LEFT) & 0x8000) {
			gx |= CMD_LEFT;
		}
		if(GetAsyncKeyState(VK_RIGHT) & 0x8000) {
			gx |= CMD_RIGHT;
		}
		char fuck = 'S';
		if(gx & CMD_UP) {
			fuck = 'U';
		}
		else if(gx & CMD_DOWN) {
			fuck = 'D';
		}
		else if(gx & CMD_LEFT) {
			fuck = 'L';
		}
		else if(gx & CMD_RIGHT) {
			fuck = 'R';
		}
		int newx = wx + dx[fuck], newy = wy + dy[fuck], newx2 = wx + 2 * dx[fuck], newy2 = wy + 2 * dy[fuck];
		if(c[newx][newy] == '#' || (c[newx][newy] == 'b' || c[newx][newy] == 'B') && (c[newx2][newy2] == 'b' || c[newx2][newy2] == 'B' || c[newx2][newy2] == '#')) {
			continue;
		}
		if(c[newx][newy] == 'b') {
			c[newx][newy] = '.';
			c[newx2][newy2] = (c[newx2][newy2] == '+' ? 'B' : 'b');
		}
		if(c[newx][newy] == 'B') {
			c[newx][newy] = '+';
			c[newx2][newy2] = (c[newx2][newy2] == '+' ? 'B' : 'b');
		}
		wx = newx, wy = newy;
		bool is_finish = true;
		for(int i = 2; i < n; i++) {
			for(int j = 2; j < m; j++) {
				if(i == wx && j == wy) {
					Color(1);
					cout << (c[i][j] == '+' ? 'W' : 'w');
					Color(0);
				}
				else if(c[i][j] == 'b' || c[i][j] == 'B') {
					Color(6);
					cout << c[i][j];
					Color(0);
				}
				else if(c[i][j] == '+') {
					Color(5);
					cout << c[i][j];
					Color(0);
				}
				else {
					cout << c[i][j];
				}
				is_finish &= (c[i][j] != '+');
			}
			cout << "\n";
		}
		if(is_finish) {
			break;
		}
		Sleep(50);
	}
	cout << "\nyou win!\n";
	return 0;
}
原文