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
const int dx[8] = {-1,-1,-1,0,1,1,1,0};
const int dy[8] = {-1,0,1,1,1,0,-1,-1};
int a,b,c;
int p[35][155];
bool opened[35][155];
bool marked[35][155];
bool make_mark;
bool openAutoMarking;
int fieldState;
string optmsg;
bool ignoreGameKey = false;
void initfield();
void all_open();
int getCurrentMarkCount();
void refreshScreen();
void clearInputBuffer();
int getConsoleWindowTopRow();
bool setConsoleWindowTopRow(int);
int readValidInt(int,int);
void inputSetting();
void init();
bool press(int,int);
bool mark(int,int,bool);
void cleanmarks();
void draw();
void autoMark();
void checkwin();
void initfield() {
struct _timeb T;
_ftime(&T);
mt19937 wdz(T.millitm);
fieldState = 0;
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j)
p[i][j] = 0,opened[i][j] = false,marked[i][j] = false;
}
for (int i = 1; i <= c; ) {
int x = wdz() % a + 1,y = wdz() % b + 1;
if (p[x][y] == -1) continue;
p[x][y] = -1;
++i;
}
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) {
if (p[i][j] == -1) continue;
for (int k = 0; k < 8; ++k) {
int ni = i + dx[k];
int nj = j + dy[k];
if (ni < 1 || ni > a || nj < 1 || nj > b) continue;
p[i][j] += p[ni][nj] == -1;
}
}
}
}
void all_open() {
fieldState = -1;
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) {
opened[i][j] = true;
}
}
refreshScreen();
}
int getCurrentMarkCount() {
int count = 0;
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) {
if (!opened[i][j] && marked[i][j]) {
count++;
}
}
}
return count;
}
void refreshScreen() {
int currentTopRow = getConsoleWindowTopRow();
system("cls");
draw();
setConsoleWindowTopRow(currentTopRow);
}
void clearInputBuffer() {
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
FlushConsoleInputBuffer(hStdIn);
}
int getConsoleWindowTopRow() {
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hOutput, &csbi)) return 0;
return csbi.srWindow.Top;
}
bool setConsoleWindowTopRow(int topRow) {
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hOutput, &csbi)) {
return false;
}
SMALL_RECT windowRect = csbi.srWindow;
int windowHeight = windowRect.Bottom - windowRect.Top + 1;
topRow = max(0LL, min(topRow, (int)csbi.dwSize.Y - windowHeight));
windowRect.Top = topRow;
windowRect.Bottom = topRow + windowHeight - 1;
return SetConsoleWindowInfo(hOutput, TRUE, &windowRect) != 0;
}
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') {
cout << endl;
if (!input.empty()) {
val = stoi(input);
if (val >= minVal && val <= maxVal) break;
}
input.clear();
cout << "Input error, re-enter:";
} else if (c == '\b' && !input.empty()) {
input.pop_back();
cout << "\b \b";
}
}
}
return val;
}
void inputSetting() {
ignoreGameKey = true;
clearInputBuffer();
cout << "=====Choose the size of the minefield=====\n";
cout << "1.10x10 10 mines\n";
cout << "2.20x30 25 mines\n";
cout << "3.30x100 50 mines\n";
cout << "4.customize\n";
cout << "Make your choice:";
int opt = readValidInt(1,4);
if (1 <= opt && opt <= 3) {
if (opt == 1) a = 10,b = 10,c = 10;
else if (opt == 2) a = 20,b = 30,c = 25;
else if (opt == 3) a = 30,b = 100,c = 50;
} else {
cout << "Enter custom line count (10 ~ 30):";
a = readValidInt(10,30);
cout << "Enter custom column count (10 ~ 150):";
b = readValidInt(10,150);
cout << "Enter custom mines count(10 ~ " << a * b - 10 << "):";
c = readValidInt(10,a * b - 10);
}
cout << "=====Set automatic marking=====\n";
cout << "0.Off\n";
cout << "1.On\n";
openAutoMarking = readValidInt(0,1);
ignoreGameKey = false;
clearInputBuffer();
initfield();
}
void init() {
cout << "Welcome to the Minesweeper game!\n";
inputSetting();
}
bool press(int x,int y) {
if (1 <= x && x <= a && 1 <= y && y <= b && !opened[x][y]) {
marked[x][y] = false;
if (p[x][y] == -1) {
bool all_not_open = true;
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) {
if (opened[i][j]) {
all_not_open = false;
break;
}
}
if (!all_not_open) break;
}
if (all_not_open) {
for (; p[x][y] == -1; initfield());
press(x,y);
} else all_open();
return true;
}
opened[x][y] = true;
if (p[x][y] == 0) {
queue<pair<int,int> > q;
q.emplace(make_pair(x,y));
while (!q.empty()) {
int g = q.front().first;
int h = q.front().second;
q.pop();
if (p[g][h] > 0) continue;
for (int i = 0; i < 8; ++i) {
int ng = g + dx[i];
int nh = h + dy[i];
if (ng < 1 || ng > a || nh < 1 || nh > b) continue;
if (opened[ng][nh]) continue;
opened[ng][nh] = true;
q.emplace(make_pair(ng,nh));
}
}
}
return true;
}
return false;
}
bool mark(int x,int y,bool isAuto = false) {
if (1 <= x && x <= a && 1 <= y && y <= b && !opened[x][y]) {
marked[x][y] = !marked[x][y];
make_mark = marked[x][y];
if (!isAuto) {
if (make_mark) optmsg = "Mark on " + to_string(x) + " " + to_string(y);
else optmsg = "Remark on " + to_string(x) + " " + to_string(y);
}
return true;
}
return false;
}
void cleanmarks() {
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) {
if (!opened[i][j] && marked[i][j]) marked[i][j] = false;
}
}
}
void draw() {
for (int j = 0; j <= b + 1; ++j) cout << '=';
cout << endl;
for (int i = 1; i <= a; ++i) {
cout << '|';
for (int j = 1; j <= b; ++j) {
if (opened[i][j]) {
if (p[i][j] == -1) cout << '*';
else if (p[i][j] == 0) cout << ' ';
else cout << p[i][j];
} else cout << (marked[i][j]?'>':'.');
}
cout << "|\n";
}
for (int j = 0; j <= b + 1; ++j) cout << '=';
cout << endl;
if (!optmsg.empty()) cout << "Operation:" << optmsg;
cout << endl;
printf("* %lld/%lld\n",getCurrentMarkCount(),c);
if (fieldState == -1) cout << "You lose!";
else if (fieldState == 1) cout << "You win!";
cout << "\n\n";
cout << "=====How to play=====\n";
cout << "Mouse left click - Dig\n";
cout << "Ctrl + Mouse left click - Mark\n";
cout << "Space - Clean marks\n";
cout << "R - Restart\n";
cout << "O - Give up\n";
cout << "X - Change setting(& Restart!)\n";
cout << "Q - End the game\n";
}
void autoMark() {
string tempoptmsg = optmsg;
set<pair<int,int> > mineToMark;
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) {
if (!opened[i][j] || p[i][j] == 0) continue;
int unopened_unmarked = 0;
int marked_mines = 0;
vector<pair<int,int> > unopened_unmarked_cells;
for (int k = 0; k < 8; ++k) {
int ni = i + dx[k];
int nj = j + dy[k];
if (ni < 1 || ni > a || nj < 1 || nj > b) continue;
if (!opened[ni][nj]) {
if (marked[ni][nj]) ++marked_mines;
else {
++unopened_unmarked;
unopened_unmarked_cells.emplace_back(ni,nj);
}
}
}
int total_needed = p[i][j];
if (marked_mines > total_needed) continue;
if (marked_mines + unopened_unmarked == total_needed
&& unopened_unmarked > 0) {
for (auto cell:unopened_unmarked_cells) {
mineToMark.emplace(cell);
}
}
}
}
for (auto cell:mineToMark) {
int x = cell.first;
int y = cell.second;
if (!marked[x][y]) mark(x,y,true);
}
optmsg = tempoptmsg;
}
void checkwin() {
if (fieldState != 0) return ;
for (int i = 1; i <= a; ++i) {
for (int j = 1; j <= b; ++j) {
if (p[i][j] != -1 && !opened[i][j]) return ;
}
}
fieldState = 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;
initfield();
refreshScreen();
for (;;) {
if (ignoreGameKey) {
Sleep(10);
continue;
}
bool havemsg = false;
bool quit = false;
if (KEY_DOWN('R')) {
while (KEY_DOWN('R'));
initfield();
optmsg = "Restart";
if (openAutoMarking) autoMark();
refreshScreen();
} else if (KEY_DOWN('Q')) {
while (KEY_DOWN('Q'));
quit = true;
optmsg = "End the game";
} else {
if (fieldState != 0) {
Sleep(10);
continue;
}
if (KEY_DOWN(VK_LBUTTON)) {
while (KEY_DOWN(VK_LBUTTON));
bool do_mark = KEY_DOWN(VK_CONTROL);
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 (!do_mark) {
if (press(y,x)) {
optmsg = "";
if (openAutoMarking) autoMark();
checkwin();
refreshScreen();
}
} else {
if (mark(y,x,false)) {
if (openAutoMarking) autoMark();
refreshScreen();
}
}
} else if (KEY_DOWN('O')) {
while (KEY_DOWN('O'));
all_open();
optmsg = "Give up";
refreshScreen();
} else if (KEY_DOWN('X')) {
while (KEY_DOWN('X'));
system("cls");
inputSetting();
optmsg = "Change setting";
refreshScreen();
} else if (KEY_DOWN(VK_SPACE)) {
while (KEY_DOWN(VK_SPACE));
cleanmarks();
optmsg = "Clean marks";
refreshScreen();
} else continue;
}
if (quit) break;
}
}
signed main() {
int t;
// cin >> t;
t = 1;
while (t--) ++testNo,doTestcase();
return 0;
}