#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int>> puzzle_type;
typedef vector<vector<int>> mask_type;
int randint(int x, int y)
{
static auto seed = random_device()();
static mt19937 rng(seed);
uniform_int_distribution<> uni(x, y);
return uni(rng);
}
int CountMines(puzzle_type pz, int x, int y)
{
int res = 0;
for (int i = max(y - 1, 0); i <= min(y + 1, (int)pz.size() - 1); i++)
{
for (int j = max(x - 1, 0); j <= min(x + 1, (int)pz.size() - 1); j++)
{
if (pz[i][j] == -1)
++res;
}
}
return res;
}
/// @brief Print the Puzzle!
/// @param puzz,massk
/// @return Nothing really!
void printPz(puzzle_type puzz, mask_type massk, bool IDie = false, bool IWin = false)
{
cout << " ";
for (int i = 0; i < puzz.size(); i++)
cout << setw(3) << i;
cout << endl;
for (int i = 0; i < puzz.size(); i++)
{
cout << setw(2) << i;
for (int j = 0; j < puzz.size(); j++)
{
cout << setw(3);
if (massk[i][j] == -1 || massk[i][j] == -2) // mark
{
cout.clear();
cout << " \e[38;2;255;20;20m";
cout << '$' << "\e[0m";
}
else if (massk[i][j] == 1)
{
if (puzz[i][j] == -1)
{
cout.clear();
if (IWin)
cout << " \e[38;2;20;255;20m";
else
cout << " \e[38;2;255;20;20m";
cout << '*' << "\e[0m";
}
else if (puzz[i][j] == 0)
cout << '.';
else
cout << puzz[i][j];
}
else
cout << '#';
}
cout << endl;
}
}
void turn(puzzle_type pz, mask_type &msk, int x, int y)
{
if (x < 0 || y < 0 || y > pz.size() - 1)
return;
if (x > pz[y].size() - 1)
return;
if (msk[y][x] != 0 || pz[y][x] != 0)
return;
msk[y][x] = 1;
turn(pz, msk, x + 1, y);
turn(pz, msk, x - 1, y);
turn(pz, msk, x, y + 1);
turn(pz, msk, x, y - 1);
}
bool isDie(puzzle_type pz, mask_type msk)
{
for (int i = 0; i < pz.size(); i++)
{
for (int j = 0; j < pz.size(); j++)
{
if (msk[i][j] == 1 && pz[i][j] == -1)
return true;
}
}
return false;
}
bool isWin(puzzle_type pz, mask_type msk, int minecount)
{
int count = 0;
for (int i = 0; i < pz.size(); i++)
{
for (int j = 0; j < pz.size(); j++)
{
if (msk[i][j] == 1)
count++;
}
}
if (count == (pz.size() * pz.size() - minecount))
return true;
else
{
int fl = 0;
for (int i = 0; i < pz.size(); i++)
{
for (int j = 0; j < pz.size(); j++)
{
if (msk[i][j] == -1 || msk[i][j] == -2 && pz[i][j] == -1)
++fl;
}
}
if (fl == minecount)
return true;
else
return false;
}
}
string rules = "Input <x> <y> to controll the game,Example: 0 0. And Use <\'m\'> <x> <y> to mark a place,Example: m 0 0.\n";
int n, mineCnt;
int main()
{
n = 9, mineCnt = 2;
puzzle_type puzzlee(n, vector<int>(n, 0));
mask_type mask(n, vector<int>(n, 0));
vector<pair<int, int>> Pos_Mines(mineCnt);
// Mine Gen
for (int i = 0; i < mineCnt; i++)
{
bool fl = 1;
for (; fl;)
{
auto x = randint(0, n - 1), y = randint(0, n - 1);
if (puzzlee[y][x] != -1)
{
puzzlee[y][x] = -1;
Pos_Mines[i] = {x, y};
fl = 0;
}
}
}
// Count Mines
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (puzzlee[i][j] != -1)
{
puzzlee[i][j] = CountMines(puzzlee, j, i);
}
}
}
// Gaming
int inx, iny;
char oper;
cout << rules;
cout << "size:" << n << "*" << n << ",mines:" << mineCnt << endl;
for (;;)
{
// Print Puzzle
printPz(puzzlee, mask);
// If lose
if (isDie(puzzlee, mask))
{
for (int i = 0; i < mineCnt; i++)
mask[Pos_Mines[i].second][Pos_Mines[i].first] = 1;
cout << "\e[38;2;200;100;200m" << "-x " << "\e[0m" << endl;
printPz(puzzlee, mask, true, false);
cout << "\e[38;2;255;20;20m"
<< "X.X\n"
<< "\e[38;2;100;220;100mYou Died,Game Over. 菜就多练,输不起就别玩。以前是以前,现在是现在。"
<< "\e[0m\n";
break;
}
// If win
if (isWin(puzzlee, mask, mineCnt))
{
for (int i = 0; i < mineCnt; i++)
mask[Pos_Mines[i].second][Pos_Mines[i].first] = 1;
cout << "\e[38;2;150;200;250m" << "-x " << "\e[0m" << endl;
printPz(puzzlee, mask, false, true);
cout << "\e[38;2;255;150;50m"
<< "^-^\n"
<< "You WIN!"
<< "\e[0m\n";
break;
}
cout << "\e[38;2;200;100;200m" << "-> " << "\e[0m";
oper = ' ';
cin >> oper;
if (isdigit(oper))
{
inx = oper - '0';
cin >> iny;
}
else
{
cin >> inx >> iny;
if (oper != 'm')
continue;
}
if (inx < 0 || iny < 0 || iny > n - 1 || inx > n - 1)
{
continue;
}
if (oper == 'm')
{
if (mask[iny][inx] == 0)
mask[iny][inx] = -2;
else if (mask[iny][inx] == 1)
mask[iny][inx] = -1;
else if (mask[iny][inx] == -1)
mask[iny][inx] = 1;
else if (mask[iny][inx] == -2)
mask[iny][inx] = 0;
}
else
{
if (puzzlee[iny][inx] == 0)
turn(puzzlee, mask, inx, iny);
if (mask[iny][inx] == -1)
mask[iny][inx] = 1;
else if (mask[iny][inx] == -2)
mask[iny][inx] = 0;
else
mask[iny][inx] = 1;
}
}
return 0;
}
有人要玩扫雷吗?