跳到正文

休闲·娱乐

只有聪明人才能玩到的游戏!

fumanke约 15 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

本蒟蒻整了个游戏,但只有聪明人才能玩到! 不要说是 AI 写的!是窝一个字一个字敲出来的!!! 可能会有些延迟

代码

C++
#include <fstream>
#include <iostream>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <limits.h>
#include <windows.h>
#include <map>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int best, point; // 记录 & 分数
void cd(int best) { // 存档
	FILE* fp = fopen("example.txt", "wb");
	fprintf(fp, "%d", best);
	fclose(fp);
}
void dd() { // 读档
    FILE* fp = fopen("example.txt", "rb");
    fscanf(fp, "%d", &best);
	fclose(fp);
}
char kb() { // 读键位
	if(_kbhit()) { char key = _getch(); return key; }
	else return '_';
}

void gotoxy(int x, int y) {
    COORD pos = {x, y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}
void clear() { // 无闪清屏
	gotoxy(0, 0);
	Sleep(20);
}

int chessboard[6][6]; // 棋盘
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0}; // 移动方向
map<int, string> mapping = {{0, "   "}, {2, "C E"}, {4, "..."}, {8, "R E"}, {16, "TLE"}, {32, "MLE"}, {64, "ILE"}, {128, "OLE"}, {256, "UKE"}, {512, "W A"}, {1024, "P C"}, {2048, "A C"}, {4096, "P E"}, {8192, "DoJ"}, {16384, "J F"}, {32768, "A U"}, {65536, "A K"}}; // 数值 -> 评测状态
map<char, int> moveMap = {{'W', 0}, {'A', 1}, {'D', 2}, {'S', 3}, {' ', 114514}}; // 键位 -> dx & dy 下标
void generate(int _x, int _y) { // 按概率生成方块
	int chance = rand() % 10000 + 1;
	if(chance <= 8800) chessboard[_x][_y] = 2;
	else if(chance <= 10000) chessboard[_x][_y] = 4;
}

pair<int, int> choose() { // 随机选择生成方块的位置
	vector<pair<int, int> > pos;
	for(int i = 1;i <= 4;i++)
		for(int j = 1;j <= 4;j++)
			if(chessboard[i][j] == 0)
				pos.push_back({i, j});
	int chosenPos = rand() % pos.size();
	return pos[chosenPos];
}

void initialization() { // 初始化棋盘
	memset(chessboard, 0, sizeof(chessboard));
	for(int i = 1;i <= 4;i++)
		chessboard[0][i] = chessboard[i][0] = chessboard[5][i] = chessboard[i][5] = 114514;
	int initialX1, initialX2, initialY1, initialY2;
	initialX1 = rand() % 4 + 1;
	initialX2 = rand() % 4 + 1;
	initialY1 = rand() % 4 + 1;
	initialY2 = rand() % 4 + 1;
	if(initialX1 == initialX2 && initialY1 == initialY2) { // 判两点相同
		initialX2 = (initialX2 == 4 ? 1 : initialX2 + 1);
		initialY2 = (initialY2 == 1 ? 4 : initialY2 - 1);
	}
	generate(initialX1, initialY1);
	generate(initialX2, initialY2);
}

void draw() { // 显示:棋盘
	cout << "AK IOI   版本号:1.4" << endl;
	cout << "合并评测状态并得到 AK!" << endl;
	cout << "WSAD  推动方块并合成" << endl;
	cout << "Space  回炉重造" << endl;
	cout << "记得切换为 英文 输入法!" << endl << endl;
	cout << "分数:" << point << "  记录:" << best << endl;
	cout << "+-----------------------+" << endl;
	for(int i = 1;i <= 4;i++) {
		cout << "|";
		for(int j = 1;j <= 4;j++) {
			string state = mapping[chessboard[i][j]];
			int stateLen = state.size();
			cout << " " << state << " |";
		}
		if(i <= 3)
			cout << endl << "|-----------------------|";
		cout << endl;
	}
	cout << "+-----------------------+" << endl << endl;
}

void print() { // 显示: 数值 -> 评测状态
	cout << "+-------------------------------------------------------+" << endl;
	cout << "|  _CE 00002  |  ... 00004  |  _RE 00008  |  TLE 00016  |" << endl;
	cout << "|-------------------------------------------------------|" << endl;
	cout << "|  MLE 00032  |  ILE 00064  |  OLE 00128  |  UKE 00256  |" << endl;
	cout << "|-------------------------------------------------------|" << endl;
	cout << "|  _WA 00512  |  _PC 01024  |  _AC 02048  |  _PE 04096  |" << endl;
	cout << "|-------------------------------------------------------|" << endl;
	cout << "|  DoJ 08192  |  _JF 16384  |  _AU 32768  |  _AK 65536  |" << endl;
	cout << "+-------------------------------------------------------+" << endl;
}

