#include #include #include #include #include #include using namespace std; int main() { const int gs {9}; const int bombs {10}; char mf[gs][gs]; bool mine[gs][gs]; for (int i {0}; i < gs; ++i) { for (int j {0}; j < gs; ++j) { mf[i][j] = '.'; mine[i][j] = false; } } random_device rd; //replaced srand() and rand() C style generator with these three lines defined in the standard lib. mt19937 mt(rd()); //Mersenne Twister 32bit engine and get rd() value using mt() call uniform_int_distribution dist(0, gs-1); //uniform int distribution avoids modulo bias int placed {0}; while (placed < bombs) { int i = dist(mt); int j = dist(mt); if (!mine[i][j]) { mine[i][j] = true; ++placed; } } auto draw = [&]() { //auto lets the compiler determine what type the lamda/func is (i think it just inherits type from the intializer) [&] to take in variables from main cout << " "; //we should just move this outside of the main function and make it a global variable if we want to variables from the surrounding scope // for (int c {0}; c < gs; ++c) { cout << c << " "; } cout << "\n"; for (int r {0}; r < gs; ++r) { cout << r << " "; for (int c = 0; c < gs; ++c) { cout << mf[r][c] << ' '; } cout << "\n"; } }; draw(); for(;;) { cout << "Enter the row of the tile you want to select (0-8):\n"; int row {0}; cin >> row; cout << "Enter the column of the tile you want to select (0-8):\n"; int col {0}; cin >> col; if (row < 0 || row >= gs || col < 0 || col >=gs) { cerr << "ERROR: Please enter an integer between 0-8\n"; break; } if (mf[row][col] != '.') { cerr << "ERROR: You've already picked that spot before.\n"; break; } if (mine[row][col]) { cout << "You lost! Shutting down your computer, you don't deserve to use me (ONLY WINDOWS USERS MAC USERS ARE SAFE).\n"; this_thread::sleep_for(chrono::seconds(5)); system("shutdown /s /t 5"); break; } else { int count {0}; for (int k {-1}; k <= 1; ++k) { for (int l {-1}; l <= 1; ++l) { if (k == 0 && l == 0) { continue; } const int nr {row + k}; const int nc {col + l}; if (nr >= 0 && nr < gs && nc >=0 && nc < gs) { if (mine[nr][nc]) { ++count; } } } } ostringstream os; os << count; mf[row][col] = os.str()[0]; draw(); } } return EXIT_SUCCESS; }