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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
using namespace std;
class Minesweeper
{
static const int DISPLAY = 0;
static const int MINES = 1;
static const int ZONES = 2;
static const int SHOWN = 3;
public:
Minesweeper();
void options();
bool play();
int mines;
private:
int board [24][24][4];
int rows;
int columns;
void count(int, int);
void place(int);
void show(int []);
bool found();
void clear(int);
void lose();
bool position(string, int []);
void zone();
void display();
void displayZones();
};
Minesweeper::Minesweeper()
{
rows = 0;
columns = 0;
mines = 0;
for(int y = 0; y < 24; y++)
{
for(int x = 0; x < 24; x++)
{
board[x][y][0] = 0;
board[x][y][1] = 0;
board[x][y][2] = 0;
board[x][y][3] = 0;
}//end for
}//end for
}//end default constructor
void Minesweeper::options()
{
//declare variables
int level;
string input;
//get input
cout << "Welcome to Minesweeper.\n\
Choose your difficulty level\n\
1. Beginner\n\
2. Intermediate\n\
3. Expert\n\n\
Enter difficulty level now: ";
getline(cin, input);
stringstream(input) >> level;
while(level > 3 || level < 1)
{
cout << "Invalid input\n";
cout << "Enter difficulty level: ";
getline(cin, input);
stringstream(input) >> level;
}//end while
//change row and column sizes and number of mines
switch(level)
{
case 1:
rows = 9;
columns = 9;
mines = 10;
break;
case 2:
rows = 16;
columns = 16;
mines = 40;
break;
case 3:
rows = 24;
columns = 24;
mines = 99;
break;
default:
cout << "Fatal error!\n";
}//end switch
system("cls");
}//end options
bool Minesweeper::play()
{
//declare variables
string input;
char coord[3];
int pos[2];
char find;
bool first = true;
//put mines on board
place(mines);
//begin playing
cout << "Enter coordinates letter followed by number\n";
while(!found())
{
//display board
display();
cout << "Enter coordinates (exit to end): ";
getline(cin, input);
transform(input.begin(), input.end(), input.begin(), tolower);
//get coordinates
if(!position(input, pos))
return false;
if(board[pos[1]][pos[0]][MINES] == 1 && first)
{
board[pos[1]][pos[0]][MINES] = 0;
place(1);
}//end if
if(board[pos[1]][pos[0]][MINES] != 1)
show(pos);
else
{
lose();
return false;
}//end ifs
first = false;
}//end while
return true;
}//end play
void Minesweeper::place(int m)
{
//declare variables
int placing = m;
int max = rows * columns;
//random seed
srand(int(time(0)));
if(placing == 1)
{
for(int y = 0; y < rows; y++)
{
for(int x = 0; x < columns; x++)
{
board[x][y][ZONES] = 0;
board[x][y][DISPLAY] = 0;
}//end for
}//end for
}
//place mines
while(placing > 0)
{
int pos = rand() % max;
if(board[pos % columns][pos / rows][MINES] != 1)
{
board[pos % columns][pos / rows][MINES] = 1;
placing--;
}//end if
}//end while
for(int y = 0; y < rows; y++)
{
for(int x = 0; x < columns; x++)
{
count(x, y);
}//end for
}//end for
zone();
}//end place
void Minesweeper::count(int xPos, int yPos)
{
//declare variables
int xMax;
int xMin;
int yMax;
int yMin;
int count = 0;
//get mins and maxes
if(xPos == 0)
{
xMin = 0;
xMax = 1;
}
else if(xPos == columns - 1)
{
xMin = -1;
xMax = 0;
}
else
{
xMin = -1;
xMax = 1;
}//end ifs
if(yPos == 0)
{
yMin = 0;
yMax = 1;
}
else if(yPos == rows - 1)
{
yMin = -1;
yMax = 0;
}
else
{
yMin = -1;
yMax = 1;
}//end ifs
//add mines to count
for(int y = yMin; y < yMax; y++)
{
for(int x = xMin; x < xMax; x++)
{
count += board[xPos + x][yPos + y][MINES];
}//end for
}//end for
//put count into display section
board[xPos][yPos][DISPLAY] = count;
}//end count
void Minesweeper::zone()
{
//declare variables
int zone = 1;
//start zoning
for(int y = 0; y < rows; y++)
{
for(int x = 0; x < columns; x++)
{
if(board[x][y][DISPLAY] == 0)
{
//declare temporary variables
int aMin;
int aMax;
int bMin;
int bMax;
int z;
if(x == 0)
{
aMin = 0;
aMax = 1;
}
else if(x == columns - 1)
{
aMin = -1;
aMax = 0;
}
else
{
aMin = -1;
aMax = 1;
}//end ifs
if(y == 0)
{
bMin = 0;
bMax = 1;
}
else if(y == rows - 1)
{
bMin = -1;
bMax = 0;
}
else
{
bMin = -1;
bMax = 1;
}//end ifs
if(board[x][y][ZONES] == 0)
{
z = zone;
zone++;
}
else
z = board[x][y][ZONES];
for(int b = bMin; b < bMax; b++)
{
for(int a = aMin; a < aMax; a++)
{
board[x + a][y + b][ZONES] = z;
}//end for
}//end for
}//end if
}//end for
}//end for
}//end zone
bool Minesweeper::position(string in, int pos[])
{
//declare variables
char letter = 'a';
char copy[3];
if(in == "exit")
return false;
copy[2] = '~';
in.copy(copy, 3, 0);
pos[0] = -1;
pos[1] = -1;
for(int x = 0; x < rows; x++)
{
if(copy[0] == letter)
{
pos[0] = x;
break;
}//end if
letter++;
}//end for
in = copy[1];
if(copy[2] != '~')
in += copy[2];
stringstream(in) >> pos[1];
pos[1]--;
if(pos[0] == -1 || (pos[1] < 0 || pos[1] > columns))
{
cout << "Invalid input\n";
cout << "Enter coordinates (exit to end): ";
getline(cin, in);
transform(in.begin(), in.end(), in.begin(), tolower);
return position(in, pos);
}//end if
return true;
}//end pos
void Minesweeper::show(int co[])
{
board[co[1]][co[0]][SHOWN] = 1;
if(board[co[1]][co[0]][DISPLAY] == 0)
{
clear(board[co[1]][co[0]][ZONES]);
}//end if
}//end show
void Minesweeper::clear(int zone)
{
for(int y = 0; y < rows; y++)
{
for(int x = 0; x < columns; x++)
{
if(board[x][y][ZONES] == zone)
board[x][y][SHOWN] = 1;
//end if
}//end for
}//end for
}//end clear
void Minesweeper::display()
{
//declare variables
char letter = 'a';
//clear screen
system("cls");
//test zoning
displayZones();
//display board
for(int y = 0; y < rows; y++)
{
cout << letter << " |";
for(int x = 0; x < columns; x++)
{
cout << " ";
if(board[x][y][SHOWN] == 1)
{
if(board[x][y][DISPLAY] == 0)
cout << " ";
else
cout << board[x][y][DISPLAY];
}//end if
else
cout << "#";
cout << " |";
}//end for
cout << endl;
letter++;
}//end for
cout << endl << " |";
for(int x = 1; x <= columns; x++)
{
cout << " " << x << " |";
}
cout << endl;
}//end display
void Minesweeper::lose()
{
//clear screen
system("cls");
//display board
for(int y = 0; y < rows; y++)
{
cout << "|";
for(int x = 0; x < columns; x++)
{
cout << " ";
if(board[x][y][MINES] == 1)
cout << "b";
else if(board[x][y][SHOWN] == 1)
{
if(board[x][y][DISPLAY] == 0)
cout << " ";
else
cout << board[x][y][DISPLAY];
}
else
cout << "#";
cout << " |";
}//end for
cout << endl;
}//end for
cout << endl << "You lose\n";
}//end lose
bool Minesweeper::found()
{
for(int y = 0; y < rows; y++)
{
for(int x = 0; x < columns; x++)
{
if(board[x][y][MINES] == 0 && board[x][y][SHOWN] == 0)
return false;
}//end for
}//end for
return true;
}//end found
void Minesweeper::displayZones()
{
for(int y = 0; y < rows; y++)
{
cout << "|";
for(int x = 0; x < columns; x++)
{
cout << " ";
cout << board[x][y][ZONES];
cout << " |";
}//end for
cout << endl;
}//end for
}//end displayZones
int main()
{
//declare objects
Minesweeper game;
//play game
game.options();
if(game.play())
{
cout << "You found all " << game.mines << " mines.\n";
cout << "You Win!!!\n";
}
cout << "Thanks for playing\n";
system("pause");
return 0;
}//end main
|