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
|
// Minesweeper 4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int score = 0;
int X,Y;
char name;
/* we take out the functionality of displaying the board
** this way we can display beginner/intermediate/... boards
** with just this function.*/
void display_board(char** board, int rows, int columns)
{
int linecharacters = 0;
char vlign = '|';
char hlign = '_';
// printing the first row here
cout << " |";
linecharacters += 3;
for (int i=0; i<columns; i++)
{
cout << i;
if (i<=9)
{
cout << " ";
linecharacters++;
}
cout << "|";
linecharacters += (i > 9) ? 3 : 2;
}
cout << endl;
string line = string(linecharacters, hlign);
int r;
for (r=0; r<rows; r++)
{
cout << line << endl;
/* as for our Y axis numbers, we want them at the start of every row
* so we put them before we start generating dots or dashes
* we can use the counter of the row-loop as the actual numbers: */
if (r<=9) { cout << r << " " << vlign; } //prints space and number
else { cout << r << vlign; } //prints 2-digit number
for (int c=0; c<columns; c++)
{
if (board[r][c] == 'e')
{ cout<<"- "<<vlign;} //dash if guessed and no bomb
else if (board[r][c] == 'x')
{cout<<"X "<<vlign;} // X marks a marked bomb
else
{ cout<<". "<<vlign;} //point if not guessed, or if bomb
}
cout<<endl;
}
}
/* we take out the functionality of playing i.e. marking fields,
** again so that this can apply to all levels */
void play_board(char** board, int rows, int columns, int mines)
{
int guessed_mines = 0; //keep track of number of correctly guessed mines
int X, Y; //user specified X/Y coordinates
char userchoice;
//we need to do the following in a loop until either the player hits a bomb
//or the player finds the mines and wins the game
do
{
display_board(board, rows, columns);
cout<<"Enter X Co-ordinate"<<endl;
cin>>X;
cout<<"Enter Y Co-ordinate"<<endl;
cin>>Y;
cout<<"Enter M to make move, B to mark as bomb"<<endl;
cin>>userchoice;
if (Y>rows||X>columns)
cout << "You have entered the wrong Co-ordinates, Please Retry" << endl;
//if user choses Move and hits a bomb, game ends
if (userchoice=='m' && board[X][Y] == 'b')
{
cout << "Boom! Sorry, you lost this game. Better luck next time!" <<endl;
cout << "Your total score for this game is : ";
break;
}
//if user choses mark Bomb on a bomb field, set value to x
else if (userchoice=='b' && board[X][Y] == 'b')
{
board[X][Y] = 'x';
guessed_mines++;
score = score + 10;
}
//possibly you may want to add a check when user marks bomb but field is empty.
//if field is empty and not already marked as bomb, mark as [e]mpty.
else
{
if (board[X][Y] != 'x') board[X][Y] = 'e';
++score;
}
} while (guessed_mines < mines);
//if the number of guessed mines is indeed the number of mines in the board, win
if (guessed_mines == mines)
{
cout << "You won!" << endl;
//you can insert a question here like "play again? next level?"
}
}
void set_mines(char** board, int columns, int rows, int mines)
{
int minecounter = 0, X, Y;
do
{
X = rand()%rows; //we use rows and columns passed in the
Y = rand()%columns; //parameters so it will work in whatever level we use this
if (board[X][Y] !='b')
{
board[X][Y] = 'b'; //Assign the bomb
minecounter++;
}
} while (minecounter < mines);
}
void beginner() { //Beginner
int rows=8, columns=8, mines=10;
cout << "You have selected to play the Beginner level of minesweeper, Good Luck!" << endl;
//declare and fill the board matrix, can't use regular array as we're passing
//this to functions, don't worry about it now, just know this is an ugly quickfix.
char** beginnerboard = new char *[rows];
for (int i=0;i<rows;i++)
{
beginnerboard[i] = new char[columns];
}
//assign bombs by putting the letter "b" in certain fields of our board
set_mines(beginnerboard, rows, columns, mines);
//play the game
play_board(beginnerboard, rows, columns, mines);
cout<<score<<endl;
system ("pause");
delete[] beginnerboard;
}
void intermediate() { //Intermediate
int rows=16, columns=16, mines=40;
cout << "You have selected to play the Intermediate level of minesweeper, Good Luck!" << endl;
//declare and fill the board matrix, can't use regular array as we're passing
//this to functions, don't worry about it now, just know this is an ugly quickfix.
char** intermediateboard = new char *[rows];
for (int i=0;i<rows;i++)
{
intermediateboard[i] = new char[columns];
}
//assign bombs by putting the letter "b" in certain fields of our board
set_mines(intermediateboard, rows, columns, mines);
//play the game
play_board(intermediateboard, rows, columns, mines);
cout<<score<<endl;
system ("pause");
delete[] intermediateboard;
}
void expert() { //Intermediate
int rows=24, columns=24, mines=99;
cout << "You have selected to play the Intermediate level of minesweeper, Good Luck!" << endl;
//declare and fill the board matrix, can't use regular array as we're passing
//this to functions, don't worry about it now, just know this is an ugly quickfix.
char** expertboard = new char *[rows];
for (int i=0;i<rows;i++)
{
expertboard[i] = new char[columns];
}
//assign bombs by putting the letter "b" in certain fields of our board
set_mines(expertboard, rows, columns, mines);
//play the game
play_board(expertboard, rows, columns, mines);
cout<<score<<endl;
system ("pause");
delete[] expertboard;
}
void leaguetable() //Table
{
system("cls");
cout << "This is the League Table" << endl;
system ("pause");
}
void displayMenu() //Menu
{
char choice;
system("cls");
cout << "Welcome to Minesweeper!" << endl;
cout << "1.Beginner" << endl;
cout << "2.Intermediate" << endl;
cout << "3.Expert" << endl;
cout << "4.League Table" << endl;
cout << "5.Exit" << endl;
cout << "Please enter your menu choice: ";
cin >> choice;
switch (choice)
{
case '1' :
beginner();
break;
case '2' :
intermediate();
break;
case '3' :
expert();
break;
case '4' : leaguetable();
break;
case '5' : cout << "Thanks for playing minesweeper" << endl;
break;
default :
cout << "Please enter a value between 1 and 5" << endl;
system ("pause");
}
}
int main()
{
srand(time(NULL));
displayMenu();
system("pause");
return 0;
}
|