C++#include <bits/stdc++.h>
#include <windows.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define int long long
#define endl '\n'
using namespace std;
int testNo = 0;
// variables & definitions
int a,b,row = 1,col = 1;
bool p[55][55];
int stepcnt = 0;
string msg;
bool creative = false;
bool ignoreGameKey = false;
namespace theOperatorsThatChangeTheLight {
void shufflelight() {
struct _timeb T;
_ftime(&T);
mt19937 wdz(T.millitm);
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) {
p[i][j] = wdz() % 2;
}
}
}
void turnoff() {
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) p[i][j] = false;
}
}
void turnon() {
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) p[i][j] = true;
}
}
void switchcreativemode() {
creative = !creative;
}
void stepto0() {
stepcnt = 0;
}
}
using namespace theOperatorsThatChangeTheLight;
void clearInputBuffer() {
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
FlushConsoleInputBuffer(hStdIn);
}
int getScrollLines() {
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hOutput,&csbi);
int scrollLines = csbi.srWindow.Top;
return scrollLines;
}
void setScrollLines(int scrollLines) {
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hOutput, &csbi);
SMALL_RECT newWindow = csbi.srWindow;
newWindow.Top = scrollLines;
newWindow.Bottom = newWindow.Top + (csbi.srWindow.Bottom - csbi.srWindow.Top);
if (newWindow.Bottom >= csbi.dwSize.Y) {
newWindow.Bottom = csbi.dwSize.Y - 1;
newWindow.Top = newWindow.Bottom - (csbi.srWindow.Bottom - csbi.srWindow.Top);
}
if (newWindow.Top < 0) newWindow.Top = 0;
SetConsoleWindowInfo(hOutput, TRUE, &newWindow);
}
int readValidInt(int minVal, int maxVal) {
int val = -1;
string input;
while (true) {
INPUT_RECORD ir;
DWORD count;
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
ReadConsoleInput(hStdIn, &ir, 1, &count);
if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown) {
char c = ir.Event.KeyEvent.uChar.AsciiChar;
if (c >= '0' && c <= '9') {
input += c;
cout << c;
} else if (c == '\r') {
if (!input.empty()) {
val = stoi(input);
if (val >= minVal && val <= maxVal) break;
}
input.clear();
cout << "\n输入错误,重新输入:";
} else if (c == '\b' && !input.empty()) {
input.pop_back();
cout << "\b \b";
}
}
}
return val;
}
void inputSize() {
ignoreGameKey = true;
clearInputBuffer();
cout << endl;
cout << "1.3x3\n";
cout << "2.5x5\n";
cout << "3.7x7\n";
cout << "4.10x10\n";
cout << "5.20x20(实验性)\n";
cout << "6.50x50(实验性)\n";
cout << "7.自定义\n";
cout << "输入选择:";
int opt = readValidInt(1,7);
if (1 <= opt && opt <= 6) {
a = b = (vector<int> {-1,3,5,7,10,20,50} [opt]);
ignoreGameKey = false;
clearInputBuffer();
turnoff();
return ;
}
cout << "输入自定义尺寸(行,不能超过 50):";
a = readValidInt(1,50);
cout << "输入自定义尺寸(列,不能超过 50):";
b = readValidInt(1,50);
ignoreGameKey = false;
clearInputBuffer();
turnoff();
}
void init() {
cout << "欢迎游玩熄灯游戏沙盒!\n";
inputSize();
}
bool press(int x,int y) {
swap(x,y);
x /= row;
y /= col;
++x,++y;
if (1 <= x && x <= a && 1 <= y && y <= b) {
p[x][y] = !p[x][y];
if (creative) return true;
if (x != 1) p[x - 1][y] = !p[x - 1][y];
if (x != a) p[x + 1][y] = !p[x + 1][y];
if (y != 1) p[x][y - 1] = !p[x][y - 1];
if (y != b) p[x][y + 1] = !p[x][y + 1];
++stepcnt;
return true;
}
return false;
}
void draw() {
for (int i = 1; i <= a; ++i) {
for (int k = 1; k <= row; ++k) {
for (int j = 1; j <= b; ++j) for (int l = 1; l <= col; ++l) cout << (p[i][j]?'O':'.');
cout << endl;
}
}
cout << "步数:" << stepcnt << endl;
if (!msg.empty()) cout << "操作:" << msg << "\n";
cout << "\n\n\n";
cout << "=====玩法=====\n";
cout << "鼠标点击——切换灯的状态\n";
cout << "空格——打乱\n";
cout << "0——全关\n";
cout << "1——全开\n";
cout << "A——开启/关闭创造模式\n";
cout << "S——计步器归零\n";
cout << "X——切换尺寸\n";
cout << "M——切换灯的显示模式\n";
}
void changeLightMode() {
++col;
if (col > 3) {
col = 1;
++row;
if (row > 3) row = 1;
}
}
void doTestcase() {
init();
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin,&mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hStdin,mode);
POINT p;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HWND h = GetForegroundWindow();
CONSOLE_FONT_INFO consoleCurrentFont;
system("cls");
draw();
for (;;) {
if (ignoreGameKey) {
Sleep(10);
continue;
}
bool havemsg = false;
if (KEY_DOWN(VK_LBUTTON)) {
while (KEY_DOWN(VK_LBUTTON));
GetCursorPos(&p);
ScreenToClient(h,&p);
GetCurrentConsoleFont(hOutput,FALSE,&consoleCurrentFont);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hOutput,&csbi);
int scrollLines = csbi.srWindow.Top;
int x = p.x /= consoleCurrentFont.dwFontSize.X;
int y = p.y /= consoleCurrentFont.dwFontSize.Y;
y += scrollLines;
if (!press(x,y)) {
if (msg != "") msg = "",system("cls"),draw();
continue;
}
msg = "";
} else if (KEY_DOWN(VK_SPACE)) {
while (KEY_DOWN(VK_SPACE));
shufflelight();
msg = "打乱";
} else if (KEY_DOWN('0')) {
while (KEY_DOWN('0'));
turnoff();
msg = "全关";
} else if (KEY_DOWN('1')) {
while (KEY_DOWN('1'));
turnon();
msg = "全开";
} else if (KEY_DOWN('A')) {
while (KEY_DOWN('A'));
switchcreativemode();
msg = "创造模式";
if (creative) msg = "开启" + msg;
else msg = "关闭" + msg;
} else if (KEY_DOWN('S')) {
while (KEY_DOWN('S'));
stepto0();
msg = "计步器归零";
} else if (KEY_DOWN('X')) {
while (KEY_DOWN('X'));
inputSize();
msg = "切换尺寸";
} else if (KEY_DOWN('M')) {
while (KEY_DOWN('M'));
changeLightMode();
msg = "切换灯的显示模式";
msg += "(当前模式:" + to_string(row * 3 - 3 + col) + ")";
} else continue;
int savedScroll = getScrollLines();
system("cls");
setScrollLines(savedScroll);
draw();
}
}
signed main() {
int t;
// cin >> t;
t = 1;
while (t--) ++testNo,doTestcase();
return 0;
}