1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
// Prototypes
void Print_Grid (vector<vector<int> >&, vector<vector<string> >&, int, int);
void Play_Game(vector<vector<int> > &, vector<vector<string> > &, int, int);
int main()
{
cout << "Enter # of rows and columns: ";
int gridSize;
cin >> gridSize;
int ROWS = gridSize;
int COLUMNS = gridSize;
// int grid
vector<vector<int> > grid(ROWS, vector<int>(COLUMNS, -2));
// cover grid
vector<vector<string> > cgrid(ROWS, vector<string>(COLUMNS, "*"));
Print_Grid(grid, cgrid, ROWS, COLUMNS);
cout << "\n";
Play_Game(grid, cgrid, ROWS, COLUMNS);
return 0;
}
void Print_Grid(vector<vector<int> > &grid, vector<vector<string> > &cgrid, int ROWS, int COLUMNS)
{
srand(time(0));
int mines;
// Assigns -1's randomly to some squares
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLUMNS; j++)
{
int randN = (rand()%10)+1;
if(randN==1 || randN==2 || randN==3)
{
grid[i][j] = -1; // mines are -1's
}
}
}
// this second nested loop checks surrounding squares for each elements.
// However many mines are adjacent to the square,
// that is the number assigned to that square.
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLUMNS; j++)
{
mines = 0;
if(grid[i][j] != -2)
continue;
else if(grid[i][j] == -2)
{
// checks squares surrounding top left corner
if(i == 0 && j == 0)
{
if(grid[i][j+1] == -1)
mines++;
if(grid[i+1][j] == -1)
mines++;
if(grid[i+1][j+1] == -1)
mines++;
}
// top right corner
if(i == 0 && j == COLUMNS-1)
{
if(grid[i][j-1] == -1)
mines++;
if(grid[i+1][j] == -1)
mines++;
if(grid[i+1][j-1] == -1)
mines++;
}
// bottom left corner
if(i == ROWS-1 && j == 0)
{
if(grid[i][j+1] == -1)
mines++;
if(grid[i-1][j] == -1)
mines++;
if(grid[i-1][j+1] == -1)
mines++;
}
// bottom right corner
if(i == ROWS-1 && j == COLUMNS-1)
{
if(grid[i][j-1] == -1)
mines++;
if(grid[i-1][j-1] == -1)
mines++;
if(grid[i-1][j] == -1)
mines++;
}
// top edge
if( i == 0 && j != 0 && j != COLUMNS-1)
{
if(grid[i][j+1] == -1)
mines++;
if(grid[i][j-1] == -1)
mines++;
if(grid[i+1][j] == -1)
mines++;
if(grid[i+1][j+1] == -1)
mines++;
if(grid[i+1][j-1] == -1)
mines++;
}
// bottom edge
if(i == ROWS-1 && j != 0 && j != COLUMNS-1)
{
if(grid[i][j+1] == -1)
mines++;
if(grid[i][j-1] == -1)
mines++;
if(grid[i-1][j] == -1)
mines++;
if(grid[i-1][j+1] == -1)
mines++;
if(grid[i-1][j-1] == -1)
mines++;
}
// left edge
if(j == 0 && i != 0 && i != ROWS-1)
{
if(grid[i+1][j] == -1)
mines++;
if(grid[i-1][j] == -1)
mines++;
if(grid[i][j+1] == -1)
mines++;
if(grid[i+1][j+1] == -1)
mines++;
if(grid[i-1][j+1] == -1)
mines++;
}
// right edge
if(j == COLUMNS-1 && i != 0 && i != ROWS-1)
{
if(grid[i+1][j] == -1)
mines++;
if(grid[i-1][j] == -1)
mines++;
if(grid[i][j-1] == -1)
mines++;
if(grid[i+1][j-1] == -1)
mines++;
if(grid[i-1][j-1] == -1)
mines++;
}
// all other possible squares to be checked
if(i !=0 && i!= ROWS-1 && j != 0 && j != COLUMNS-1)
{
if(grid[i][j+1] == -1)
mines++;
if(grid[i][j-1] == -1)
mines++;
if(grid[i+1][j] == -1)
mines++;
if(grid[i-1][j] == -1)
mines++;
if(grid[i+1][j+1] == -1)
mines++;
if(grid[i+1][j-1] == -1)
mines++;
if(grid[i-1][j-1] == -1)
mines++;
if(grid[i-1][j+1] == -1)
mines++;
}
grid[i][j] = mines; // assign number of mines adjecent to that square
}
}
}
// print out int grid
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLUMNS; j++)
{
cout << setw(3) << grid[i][j] << setw(3);
}
cout << endl;
}
cout << "\n\nCOVER GRID\n\n";
//Print cover grid
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLUMNS; j++)
{
cout << setw(3) << cgrid[i][j] << setw(3);
}
cout << endl;
}
}
void Play_Game(vector<vector<int> > &grid, vector<vector<string> > &cgrid, int ROWS, int COLUMNS)
{
// Bool variables used to break out of the nested loops
bool flag1 = true;
bool flag2 = true;
bool flag3 = true;
int row, column; // coordinares entered by user stored in these two variables
while (flag1==true)
{
cout << "Enter row and column # (separated by space): ";
cin >> row >> column;
cout << "\n";
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLUMNS; j++)
{
if(grid[row][column] == grid[i][j] && grid[i][j])
cout << setw(3) << grid[i][j] << setw(3);
else cout << setw(3) << cgrid[i][j] << setw(3);
if(grid[row][column] == -1)
flag3 = false;
}
if(flag3 == false)
flag2 = false;
cout << endl;
}
if(flag2 == false)
flag1 = false;
cout << "\n";
}
cout << "\nYOU LOSE!" << endl;
|