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
|
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
const int DIMX =6;
const int DIMY = 6;
const int MINES = 6;
const int MINE = -1;
const char COVERED = 'X';
const char UNCOVERED = ' ';
const char FLAG = 'F';
int state[DIMX][DIMY];
char display[DIMX][DIMY];
void init(); // initialize game states
int countSorroundingMines(int x, int y); // helper function used by init
void reveal(int x, int y);
void player(); // handle player input
void setFlag();
void uncover();
void print(); // print the minefield
void cheat(); // print out the mines
bool isWin();
bool isLose();
bool playNewGame();
int main() {
cout << "Welcome to MineSweeper!" << endl;
init();
bool playing = true;
while(playing) {
print();
player();
if(isWin()) {
cout << "You Win!" <<endl;
playing = false;
} else if(isLose()) {
cout << "You Lose!" << endl;
playing = false;
}
if(!playing) {
playing = playNewGame();
if(playing) {
init();
}
}
}
cout << "Exiting Minesweeper!" << endl;
return 0;
}
void init() {
cout << "New Game!" << endl;
// set display to "uncovered"
for(int y = 0; y < DIMY; y++) {
for(int x = 0; x < DIMX; x++) {
display[x][y] = COVERED;
}
}
// initialize mines
srand(time(0));
for(int i = 0; i < MINES; i++) {
bool placed = false;
while(!placed) {
int x = rand() % DIMX;
int y = rand() % DIMY;
if(state[x][y] != MINE) { // check mine not set
placed = true;
state[x][y] = MINE;
}
}
}
// place hint numbers
for(int y = 0; y < DIMY; y++) {
for(int x = 0; x < DIMX; x++) {
state[x][y] = countSorroundingMines(x, y);
}
}
}
bool playNewGame() {
bool selected = false;
while(!selected) {
char choice;
cout << "Play another game? Y or N" << endl;
cin >> choice;
if(choice == 'N' || choice == 'n') {
return false;
selected = true;
} else if(choice == 'Y' || choice == 'y') {
return true;
selected = true;
} else {
cout << "That is not a valid choice!" << endl;
}
}
}
void reveal(int x, int y) {
if(x >= 0 && x < DIMX && y >= 0 && y < DIMY) { // check that x and y are valid
if(display[x][y] == UNCOVERED) {
return;
}
display[x][y] = UNCOVERED;
if(state[x][y] == MINE || state[x][y] != 0) {
return;
}
reveal(x - 1, y - 1);
reveal(x, y - 1);
reveal(x + 1, y - 1);
reveal(x - 1, y);
reveal(x + 1, y);
reveal(x - 1, y + 1);
reveal(x, y + 1);
reveal(x + 1, y + 1);
} else {
return;
}
}
/*
[x-1, y-1][x, y-1][x+1, y-1]
[x-1, y ][x, y ][x+1, y ]
[x-1, y+1][x, y+1][x+1, y+1]
*/
int countSorroundingMines(int x, int y) {
if(state[x][y] == MINE) {
return MINE;
}
int num = 0;
if(x > 0 && y > 0) { // top left
if(state[x - 1][y - 1] == MINE) {
num++;
}
}
if(y > 0) { // top
if(state[x][y - 1] == MINE) {
num++;
}
}
if(x < DIMX - 1 && y > 0) { // top right
if(state[x + 1][y - 1] == MINE) {
num++;
}
}
if(x > 0) { // left
if(state[x - 1][y] == MINE) {
num++;
}
}
if(x < DIMX - 1) { // right
if(state[x + 1][y] == MINE) {
num++;
}
}
if(x > 0 && y < DIMY - 1) { // bottom left
if(state[x - 1][y + 1] == MINE) {
num++;
}
}
if(y < DIMY - 1) { // bottom
if(state[x][y + 1] == MINE) {
num++;
}
}
if(x < DIMX - 1 && y < DIMY - 1) { // bottom right
if(state[x + 1][y + 1] == MINE) {
num++;
}
}
return num;
}
bool isWin() {
bool win = true;
for(int y = 0; y < DIMY; y++) {
for(int x = 0; x < DIMX; x++) {
win &= ((display[x][y] == UNCOVERED && state[x][y] != MINE) ||
((display[x][y] == COVERED || display[x][y] == FLAG) && state[x][y] == MINE));
}
}
return win;
}
bool isLose() {
for(int y = 0; y < DIMY; y++) {
for(int x = 0; x < DIMX; x++) {
if (display[x][y] == UNCOVERED && state[x][y] == MINE) {
return true;
}
}
}
return false;
}
|