Advertisement
Proppsicle

Minesweeper 4

Dec 6th, 2019
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1.  
  2. //global constants and variables
  3. final int NUM_MINES = 10;
  4. final int CELLSIZE = 50;
  5. final int NUM_ROWS = 10;
  6. final int NUM_COLS = 12;
  7. int [] mineX = new int[NUM_ROWS];
  8. int [] mineY = new int[NUM_COLS];
  9. boolean gameOn = true;
  10. int xCoordinate;
  11. int yCoordinate;
  12. int[] numX = new int[NUM_ROWS*NUM_COLS];
  13. int[] numY = new int[NUM_ROWS*NUM_COLS];
  14. char[] numValue = new char[NUM_ROWS*NUM_COLS];
  15. int numCount = 0;
  16. String endGameMessage = "";
  17.  
  18. void setup() {
  19. size(600, 500);
  20. generateMines(NUM_MINES); // generates the mines
  21. }
  22.  
  23. void draw() {
  24. drawGrid(); // draws the grid
  25. drawMines(mineX, mineY, NUM_MINES); //fill the cells with mines
  26. drawNums(numX, numY, numValue, (NUM_ROWS*NUM_COLS)); // draws the numbers
  27.  
  28. //checks if the player has won or not and displays a message
  29. if (!gameOn) {
  30. checkIfPlayerWon();
  31. gameLost();
  32. }
  33. }
  34.  
  35. //draws a grid
  36. void drawGrid() {
  37. for (int i=0; i<NUM_COLS; i++) {
  38. for (int j=0; j<NUM_ROWS; j++) {
  39. int rectX = i*CELLSIZE;
  40. int rectY = j*CELLSIZE;
  41. fill(167);
  42. rect(rectX, rectY, CELLSIZE, CELLSIZE);
  43. }
  44. }
  45. }
  46.  
  47. //takes in the mine arrays and calls on my drawMines function
  48. void drawMines(int[] x, int[] y, int num) {
  49.  
  50. for (int i=0; i<num; i++) {
  51.  
  52. int posX = y[i];
  53. int posY = x[i];
  54.  
  55. drawMine(posX, posY);
  56. }
  57. }
  58.  
  59. //takes in the num arrays and calls on my drawNum function
  60. void drawNums(int[] x, int[] y, char[] val, int num) {
  61. for (int i=0; i<num; i++) {
  62. int posX = y[i];
  63. int posY = x[i];
  64. char number = val[i];
  65. drawNum(posX, posY, number);
  66. }
  67. }
  68.  
  69.  
  70. //draws the mine cells
  71. void drawMine(int x, int y) {
  72. int numX = (y%NUM_COLS) * CELLSIZE;
  73. int numY = (x%NUM_COLS) * CELLSIZE;
  74. int mineSize = 40;
  75. strokeWeight(1);
  76. fill(255, 0, 0);
  77. rect(numX, numY, CELLSIZE, CELLSIZE);
  78. fill(255, 255, 0);
  79. ellipse(numX+CELLSIZE/2, numY+CELLSIZE/2, mineSize, mineSize);
  80. stroke(0);
  81. strokeWeight(2);
  82. line(numX+CELLSIZE/4, numY+CELLSIZE/4, numX+3*CELLSIZE/4, numY+3*CELLSIZE/4);
  83. line(numX+3*CELLSIZE/4, numY+CELLSIZE/4, numX+CELLSIZE/4, numY+3*CELLSIZE/4);
  84. //println(numX, numY);
  85. }
  86.  
  87. //draws the numbered Cells
  88. void drawNum(int x, int y, char num) {
  89. int numX = (y%NUM_COLS) * CELLSIZE;
  90. int numY = (x%NUM_COLS) * CELLSIZE;
  91. strokeWeight(1);
  92. fill(255);
  93. rect(numX, numY, CELLSIZE, CELLSIZE);
  94. textSize(40);
  95. textAlign(CENTER, CENTER);
  96. fill(0, 0, 255);
  97. text(num, numX + CELLSIZE/2, numY + CELLSIZE/2);
  98. }
  99.  
  100. //generates a certain amount of random mines
  101. void generateMines(int numberOfMines) {
  102. boolean continu = true;
  103. for ( int i=0; i<numberOfMines; i++) {
  104. continu = true;
  105. do {
  106. int newX = int(random(NUM_COLS));
  107. int newY = int(random(NUM_ROWS));
  108. if (search(mineX, mineY, i, newX, newY) == false) {
  109. insert(mineX, mineY, i, newX, newY);
  110. continu = false;
  111. println(newX, newY);
  112. }
  113. } while (continu);
  114. }
  115. }
  116.  
  117. //searches and tests for an empty spot to place a mine
  118. boolean search(int[] x, int[] y, int n, int col, int row) {
  119. boolean mines = false;
  120. for (int i=0; i<n; i++) {
  121. if (x[i] == col && y[i] == row) {
  122. mines = true;
  123. } else {
  124. mines = false;
  125. }
  126. }
  127. return mines;
  128. }
  129.  
  130. //inserts the mine into an empty location
  131. int insert(int[] x, int[]y, int n, int col, int row) {
  132. if (x.length <= n || y.length <= n) {
  133. return 0;
  134. } else {
  135. x[n] = col;
  136. y[n] = row;
  137. return n+1;
  138. }
  139. }
  140.  
  141. //checks on the mouse location and returns the row cell its in
  142. int getRowIndexFromMouseClick() {
  143. xCoordinate = mouseX/CELLSIZE;
  144. return xCoordinate;
  145. }
  146.  
  147. //checks on the mouse location and returns the column cell its in
  148. int getColumnIndexFromMouseClick() {
  149. yCoordinate = mouseY/CELLSIZE;
  150. return yCoordinate;
  151. }
  152.  
  153. //inserts a number into a guessed cell
  154. void insertNumber(int xCoordinate, int yCoordinate) {
  155. numX[numCount] = xCoordinate;
  156. numY[numCount] = yCoordinate;
  157. numValue[numCount] = countNeighbourMines(xCoordinate, yCoordinate);
  158. numCount += 1;
  159. }
  160.  
  161. //checks the neighbouring cells for mines
  162. char countNeighbourMines(int xCoordinate, int yCoordinate) {
  163. int mineCount = 0;
  164. char mineCountDigit;
  165. for (int i=0; i <= NUM_ROWS; i++) {
  166. if (mineX[i] == xCoordinate && mineY[i] == yCoordinate) {
  167. mineCount +=1;
  168. }
  169. }
  170. mineCountDigit = (char)mineCount;
  171. return mineCountDigit;
  172. }
  173.  
  174. //checks if the player won the game
  175. void checkIfPlayerWon() {
  176. if (numCount >= (NUM_ROWS*NUM_COLS) - NUM_MINES) {
  177. endGameMessage = "WINNER!";
  178. gameOn = false;
  179. }
  180. }
  181.  
  182. //checks in the player lost the game
  183. void gameLost() {
  184. endGameMessage = "LOSER!";
  185. gameOn = false;
  186. }
  187.  
  188. //mouse clicked function for making guesses by the player
  189. void mouseClicked() {
  190. if (gameOn) {
  191. xCoordinate = getRowIndexFromMouseClick();
  192. yCoordinate = getColumnIndexFromMouseClick();
  193. if (search(mineX, mineY, NUM_MINES, xCoordinate, yCoordinate)) {
  194. gameLost();
  195. } else if (!search(mineX, mineY, NUM_MINES, xCoordinate, yCoordinate)) {
  196. insertNumber(xCoordinate, yCoordinate);
  197. checkIfPlayerWon();
  198. }
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement