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
|
// Minesweeper 4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
int score = 0;
int X,Y;
/* 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)
{
for (rows=0; rows<8; rows++)
{
for (columns=0; columns<8; columns++)
{
if (board[rows][columns] == 'e')
{ cout<<"- ";} //dash if guessed and no bomb
else if (board[rows][columns] == 'x')
{cout<<"X ";} // X marks a marked bomb
else
{ cout<<". ";} //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 (1-8)"<<endl;
cin>>X;
cout<<"Enter Y Co-ordinate (1-8)"<<endl;
cin>>Y;
cout<<"Enter M to make move, B to mark as bomb"<<endl;
cin>>userchoice;
if (Y>columns||X>rows)
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;
do
{
if (board[X][Y] !='b')
{
minecounter++;
}
} while (minecounter < mines);
}
void beginner() { //Beginner
int rows=9, columns=9, 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
cout<<set_mines;
//play the game
play_board(beginnerboard, rows, columns, mines);
cout<<score<<endl;
system ("pause");
delete[] beginnerboard;
}
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' : break;
case '3' : 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;
}
|