Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.ArrayList;
  3.  
  4. public class MineBoard {
  5. private int[][] board;
  6. private String[][] boardh;
  7. private int mines;
  8.  
  9. //constructor
  10. //uses int to determine size and mines
  11. public MineBoard (int a) {
  12. if (a == 2) {
  13. board = new int[18][18];
  14. mines = 40;
  15. } else if (a == 3) {
  16. board = new int[26][26];
  17. mines = 99;
  18. } else {
  19. board = new int[10][10];
  20. mines = 10;
  21. }
  22. boardh = new String[board.length-2][board.length-2];
  23. for (int i = 0; i < board.length; i++) {
  24. board[0][i] = -1;
  25. board[9][i] = -1;
  26. board[i][9] = -1;
  27. board[i][0] = -1;
  28. }
  29. for (int i = 0; i < boardh.length; i++) {
  30. for (int j = 0; j < boardh.length; j++) {
  31. boardh[i][j] = " - ";
  32. }
  33. }
  34. }
  35. //fills board later so that the user cannot lose on their first turn
  36. public void fillBoard(int a, int b) {
  37. Random rand = new Random();
  38. int c,d;
  39. a++;
  40. b++;
  41. for (int i = 0; i < mines; i++) {
  42. c = rand.nextInt(board.length-2) + 1;
  43. d = rand.nextInt(board.length-2) + 1;
  44. if (((c >= a-1 && c <= a+1) && (d >= b-1 && d <= b+1)) || ((c >= b-1 && c <= b+1) && (d >= a-1 && d <= a+1))) {
  45. i--;
  46. } else if (board[c][d] == 10) {
  47. i--;
  48. } else {
  49. board[c][d] = 10;
  50. }
  51. }
  52. for (int i = 1; i < board.length-1; i++) {
  53. for (int j = 1; j < board.length-1; j++) {
  54. board[i][j] = checkBorder(i,j);
  55. }
  56. }
  57. }
  58. // helper to remove spam in the double for loop in above method
  59. public int checkBorder (int i, int j) {
  60. int sum = 0;
  61. if (board[i][j] == 10) {
  62. return 10;
  63. }
  64. for (int f = i-1; f < i + 2; f++) {
  65. for (int g = j-1; g < j + 2; g++) {
  66. if (board[f][g] == 10) {
  67. sum++;
  68. }
  69. }
  70. }
  71. return sum;
  72. }
  73. public void zeroBorder (int i, int j) {
  74. if (i <= 0 || j <= 0) {
  75. return;
  76. }
  77. if (i > boardh.length || j > boardh.length) {
  78. return;
  79. }
  80. if (!(boardh[i-1][j-1]).equals(" - ")) {
  81. return;
  82. }
  83. boardh[i-1][j-1] = " " + board[i][j] + " ";
  84. if (board[i][j] != 0) {
  85. return;
  86. }
  87. zeroBorder(i+1,j);
  88. zeroBorder(i-1,j);
  89. zeroBorder(i,j+1);
  90. zeroBorder(i,j-1);
  91. }
  92. public boolean uncover (int i,int j) {
  93. if (i < 0 || j < 0) {
  94. return true;
  95. }
  96. if (i > boardh.length || j > boardh.length) {
  97. return true;
  98. }
  99. if (!boardh[i][j].equals(" - ")) {
  100. return true;
  101. }
  102. if ((board[i+1][j+1]) == 10) {
  103. boardh[i][j] = " 10 ";
  104. return false;
  105. }
  106. if (board[i+1][j+1] == 0) {
  107. zeroBorder(i+1,j+1);
  108. return true;
  109. }
  110. boardh[i][j] = " " + board[i+1][j+1] + " ";
  111. return true;
  112. }
  113. public boolean win () {
  114. int count = 0;
  115. for (int i = 0; i < boardh.length; i++) {
  116. for (int j = 0; j < boardh.length; j++) {
  117. if (!boardh[i][j].equals(" - ")) {
  118. count++;
  119. }
  120. }
  121. }
  122. int difference = (boardh.length * boardh.length) - count;
  123. if (difference == mines) {
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. }
  129. public String toString() {
  130. String a = "";
  131. for (int i = 0; i < boardh.length; i++) {
  132. for (int j = 0; j < boardh.length; j++) {
  133. a += boardh[i][j];
  134. }
  135. a += "\n";
  136. }
  137. return a;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement