Minesweeper - C

game development c

The other day I was tutoring a freshman programmer program his first minesweeper text game in C++. Here is the resulting code.

#include <iostream>
#include<cstdlib>
#include<ctime>

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() {
ใ€€ใ€€ใ€€ใ€€std::cout << "Welcome to MineSweeper!" << std::endl;
ใ€€ใ€€ใ€€ใ€€init();
ใ€€ใ€€ใ€€ใ€€bool playing = true;
ใ€€ใ€€ใ€€ใ€€while(playing) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€print();
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€player();
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(isWin()) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "You Win!" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€playing = false;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else if(isLose()) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "You Lose!" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€playing = false;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}

ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(!playing) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€playing = playNewGame();
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(playing) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€init();
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€std::cout << "Exiting Minesweeper!" << std::endl;
ใ€€ใ€€ใ€€ใ€€return 0;
}

void init() {
ใ€€ใ€€ใ€€ใ€€std::cout << "New Game!" << std::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;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "Play another game? Y or N" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cin >> choice;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(choice == 'N' || choice == 'n') {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€return false;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€selected = true;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else if(choice == 'Y' || choice == 'y') {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€return true;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€selected = true;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "That is not a valid choice!" << std::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;
}

void player() {
ใ€€ใ€€ใ€€ใ€€std::cout << "Commands: R => reveal square, F => set flag, C => cheat, N => New Game" << std::endl;
ใ€€ใ€€ใ€€ใ€€bool selected = false;
ใ€€ใ€€ใ€€ใ€€while(!selected) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€char choice;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cin >> choice;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(choice == 'R' || choice == 'r') {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€uncover();
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€selected = true;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else if(choice == 'F' || choice == 'f') {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€setFlag();
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€selected = true;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else if(choice == 'C' || choice == 'c') {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€cheat();
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else if(choice == 'N' || choice == 'n') {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€init();
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€selected = true;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "Invalid Choice!" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€}

}

void uncover() {
ใ€€ใ€€ใ€€ใ€€bool selected = false;
ใ€€ใ€€ใ€€ใ€€while(!selected) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€int x;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€int y;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "Input X (1 - " << (DIMX) << ")" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cin >> x;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "Input Y (1 - " << (DIMY) << ")" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cin >> y;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€x--;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€y--;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(x >= 0 && x < DIMX && y >= 0 && y < DIMY) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€reveal(x, y); // call recursive reveal algorithm
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€selected = true;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "(X,Y) Values out of range!" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€}
}

void setFlag() {
ใ€€ใ€€ใ€€ใ€€bool selected = false;
ใ€€ใ€€ใ€€ใ€€while(!selected) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€int x;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€int y;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "Input X (1 - " << (DIMX) << ")" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cin >> x;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "Input Y (1 - " << (DIMY) << ")" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cin >> y;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€x--;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€y--;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(x >= 0 && x < DIMX && y >= 0 && y < DIMY) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€display[x][y] = FLAG;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€selected = true;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "(X,Y) Values out of range!" << std::endl;
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€}
}

void cheat() {
ใ€€ใ€€ใ€€ใ€€std::cout << "Cheating is bad!" << std::endl;
ใ€€ใ€€ใ€€ใ€€std::cout << "   ";
ใ€€ใ€€ใ€€ใ€€for(int x = 0; x < DIMX; x++) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << (x + 1) << " ";
ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€std::cout << std::endl << "";
ใ€€ใ€€ใ€€ใ€€for(int x = 0; x < DIMX; x++) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "";
ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€std::cout << std::endl;
ใ€€ใ€€ใ€€ใ€€for(int y = 0; y < DIMY; y++) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << (y + 1) << "  ";
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€for(int x = 0; x < DIMX; x++) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(state[x][y] == MINE) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "@ ";
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(state[x][y] == 0) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std:: cout << "  ";
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std:: cout << state[x][y] << " ";
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << std::endl;
ใ€€ใ€€ใ€€ใ€€}
}

void print() {
ใ€€ใ€€ใ€€ใ€€std::cout << "   ";
ใ€€ใ€€ใ€€ใ€€for(int x = 0; x < DIMX; x++) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << (x + 1) << " ";
ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€std::cout << std::endl << "";
ใ€€ใ€€ใ€€ใ€€for(int x = 0; x < DIMX; x++) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "";
ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€std::cout << std::endl;
ใ€€ใ€€ใ€€ใ€€for(int y = 0; y < DIMY; y++) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << (y + 1) << "  ";
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€for(int x = 0; x < DIMX; x++) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(display[x][y] == COVERED || display[x][y] == 'F') { // is it tile still uncovered
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << display[x][y] << " ";
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else { // display the state
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(state[x][y] == MINE) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << "@ ";
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€if(state[x][y] == 0) {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std:: cout << "  ";
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€} else {
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std:: cout << state[x][y] << " ";
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}
ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€}

ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€std::cout << std::endl;
ใ€€ใ€€ใ€€ใ€€}
}

Sample Output:



Projects

Site

Social

Tags