Advertisement
Guest User

demineur v3 wip

a guest
Nov 22nd, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
  5. C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
  6. Code, Compile, Run and Debug online from anywhere in world.
  7.  
  8. *******************************************************************************/
  9. #include <iostream>
  10. #include <vector>
  11. #include <cstdlib>
  12. #include <ctime>
  13.  
  14. using namespace std;
  15. typedef vector<int> VecteurI;
  16. typedef vector<VecteurI> MatriceI;
  17.  
  18. int sizeH = 10;
  19. int sizeW = 10;
  20. int nb_mine = 10;
  21. int hit_x = 5;
  22. int hit_y = 5;
  23. int cases_remaining = sizeH * sizeW - nb_mine;
  24. MatriceI bo_sup(sizeH, VecteurI(sizeW));
  25. MatriceI bo_inf(sizeH, VecteurI(sizeW));
  26. bool init = false; //global if using class
  27. /////
  28. void loose {
  29. cout << "Vous avez malheuresement perdu." << endl << endl;
  30. for (int i = 0 ; i < sizeH ; i++) {
  31. for (int k = 0 ; k < sizeW ; k++) {
  32. if (bo_inf[i][k] == 9) {
  33. cout << "+ ";
  34. }
  35. else if (bo_sup[i][k] == 1) {
  36. cout << " ";
  37. }
  38. else
  39. cout << "# ";
  40. }
  41. cout << endl;
  42. }
  43. cout << endl;
  44. }
  45.  
  46. void limites_tab(int& mX, int& MX, int& mY, int& MY, int posX, int posY)
  47. {
  48. mX = 0;
  49. MX = 0;
  50. mY = 0;
  51. MY = 0;
  52. if (posX == 0) {
  53. mX = 1;
  54. }
  55. else if (posX == sizeH) {
  56. MX = 1;
  57. }
  58.  
  59. if (posY == 0) {
  60. mY = 1;
  61. }
  62. if (posY == sizeW) {
  63. MY = 1;
  64. }
  65. //ternaires ?
  66. }
  67.  
  68. int adjacentes(int posX, int posY)
  69. {
  70. int minX, maxX, minY, maxY;
  71. limites_tab(minX, maxX, minY, maxY, posX, posY);
  72. int adja = 0;
  73.  
  74. for (int x = posX - 1 + minX; x <= posX + 1 - maxX; x++) {
  75. for (int y = posY - 1 + minY; y <= posY + 1 - maxY; y++) {
  76. if (not(x == posX && y == posY)) {
  77. if (bo_inf[x][y] == 9) {
  78. adja++;
  79. }
  80. }
  81.  
  82. }
  83. }
  84. return adja;
  85. }
  86.  
  87. void init_bo_inf()
  88. {
  89. for (int i = 0; i < bo_inf.size(); i++) {
  90. for (int j = 0; j < bo_inf[i].size(); j++) {
  91. tab_inf[i][j] = 0;
  92. }
  93. }
  94.  
  95. srand(time(NULL));
  96. while (nb_mine != 0) {
  97. int x = 0;
  98. int y = 0;
  99. do {
  100. x = rand() % (bp_inf.size() - 0) + 0;
  101. y = rand() % (bo_inf[0].size() - 0) + 0;
  102. } while (bo_inf[x][y] == 9 || bos_sup == 1); ///
  103.  
  104. bo_inf[x][y] = 9;
  105. nb_mine--;
  106. }
  107.  
  108. for (int X = 0; X < bo_inf.size(); X++) {
  109. for (int Y = 0; Y < bo_inf[X].size(); Y++) {
  110. if (bo_inf[X][Y] == 0) {
  111. bo_inf[X][Y] = adjacentes(X,Y);
  112.  
  113. }
  114. }
  115. }
  116. }
  117.  
  118. bool play_hit(vecteurI hit)
  119. {
  120. bo_sup[hit[0]][hit[1]] = 1;
  121.  
  122. if (!init) {
  123. init = true;
  124. init_bo_inf();
  125. }
  126.  
  127. if (bo_sup[hit[0]][hit[1]] == 9) {
  128. return true;
  129. }
  130.  
  131. return false;
  132. }
  133.  
  134. vecteurI ask_hit()
  135. {
  136. int x,y;
  137. do {
  138. do {
  139. std::cout << "Entrez votre coup sur l axe X (haut-bas) [1-" << sizeH << "] : ";
  140. cin >> x;
  141. x--;
  142. } while (x < 0 || x > bo_inf.size() -1);
  143. do {
  144. std::cout << "Entrez votre coup sur l axe Y (gauche-droite [1-" << sizeW <<"] : ";
  145. cin >> y;
  146. y--;
  147. } while (y < 0 || y > bo_inf[x].size() -1);
  148. if (bo_sup[x][y] == 1) {
  149. cout << "erreur, vous avez déjà joué sur cette case." << endl;
  150. }
  151. } while (bo_sup[x][y] == 1);
  152. VecteurI hit{x,y};
  153. return hit;
  154. }
  155.  
  156. MatriceI init_bo_sup(int sizeH, int sizeW)
  157. {
  158.  
  159.  
  160. for (int i = 0; i < bo_sup.size(); i++) {
  161. for (int j = 0; j < bo_sup[i].size(); j++) {
  162. tab_sup[i][j] = 0;
  163. }
  164. }
  165.  
  166. return bo_sup;
  167. }
  168.  
  169. int main()
  170. {
  171. if (!init) {
  172. VecteurI hit = ask_hit();
  173. play_hit(hit);
  174. if (play_hit) {
  175. loose;
  176. }
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement