Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.81 KB | None | 0 0
  1. /*
  2. * File: main.cpp
  3. * Author: James Rungsawang
  4. * Created on February 11th, 2019, 12:36 PM
  5. * Purpose: Creation of Template to be used for all
  6. * future projects
  7. */
  8.  
  9. //System Libraries
  10. #include <iostream> //Input/Output Library
  11. #include <iomanip> //Format Library
  12. using namespace std;
  13.  
  14. //User Libraries
  15.  
  16. //Global Constants, no Global Variables are allowed
  17. //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
  18. const int N = 10; //Size of board (10 by 10)
  19. const int M = 3; //Size of window/range (3 by 3)
  20. //Function Prototypes
  21.  
  22. void noBomb(int [10][10],int,int); //Ensures the first point and its 3x3 aren't bombs
  23. void startOpen(int[10][10], string[10][10],int,int); //Opens one cell
  24. void floodFill(int[10][10], string[10][10],int,int); //Fills all neighboring blanks
  25. void open3x3(int[10][10], string[10][10],int,int); //Uses startOpen to open 3x3
  26. bool numClear(int[10][10], string [10][10],int,int);
  27. bool clearOne(int[10][10], string [10][10],int,int);
  28. void printTable(string[10][10]);
  29. void printGrid(int [10][10]);
  30. bool gameCheck(string [10][10],bool);
  31.  
  32. //Execution Begins Here!
  33. int main(int argc, char** argv) {
  34.  
  35. int array[N][N]; //The grid with real values
  36. string table[N][N]; //Table displayed to the player
  37.  
  38.  
  39. //Sets every value in grid to 0///////////////////////////////
  40. for(int x = 0; x < N; x++){
  41. for(int y = 0; y < N; y++){
  42. array[x][y] = 0;
  43. }
  44. }
  45. //////////////////////////////////////////////////////////////
  46.  
  47. int numBombs=0; //Counter for the number of bombs generated
  48. srand(time(NULL)); //For rand function
  49.  
  50. int firstx; //Used to move positions within 3x3 for x
  51. int firsty; //Used to move positions within 3x3 for y
  52.  
  53. cout<<"========================================================="<<endl;
  54. cout<<"WELCOME TO MINESWEEPER!!"<<endl;
  55. cout<<"========================================================="<<endl<<endl;
  56. cout<<"Enter the first square to open!"<<endl;
  57. cout<<"(rows first and then the columns)"<<endl;
  58.  
  59. cin>>firstx>>firsty;
  60. while (firstx<0 or firstx>9 or firsty<0 or firsty>9){
  61. cout<<"Invalid inputs, enter (0-9)"<<endl;
  62. cin>>firstx>>firsty;
  63. }
  64.  
  65. //Guarantees for each point in a 3x3 around the first point cannot//
  66. //be a bomb/////////////////////////////////////////////////////////
  67. noBomb(array,firstx,firsty);
  68. noBomb(array,firstx,firsty-1);
  69. noBomb(array,firstx,firsty+1);
  70. noBomb(array,firstx-1,firsty);
  71. noBomb(array,firstx-1,firsty-1);
  72. noBomb(array,firstx-1,firsty+1);
  73. noBomb(array,firstx+1,firsty);
  74. noBomb(array,firstx+1,firsty-1);
  75. noBomb(array,firstx+1,firsty+1);
  76. ////////////////////////////////////////////////////////////////////
  77.  
  78.  
  79. //Loops until there is a certain amount of bombs on the grid
  80. while(numBombs<15)
  81. {
  82. int i=rand()%10;
  83. int j=rand()%10;
  84.  
  85. if(array[i][j]==0 and array[i][j]!=9){
  86. array[i][j]=-1; //Sets bombs to value of -1
  87. numBombs++;
  88. }
  89. }
  90.  
  91.  
  92. //////////////////////////////////////////////////////////////////////////////
  93. //////////////////////////////////////////////////////////////////////////////
  94. //////////////////////////////////////////////////////////////////////////////
  95. for(int x = 0; x < N; x++){
  96. for(int y = 0; y < N; y++){
  97.  
  98. if (array[x][y]!= -1){ //Only loops if point is not a bomb
  99. int count = 0; //Keeps track of number of bombs for each point
  100. int startX = x-1; //Starting point of 3x3 for x
  101.  
  102. for(int p = 0; p < M; p++){
  103. int startY = y-1; //Starting point of 3x3 for y
  104. for (int i = 0; i < M; i++){
  105.  
  106. //Checks that the window is moving within the range of the grid
  107. if (startX>=0 and startY>=0 and startX<N and startY<N){
  108. if (array[startX][startY]==-1){
  109. count++;
  110. }
  111. }
  112. startY++; //Moves to next column of 3x3
  113. }
  114. startX++; //Moves to next row of 3x3
  115. }
  116. //End of calculating bombs for this specific point
  117.  
  118. array[x][y] = count; //Assigns point on grid to number of bombs found
  119. }
  120. }
  121.  
  122. }
  123. //////////////////////////////////////////////////////////////////////////////
  124. //////////////////////////////////////////////////////////////////////////////
  125. //////////////////////////////////////////////////////////////////////////////
  126.  
  127.  
  128. //Sets every value of table to ?
  129. for(int x = 0; x < N; x++){
  130. for(int y = 0; y < N; y++){
  131. table[x][y] = '?';
  132. }
  133. }
  134.  
  135.  
  136. //Opens the first point of grid onto the table
  137. table[firstx][firsty] = to_string(array[firstx][firsty]);
  138. //Opens the 3x3 grind around the first point
  139. open3x3(array,table,firstx,firsty);
  140.  
  141. //Floodfill function ensures every 0 is bordered by a number
  142. floodFill(array,table,firstx,firsty);
  143.  
  144.  
  145. //printGrid(array);
  146. printTable(table);
  147.  
  148.  
  149. bool winGame = false; //Checking condition if game is won
  150.  
  151.  
  152. //THE ACTUAL START OF THE GAME
  153. string choice1;
  154. string choice2;
  155. int pickX;
  156. int pickY;
  157.  
  158. while (winGame == false){
  159. cout<<"Enter a cell to open"<<endl;
  160. cout<<"(rows first then columns)"<<endl;
  161. cin>>choice1;
  162. if (choice1!="f"){
  163. cin>>choice2;
  164. pickX = atoi(choice1.c_str());
  165. pickY = atoi(choice2.c_str());
  166. while (pickX<0 or pickX>9 or pickY<0 or pickY>9){
  167. cout<<"Invalid inputs, enter (0-9)"<<endl;
  168. cin>>choice1>>choice2;
  169. pickX = atoi(choice1.c_str());
  170. pickY = atoi(choice2.c_str());
  171. }
  172. if(table[pickX][pickY] == "?"){
  173. if (array[pickX][pickY]==-1){
  174. cout<<"============================="<<endl;
  175. cout<<"YOU CLICKED ON A BOMB!!"<<endl;
  176. cout<<"============================="<<endl;
  177. return 0;
  178. }
  179.  
  180. else{
  181. table[pickX][pickY] = to_string(array[pickX][pickY]);
  182. floodFill(array,table,pickX,pickY);
  183. printTable(table);
  184. cout<<endl<<"========================================================="<<endl;
  185. }
  186. }
  187. else{
  188. bool failed = false;
  189. failed = numClear(array,table,pickX,pickY);
  190. if (failed == false){
  191. printTable(table);
  192. cout<<endl<<"========================================================="<<endl;
  193. }
  194. else if (failed==true){
  195. cout<<"============================="<<endl;
  196. cout<<"THAT CELL WAS NOT FLAGGED PROPERLY"<<endl;
  197. cout<<"YOU REVEALED A BOMB!!"<<endl;
  198. cout<<"============================="<<endl;
  199. return 0;
  200. }
  201.  
  202. }
  203. }
  204.  
  205. if (choice1=="f"){
  206. cout<<"Enter the coordinates that you would like to flag"<<endl;
  207. cout<<"(rows first and then columns"<<endl;
  208. cin>>pickX;
  209. cin>>pickY;
  210.  
  211. if (table[pickX][pickY]=="?")
  212. table[pickX][pickY] = "F";
  213.  
  214. else if (table[pickX][pickY]=="F")
  215. table[pickX][pickY] = "?";
  216.  
  217. else
  218. cout<<endl<<"That cell is already open, you cannot flag it"<<endl<<endl;
  219.  
  220.  
  221. printTable(table);
  222. } cout<<endl<<"========================================================="<<endl;
  223. winGame = gameCheck(table,winGame);
  224.  
  225. }
  226.  
  227. cout<<"You have won!"<<endl;
  228.  
  229. return 0;
  230. }
  231.  
  232. //Makes the first point and its 3x3 not bombs
  233. void noBomb(int array[10][10],int firstx,int firsty){
  234. if (firstx>=0 and firsty>=0 and firstx<10 and firsty<10)
  235. array[firstx][firsty] = 9;
  236. }
  237.  
  238. //Actually opens the first point on the table via function noBomb
  239. void startOpen(int array[10][10], string table[10][10],int x,int y){
  240. if (x>=0 and y>=0 and x<10 and y<10){
  241. table[x][y] = to_string(array[x][y]);
  242. }
  243. }
  244.  
  245. //Opens a 3x3 space around a given point on the table
  246. void open3x3(int array[10][10], string table[10][10],int firstx,int firsty){
  247. startOpen(array,table,firstx,firsty-1);
  248. startOpen(array,table,firstx,firsty+1);
  249. startOpen(array,table,firstx-1,firsty);
  250. startOpen(array,table,firstx-1,firsty-1);
  251. startOpen(array,table,firstx-1,firsty+1);
  252. startOpen(array,table,firstx+1,firsty-1);
  253. startOpen(array,table,firstx+1,firsty);
  254. startOpen(array,table,firstx+1,firsty+1);
  255. }
  256.  
  257. //This function makes sure that any zero clears bordering zeros
  258. void floodFill(int array[10][10], string table[10][10],int x, int y) {
  259.  
  260. for(int t = 0; t < 100; t++){
  261. for(int x = 0; x < 10; x++){
  262. for(int y = 0; y < 10; y++){
  263. if (table[x][y] == "0")
  264. open3x3(array,table,x,y);
  265. }
  266. }
  267. }
  268. }
  269.  
  270. //Simply prints the table for player
  271. void printTable(string table[10][10]){
  272. int line = 0;
  273. cout<<" 0 1 2 3 4 5 6 7 8 9"<<endl;
  274. cout<<" _____________________________"<<endl;
  275. for(int x = 0; x < N; x++){
  276. cout<<line<<" |";
  277. line++;
  278. for(int y = 0; y < N; y++){
  279. cout<<setw(2)<<table[x][y]<<" ";
  280. }
  281. cout<<endl;
  282. }
  283. cout<<endl<<endl;
  284. }
  285.  
  286. void printGrid(int array[10][10]){
  287. int line = 0; //Simply says what row you're on
  288. cout<<" 0 1 2 3 4 5 6 7 8 9"<<endl;
  289. cout<<" _______________________________________"<<endl;
  290. for(int x = 0; x < N; x++){
  291. cout<<line<<" |";
  292. line++;
  293. for(int y = 0; y < N; y++){
  294. cout<<setw(3)<<array[x][y]<<" ";
  295. }
  296. cout<<endl;
  297. }
  298. cout<<endl<<endl;
  299. }
  300.  
  301. bool gameCheck(string table[10][10], bool winGame){
  302. int score = 0;
  303. for(int x = 0; x < 10; x++){
  304. for(int y = 0; y < 10; y++){
  305. if (table[x][y]!="F" and table[x][y]!="?"){
  306. score++;
  307. }
  308. }
  309. }
  310. //cout<<"Your score is "<<score<<endl;
  311. if (score == 85)
  312. return true;
  313.  
  314. else return false;
  315. }
  316. bool numClear(int array[10][10], string table[10][10], int x, int y){
  317. bool openBomb = false;
  318. openBomb = clearOne(array,table,x,y-1);
  319.  
  320. if (openBomb == false)
  321. openBomb = clearOne(array,table,x,y+1);
  322. if (openBomb == false)
  323. openBomb = clearOne(array,table,x-1,y);
  324. if (openBomb == false)
  325. openBomb = clearOne(array,table,x-1,y-1);
  326. if (openBomb == false)
  327. openBomb = clearOne(array,table,x-1,y+1);
  328. if (openBomb == false)
  329. openBomb = clearOne(array,table,x+1,y-1);
  330. if (openBomb == false)
  331. openBomb = clearOne(array,table,x+1,y);
  332. if (openBomb == false)
  333. openBomb = clearOne(array,table,x+1,y+1);
  334.  
  335. return openBomb;
  336. }
  337.  
  338. bool clearOne(int array[10][10], string table[10][10],int x, int y){
  339. if (table[x][y] == "?"){
  340. if (x>=0 and y>=0 and x<10 and y<10){
  341. table[x][y] = to_string(array[x][y]);
  342.  
  343. if (array[x][y] == -1)
  344. return true;
  345. else
  346. return false;
  347. }
  348. }
  349. return false;
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement