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 258 259 260 261 262 263 264 265 266 267 268 269 270
|
#include <iostream>
#include <conio.h>
#include <algorithm>
#include <ctime>
#include <string>
#include <climits>
using namespace std;
const int n = 8; //used to pass parramaters for beginner minefield
int Row = 0;
int counter = 0;
int Column = 0;
char choice = 0;
int gameGrid[8][8]; ///changed to int
//------------------------------------Class for game board -------------------------------------------
class gameBoard
{
public:
void init_gameGrid() ///renamed
{
for (int j = 0; j < 8; j++)
{
for (int i = 0; i < 8; i++)
{
gameGrid[j][i]=0;
}
}
}
void draw_gameGrid() ///
{
int line = 1;
cout << " 1 2 3 4 5 6 7 8\n";
for (int j = 0; j < 8; j++)
{
cout << line++;
for (int i = 0; i < 8; i++) ///redefined this loop
{
if(gameGrid[j][i] > 0)
cout << "[" << gameGrid[j][i] << "]";
else
cout<<"[ ]";
}
cout<<endl;
}
}
void drawBeginnerBoard(int beginnerMField[][n])
{
int line = 1;
cout << " 1 2 3 4 5 6 7 8\n";
for (int j = 0; j < 8; j++)
{
cout << line++;
for (int i = 0; i < 8; i++)
{
cout << "[" << beginnerMField[j][i] << "]";
}
cout<<endl;
}
}
// randomises 10 mines (9) into an array 8 x 8(beginner board)
void shuffleBoard(int beginnerMField[][n]) ///renamed
{
srand ((unsigned int)(time(0)));
for(int x=0; x<8; x++)
{
for(int j=0;j<8; j++)
{
int xCoord = rand()%8;
int yCoord = rand()%8;
int temp = beginnerMField[x][j];
beginnerMField[x][j] = beginnerMField[xCoord][yCoord];
beginnerMField[xCoord][yCoord] = temp;
}
}
}
};
//-----------------------------End game board class -------------------------------------------------
//---------------------------------Class for Move functions------------------------------------------
class Move
{
public:
int getPos() ///new function
{
int val = 0;
std::cin>>val;
while(val<1 || val>9)
{
std::cout<<"out of range retry: ";
std::cin>>val;
}
return val;
}
bool validCoords(int _row,int _col)///checks if [_row,_col] are within the grid // new func
{
if((_row < 0 || _row > 7) || (_col < 0 || _col > 7))
return false;
return true;
}
bool gotMine(int board[][n],int _row, int _col) /// return true if the coords have a mine //new func
{
return (board[_row][_col] == 9);
}
void checkMines(int beginnerMField[][n]) ///renamed nad redifined function Move
{
int _MinesFound = 0;
int _NeighbrCellsOffset[8][2] = {{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,+1},{-1,0}};
for(int i =0; i<8; i++)
{
int _neigx = _NeighbrCellsOffset[i][0];//neighbor cell row
int _neigy = _NeighbrCellsOffset[i][1];//neighbor cell column
if(validCoords((Row+_neigx),(Column+_neigy)) && gotMine(beginnerMField,(Row+_neigx),(Column+_neigy)))
++_MinesFound;
}
gameGrid[Row][Column] = _MinesFound;
Gboard.draw_gameGrid();
}
void playerbeginner(int beginnerMField[][n]) ///working now
{
bool alive = true;
while(alive)
{
cout << "Enter Row number 1-9: "<<endl;
Row = getPos()-1;
cout << "Enter Column number 1-9: "<<endl;
Column = getPos()-1;
cout << "[" << Row+1 << "][" << Column+1 << "]" << "\n";
cout <<beginnerMField[Row][Column] << "\n";
Move move1;
move1.checkMines(beginnerMField);
if(gotMine(beginnerMField,Row,Column))
{
char answer= ' ';
system("CLS");
cout<<" BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOM"<<endl;
cout<<" GAME OVER"<<endl;
cout<<" Play again? Type Y or N"<<endl;
cin>>answer;
if(answer == 'Y' || answer == 'y')
{
Gboard.shuffleBoard(beginnerMField);
Gboard.init_gameGrid();
Gboard.draw_gameGrid();
Gboard.drawBeginnerBoard(beginnerMField);
}
else
alive = false;
}
}
}
private:
gameBoard Gboard; ///put this here
};
//-------------------------------------------------End Counting array squares around move-----------------------------------------------------------------------------------------
//--------------------------------------------------------------------Menu Starts----------------------------------------------------------------------------
class menu
{
public:
void Menu(int beginnerMField[][n]){
int selection;
cout << "********************************MINESWEEPER*************************************\n\n";
cout << "1. Play Beginner Game\n" << "2. Play Intermediate Game\n" << "3. Play Advanced Game\n" << "4. Show Score Board\n" << "5. Quit Game\n\n\n";
cout << "Please enter your selection: ";
cin >> selection;
cout << endl;
switch (selection)
{
case 1:
cout << "Play Beginner Game\n";
cout << "\n";
system("CLS");
gameBoard Gboard; ///one gameboard is enough
Gboard.shuffleBoard(beginnerMField);
Gboard.init_gameGrid();
Gboard.draw_gameGrid();
Gboard.drawBeginnerBoard(beginnerMField);
Move Move1;
Move1.playerbeginner(beginnerMField);
break;
case 2:
cout << "Play Intermediate Game\n";
cout << "\n";
system("CLS");
break;
case 3:
cout << "Play Advanced Game\n";
cout << "\n";
system("CLS");
break;
case 4:
cout << "Show Score Board\n";
cout << "\n";
break;
case 5:
cout << "Goodbye.\n";
break;
default: cout << selection << "is not a valid menu item.\n";
cout << endl;
}
}
};
//---- End menu----
int main(){
bool alive = true;
int beginnerMField[][n] = {
{0,0,9,0,0,9,0,0},
{0,0,9,0,0,9,0,0},
{0,0,9,0,0,0,0,0},
{0,0,9,0,0,0,0,0},
{0,0,0,0,0,9,0,0},
{0,0,9,0,0,0,0,0},
{0,0,9,0,0,0,0,0},
{0,0,0,9,0,0,0,0}
};
menu Menu1;
Menu1.Menu(beginnerMField);
}
|