Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. package cs2410.assn8.controller;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5.  
  6. import static java.lang.Boolean.FALSE;
  7. import static java.lang.Boolean.TRUE;
  8.  
  9. import cs2410.assn8.view.ScoreBoard;
  10. import cs2410.assn8.view.Cell;
  11.  
  12. public class MineLogic {
  13.  
  14. private ScoreBoard scoreBoard;
  15. private int mineCount = 300;
  16. private int gameState = 0;
  17. // 1 - New game in progress
  18. // 2 -
  19. // 3 -
  20. private Cell[][] cellRefArray;
  21. private int[][] mineField;
  22. private ArrayList<Integer> randMines;
  23.  
  24.  
  25. public MineLogic(ScoreBoard board) {
  26. scoreBoard = board;
  27. cellRefArray = new Cell[20][20];
  28. mineField = new int[20][20];
  29. }
  30.  
  31. public void updateRef(Cell[][] ref) {
  32. cellRefArray = ref;
  33. }
  34.  
  35. public void startGame() {
  36. shuffleBoard();
  37. scoreBoard.disableStartButton(true);
  38. mineCount = 300;
  39. gameState = 1;
  40. cheater();
  41. }
  42.  
  43. public void reveal(int x, int y) {
  44.  
  45. //System.out.println("FROM BUTTON --- x: " + x + "y: " + y);
  46. revealNext(x, y + 1);
  47. revealNext(x + 1, y + 1);
  48. revealNext(x + 1, y);
  49. revealNext(x + 1, y - 1);
  50. revealNext(x, y - 1);
  51. revealNext(x - 1, y - 1);
  52. revealNext(x - 1, y);
  53. revealNext(x - 1, y + 1);
  54. }
  55.  
  56. private void revealNext(int x, int y) {
  57. // Return if coordinates sent go outside the grid
  58. if (x < 1 || x > 20 || y < 1 || y > 20) {
  59. return;
  60. }
  61.  
  62. // Return if button has already been clicked
  63. if (cellRefArray[x][y].isDisabled()) {
  64. return;
  65. }
  66.  
  67. // Use recursion if the next cell should be blank
  68.  
  69. // Reveal adjacent mines if the next cell should not be blank
  70. }
  71.  
  72. private void gameOver() {
  73.  
  74. }
  75.  
  76. private void gameReset() {
  77.  
  78. }
  79.  
  80. private void shuffleBoard() {
  81.  
  82. randMines = new ArrayList<>();
  83. for (int i = 0; i < 100; i++) {
  84. randMines.add(-1);
  85. }
  86.  
  87. for (int i = 0; i < 300; i++) {
  88. randMines.add(0);
  89. }
  90. Collections.shuffle(randMines);
  91.  
  92. int index = -1;
  93. for (int r = 0; r < 20; r++) {
  94. for (int c = 0; c < 20; c++) {
  95. index++;
  96. mineField[r][c] = randMines.get(index);
  97. }
  98. }
  99.  
  100. /*
  101. for (int r = 0; r < 20; r++) {
  102. System.out.println();
  103. for (int c = 0; c < 20; c++) {
  104. System.out.print(mineField[r][c] + "\t");
  105. }
  106. }
  107. */
  108.  
  109. for (int r = 0; r < 20; r++) {
  110. for (int c = 0; c < 20; c++) {
  111.  
  112. if (mineField[r][c] != -1) {
  113. // NORTH
  114. if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
  115. if (mineField[r + 1][c] == -1) {
  116. mineField[r][c]++;
  117. }
  118. }
  119.  
  120. // NORTHEAST
  121. if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
  122. if (mineField[r + 1][c + 1] == -1) {
  123. mineField[r][c]++;
  124. }
  125. }
  126.  
  127. // EAST
  128. if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
  129. if (mineField[r][c + 1] == -1) {
  130. mineField[r][c]++;
  131. }
  132. }
  133.  
  134. // SOUTHEAST
  135. if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
  136. if (mineField[r - 1][c - 1] == -1) {
  137. mineField[r][c]++;
  138. }
  139. }
  140. // SOUTH
  141. if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
  142. if (mineField[r - 1][c] == -1) {
  143. mineField[r][c]++;
  144. }
  145. }
  146.  
  147. // SOUTHWEST
  148. if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
  149. if (mineField[r - 1][c - 1] == -1) {
  150. mineField[r][c]++;
  151. }
  152. }
  153.  
  154. // WEST
  155. if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
  156. if (mineField[r + 1][c] == -1) {
  157. mineField[r][c - 1]++;
  158. }
  159. }
  160.  
  161. //NORTHWEST
  162. if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
  163. if (mineField[r + 1][c - 1] == -1) {
  164. mineField[r][c]++;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171.  
  172. public void decreaseBombs () {
  173. scoreBoard.setTextBombs(--mineCount);
  174. }
  175.  
  176. public void increaseBombs () {
  177. scoreBoard.setTextBombs(++mineCount);
  178. }
  179.  
  180. public void cheater() {
  181. for (int r = 0; r < 20; r++) {
  182. for (int c = 0; c < 20; c++) {
  183. if (mineField[r][c] == -1) {
  184. cellRefArray[r][c].getStyleClass().clear();
  185. cellRefArray[r][c].getStyleClass().add("button-tile-red");
  186. }
  187.  
  188. else {
  189. cellRefArray[r][c].setText(Integer.toString(mineField[r][c]));
  190. }
  191. }
  192. }
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement