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
|
#include <iostream>
#include <string>
using namespace std;
int X,Y;
char name;
void display_board(char** board, int rows, int columns)
{
int linecharacters = 0;
char vlign = ' ';
char hlign = ' ';
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;
if (r<=9) { cout << r << " " << vlign; }
else { cout << r << vlign; }
for (int c=0; c<columns; c++)
{
if (board[r][c] == 'e')
{ cout<<"O "<<vlign;}
else if (board[r][c] == 'x')
{cout<<"M "<<vlign;}
else
{ cout<<"? "<<vlign;}
}
cout<<endl;
}
}
void play_board(char** board, int rows, int columns, int mines)
{
int guessed_mines = 0;
int X, Y;
char userchoice;
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 (userchoice=='m' && board[X][Y] == 'b')
{
cout << "You Hit a Mine" <<endl;
board[X][Y] = 'x';
guessed_mines++;
}
else if (userchoice=='b' && board[X][Y] == 'b')
{
board[X][Y] = 'x';
guessed_mines++;
}
else
{
if (board[X][Y] != 'x') board[X][Y] = 'e';
}
} while (guessed_mines < mines);
if (guessed_mines == mines)
{
cout << "You won!" << endl;
}
}
void set_mines(char** board, int columns, int rows, int mines)
{
int minecounter = 0, X, Y;
do
{
X = rand()%rows;
Y = rand()%columns;
if (board[X][Y] !='b')
{
board[X][Y] = 'b';
minecounter++;
}
} while (minecounter < mines);
}
void beginner() { //Beginner
int rows=6, columns=6, mines=5;
cout << "Welcome to minesweeper" << endl;
char** beginnerboard = new char *[rows];
for (int i=0;i<rows;i++)
{
beginnerboard[i] = new char[columns];
}
set_mines(beginnerboard, rows, columns, mines);
play_board(beginnerboard, rows, columns, mines);
system ("pause");
delete[] beginnerboard;
}
int main()
{
beginner();
system("pause");
return 0;
}
|