bool canPushDir(int direction) { // 判能否朝 direction 动
	int _dx = dx[direction], _dy = dy[direction];
	for(int i = 1;i <= 4;i++)
		for(int j = 1;j <= 4;j++)
			if((!chessboard[i + _dx][j + _dy] && chessboard[i][j]) || chessboard[i][j] == chessboard[i + _dx][j + _dy])
				return 1;
	return 0;
}

bool canPush() { // 判 GameOver
	for(int i = 0;i < 4;i++)
		if(canPushDir(i))
			return 1;
	return 0;
}

int getDirection() { // 获取移动方向
	char c;
	while(true) {
		c = kb();
		if('a' <= c && c <= 'z') c -= ' ';
		if(c == 'W' || c == 'S' || c == 'A' || c == 'D')
			if(canPushDir(moveMap[c])) break;
		if(c == ' ') break;
	}
	return moveMap[c];
}

void push(int direction) { // 推动方块
	for(int i = 1;i <= 4;i++)
		for(int j = 1;j <= 4;j++) {
			int _x = i + dx[direction];
			int _y = j + dy[direction];
			if(chessboard[_x][_y] == 0) {
				chessboard[_x][_y] = chessboard[i][j];
				chessboard[i][j] = 0;
			}
		}
}

void mergeW() { // 向上
	for(int j = 1;j <= 4;j++)
		for(int i = 1;i < 4;i++)
			if(chessboard[i][j] == chessboard[i + 1][j]) {
				chessboard[i][j] *= 2, chessboard[i + 1][j] = 0;
				point += chessboard[i][j], i++;
			}
}

void mergeA() { // 向左
	for(int i = 1;i <= 4;i++)
		for(int j = 1;j < 4;j++)
			if(chessboard[i][j] == chessboard[i][j + 1]) {
				chessboard[i][j] *= 2, chessboard[i][j + 1] = 0;
				point += chessboard[i][j], j++;
			}
}

void mergeD() { // 向右
	for(int i = 1;i <= 4;i++)
		for(int j = 4;j > 1;j--)
			if(chessboard[i][j] == chessboard[i][j - 1]) {
				chessboard[i][j] *= 2, chessboard[i][j - 1] = 0;
				point += chessboard[i][j], j--;
			}
}

void mergeS() { // 向前看(划掉)向下
	for(int j = 1;j <= 4;j++)
		for(int i = 4;i > 1;i--)
			if(chessboard[i][j] == chessboard[i - 1][j]) {
				chessboard[i][j] *= 2, chessboard[i - 1][j] = 0;
				point += chessboard[i][j], i--;
			}
}

void merge(int direction) { // 合并方块
	if(direction == 0) mergeW();
	else if(direction == 1) mergeA();
	else if(direction == 2) mergeD();
	else if(direction == 3) mergeS();
}

int play() { // 游戏过程
	initialization();
	draw(), print();
	while(canPush()) {
		int dir = getDirection();
		if(dir == 114514) return 114514;
		for(int i = 1;i <= 10;i++) push(dir);
		merge(dir);
		for(int i = 1;i <= 10;i++) push(dir);
		best = max(point, best);
		clear(), cd(best);
		draw(), print();
		for(int i = 1;i <= 4;i++)
			for(int j = 1;j <= 4;j++)
				if(chessboard[i][j] == 65536)
					return 1;
		pair<int, int> pos = choose();
		generate(pos.first, pos.second);
		clear();
		draw(), print();
	}
	return 0;
}

int main() {
	system("color E3");
	system("title AK IOI!");
	srand(time(NULL)), dd();
	while(true) { // 重复游戏
		system("cls");
		point = 0;
		cout << "AK IOI   版本号:1.4" << endl;
		cout << "合并评测状态并得到 AK!" << endl;
		cout << "记得切换为 英文 输入法!" << endl;
		cout << "F  开始游戏" << endl;
		cout << "B  退出游戏" << endl;
		bool startOrBack;
		while(true) {
			char c = kb();
			if(c == 'F' || c == 'f') { startOrBack = 1; break; }
			else if(c == 'B' || c == 'b') { startOrBack = 0; break; }
		}
		if(startOrBack) {
			MessageBox(NULL, "即将开始游戏!", "开始游戏", MB_OK);
			Again: point = 0;
			system("cls");
			int isAKIOI = play();
			if(isAKIOI == 1) cout << "You AK IOI!!!" << endl;
			else if(isAKIOI == 0) cout << "Game Over." << endl;
			else if(isAKIOI == 114514) goto Again;
			Sleep(2500); //间隔时间两 s 半
			continue;
		}
		else {
			if(MessageBox(NULL, "是否退出游戏?", "退出游戏", MB_YESNO) == IDYES) break;
			else continue;
		}
	}
	cout << "游戏结束,Bye bye~" << endl;
	Sleep(500);
	cout << "作者:fumanke" << endl;
	return 0;
}

提示

请用 C++14 运行。

“诶,怎么报错了?” “报错信息好像是文件错误,翻翻代码吧。” “代码好长啊……” “文件操作……我知道了!先这样做……” 之后的话,你听不清了。

原文