Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1.  
  2.  
  3. //////////////hed.cpp
  4.  
  5. #include <iostream>
  6. #include <stdlib.h>
  7. #include <time.h>
  8.  
  9. #include "hed.h"
  10.  
  11.  
  12.  
  13. MinesweeperBoard::MinesweeperBoard(int w,int h, GameMode g)
  14. {
  15. width=w;
  16. height=h;
  17. board[width][height];
  18. revealedcount=0;
  19. state=RUNNING;
  20. GameMode mode=g;
  21. minecount=(height*width/10)*g;
  22. srand(time(NULL));
  23. int arr[minecount+1][2];
  24. int k,l;
  25. for(unsigned int i=0;i<minecount+1;i++)
  26. {
  27. k=rand()%width;
  28. l=rand()%height;
  29. arr[i][0]=k;
  30. arr[i][1]=l;
  31. if(i>0)
  32. {
  33. for(unsigned int j=0;j<i;j++)
  34. {
  35. if(arr[i][0]==arr[j][0] && arr[i][1]==arr[j][2])
  36. {
  37. i--;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. reserveX=arr[minecount][0];
  44. reserveY=arr[minecount][1];
  45.  
  46. for(unsigned int i=0;i<minecount;i++)
  47. {
  48. board[arr[i][0]][arr[i][1]].hasMine=true;
  49. std::cout<<arr[i][0]<<" "<<arr[i][1]<<std::endl;
  50. }
  51.  
  52. }
  53.  
  54.  
  55. void MinesweeperBoard::setField(int width, int height,const Field& field)
  56. {
  57. if (width < this->width && height < this->height) {
  58. this->board[height][width] = field;
  59. }
  60. }
  61.  
  62. void MinesweeperBoard::debug_display()
  63. {
  64. for(int i=0;i<this->height;i++)
  65. {
  66. for(int j=0;j<this->width;j++)
  67. {
  68. std::cout<<"[";
  69.  
  70. if(board[j][i].hasMine==true)
  71. {
  72. std::cout<<"M";
  73. }
  74. else {std::cout<<j;}
  75. if(board[j][i].isRevealed==true)
  76. {
  77. std::cout<<"o";
  78. }
  79. else {std::cout<<".";}
  80. if(board[j][i].hasFlag==true)
  81. {
  82. std::cout<<"f";
  83. }
  84. else {std::cout<<".";}
  85.  
  86. std::cout<<"]";
  87. }
  88. std::cout<<std::endl;
  89. }
  90.  
  91. }
  92.  
  93.  
  94. int MinesweeperBoard::getBoardWidth(int) const
  95. {
  96. return this->width;
  97. }
  98.  
  99. int MinesweeperBoard::getBoardHeight(int) const
  100. {
  101. return this->height;
  102. }
  103.  
  104. int MinesweeperBoard::getMineCount() const
  105. {
  106. return this->minecount;
  107. }
  108.  
  109. int MinesweeperBoard::countMines(int x, int y) const
  110. {
  111. int c=0;
  112. if(board[x][y].isRevealed==false)
  113. {
  114. return -1;
  115. }
  116. if(x>=width || y>=height || x<0 || y<0)
  117. {
  118. return -1;
  119. }
  120.  
  121. if(board[x-1][y-1].hasMine==true)
  122. {c++;}
  123. if(board[x][y-1].hasMine==true)
  124. {c++;}
  125. if(board[x+1][y-1].hasMine==true)
  126. {c++;}
  127. if(board[x-1][y].hasMine==true)
  128. {c++;}
  129. if(board[x+1][y].hasMine==true)
  130. {c++;}
  131. if(board[x-1][y+1].hasMine==true)
  132. {c++;}
  133. if(board[x][y+1].hasMine==true)
  134. {c++;}
  135. if(board[x+1][y+1].hasMine==true)
  136. {c++;}
  137. return c;
  138. }
  139.  
  140. bool MinesweeperBoard::hasFlag(int x, int y) const
  141. {
  142. if(board[x][y].hasFlag==true)
  143. {return true;}
  144. if(x>=width || y>=height || x<0 || y<0 || board[x][y].hasFlag==false || board[x][y].isRevealed==true)
  145. {return false;}
  146. }
  147.  
  148. void MinesweeperBoard::toggleFlag(int x, int y)
  149. {
  150. // board[x][y].hasFlag=true
  151. if(board[x][y].isRevealed==false && board[x][y].hasFlag==false )
  152. {board[x][y].hasFlag=true;}
  153. else{ if(board[x][y].isRevealed==false && board[x][y].hasFlag==true )
  154. {board[x][y].hasFlag=false;}}
  155. }
  156.  
  157. void MinesweeperBoard::revealField(int x, int y)
  158. {
  159. if(x>=width || y>=height || x<0 || y<0 || board[x][y].isRevealed==true || state!=RUNNING|| board[x][y].hasFlag==true)
  160. {}
  161. else
  162. {
  163. board[x][y].isRevealed=true;
  164. revealedcount++;
  165. if(revealedcount==1 && board[x][y].hasMine==true)
  166. {
  167. board[x][y].hasMine=false;
  168. board[reserveX][reserveY].hasMine=true;
  169. }
  170.  
  171. if(board[x][y].hasMine==true)
  172. {
  173. state=FINISHED_LOSS;
  174. }
  175. else if(revealedcount==height*width-minecount)
  176. {
  177. state=FINISHED_WIN;
  178. }
  179. }
  180.  
  181. }
  182.  
  183. bool MinesweeperBoard::isRevealed(int x, int y) const
  184. {
  185. if(board[x][y].isRevealed==true)
  186. {return true;}
  187. if(board[x][y].isRevealed==false)
  188. {return false;}
  189. }
  190.  
  191.  
  192. GameState MinesweeperBoard::getGameState() const
  193. {
  194. return state;
  195. }
  196.  
  197. char MinesweeperBoard::getFieldInfo(int x, int y) const
  198. {
  199. if(x>=width || y>=height || x<0 || y<0)
  200. {return '#';}
  201. if(board[x][y].isRevealed==false && board[x][y].hasFlag==true )
  202. {return 'F';}
  203. if(board[x][y].isRevealed==false && board[x][y].hasFlag==false )
  204. {return '_';}
  205. if(board[x][y].isRevealed==true && board[x][y].hasMine==true )
  206. {return 'x';}
  207. if(board[x][y].isRevealed==true && countMines(x,y)==0 )
  208. {return ' ' ;}
  209. if(board[x][y].isRevealed==true && countMines(x,y)>0 )
  210. {return countMines(x,y)+48;}
  211. }
  212.  
  213. ////////hed.h
  214. #ifndef SEPER_HED_H
  215. #define SEPER_HED_H
  216.  
  217.  
  218.  
  219. #include <iostream>
  220.  
  221.  
  222. enum GameMode { DEBUG, EASY, NORMAL, HARD };
  223. enum GameState { RUNNING, FINISHED_WIN, FINISHED_LOSS };
  224.  
  225.  
  226.  
  227. struct Field
  228. {
  229. bool hasMine;
  230. bool hasFlag;
  231. bool isRevealed;
  232. };
  233.  
  234. class MinesweeperBoard
  235. {
  236. public:
  237. MinesweeperBoard(int,int,GameMode);
  238. void setField(int, int,const Field&);
  239. void debug_display();
  240. int getBoardWidth(int) const;
  241. int getBoardHeight(int) const;
  242. int getMineCount() const;
  243. int countMines(int x, int y) const;
  244. bool hasFlag(int x, int y) const;
  245. void toggleFlag(int x, int y);
  246. void revealField(int x, int y);
  247. bool isRevealed(int x, int y) const;
  248. GameState getGameState() const;
  249. char getFieldInfo(int x, int y) const;
  250.  
  251. public:
  252. Field board[100][100];
  253. int width;
  254. int height;
  255. unsigned int minecount;
  256. GameState state;
  257. int revealedcount;
  258. int reserveX, reserveY;
  259.  
  260. };
  261.  
  262. #endif //SEPER_HED_H
  263.  
  264. ///////////////MSBTextView.h
  265.  
  266. #ifndef SEPER_MSBTEXTVIEW_H
  267. #define SEPER_MSBTEXTVIEW_H
  268.  
  269. #endif //SEPER_MSBTEXTVIEW_H
  270.  
  271. #include <iostream>
  272. #include "hed.h"
  273.  
  274.  
  275. class MSBoardTextView :public MinesweeperBoard
  276. {
  277. public:
  278. MinesweeperBoard brd;
  279. public:
  280. MSBoardTextView(MinesweeperBoard& );
  281.  
  282.  
  283. };
  284.  
  285.  
  286.  
  287. ///////////////MSBTextView.cpp
  288.  
  289. #include <iostream>
  290. #include "hed.h"
  291. #include "MSBTextView.h"
  292.  
  293. MSBoardTextView::MSBoardTextView(MinesweeperBoard& brdt)
  294. {
  295. brd=brdt;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement