Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class RabotenPlot {
  5.  
  6. public static void main(String[] arg) {
  7. Scanner input = new Scanner(System.in);
  8.  
  9. System.out.print("How wide should it be:");
  10. int rangeX = input.nextInt();
  11. System.out.print("How high should it be:");
  12. int rangeY = input.nextInt();
  13.  
  14. int[][] map = new int[rangeY + 2][rangeX + 2];
  15. int[][] cover = new int[rangeY + 2][rangeX + 2];
  16.  
  17. int countMines = Math.round(rangeX * rangeY / 4);
  18. boolean boom = false;
  19. int counterMarks = 0;
  20. int markedMines = 0;
  21.  
  22. System.out.println("There are " + countMines + " which are mines(1/4 of the field)");
  23. map = initializeMap(countMines, map);
  24.  
  25. //for map
  26. //-2 = border
  27. //-1 = mine
  28. //0 = empty
  29. //number....
  30.  
  31. //for cover
  32. //-1=marked
  33. //0=hidden
  34. //1=open
  35.  
  36.  
  37. for (int i = 0; i < cover.length; i++) {
  38. for (int j = 0; j < cover[0].length; j++) {
  39. cover[i][j] = 0;
  40. }
  41. }
  42.  
  43. while (!boom) {
  44. showMap(cover, map);
  45. System.out.println("You have " + (countMines + 1) + " total marks");
  46. System.out.println("You have " + ((countMines + 1) - counterMarks) + " marks left");
  47. System.out.print("What row is the cell in?");
  48. int x = input.nextInt();
  49. System.out.print("What colomn is the cell in?");
  50. int y = input.nextInt();
  51. System.out.println("What to do with it?");
  52. System.out.println("[1]Open");
  53. System.out.println("[2]Mark");
  54. System.out.println("[3]End game");
  55. int action = input.nextInt();
  56. if (action == 1) {
  57. cover = openCell(x, y, map, cover);
  58. } else if (action == 2) {
  59. if (cover[x][y] != 1) {
  60. if (map[x][y] != -2) {
  61. if (counterMarks < countMines + 1) {
  62. if (cover[x][y] == -1) {
  63. cover[x][y] = 0;
  64. counterMarks--;
  65. if (map[x][y] == -1) {
  66. markedMines--;
  67. }
  68. } else {
  69. cover[x][y] = -1;
  70. if (map[x][y] == -1) {
  71. markedMines++;
  72. }
  73. counterMarks++;
  74. }
  75. } else {
  76. System.out.println("You don't have marks left!");
  77. }
  78. } else {
  79. System.out.println("You can't mark a border");
  80. }
  81. } else {
  82. System.out.println("You can't mark it if it's open");
  83. }
  84. } else if (action == 3) {
  85. System.out.println("The game is ending");
  86. for (int i = 0; i < cover.length; i++) {
  87. for (int j = 0; j < cover[0].length; j++) {
  88. cover[i][j] = 1;
  89. }
  90. }
  91. } else {
  92. System.out.println("No such function");
  93. }
  94. if (markedMines == countMines) {
  95. System.out.println("YOu marked all the mines!!");
  96. boom = true;
  97. }
  98. if (cover[x][y] == 1 && map[x][y] == -1) {
  99. boom = true;
  100. }
  101. }
  102. showMap(cover, map);
  103. }
  104.  
  105. public static int[][] initializeMap(int howManyMines, int[][] emptyMap) {
  106. Random random = new Random();
  107.  
  108. for (int i = 0; i < emptyMap.length; i++) {
  109. emptyMap[i][0] = -2;
  110. emptyMap[i][emptyMap[0].length - 1] = -2;
  111. }
  112. for (int i = 0; i < emptyMap[0].length; i++) {
  113. emptyMap[0][i] = -2;
  114. emptyMap[emptyMap.length - 1][i] = -2;
  115. }
  116. for (int i = 0; i <= howManyMines; i++) {
  117. int randX = random.nextInt(emptyMap.length);
  118. int randY = random.nextInt(emptyMap[0].length);
  119. while (emptyMap[randX][randY] == -1 || emptyMap[randX][randY] == -2) {
  120. randX = random.nextInt(emptyMap.length);
  121. randY = random.nextInt(emptyMap[0].length);
  122. }
  123. emptyMap[randX][randY] = -1;
  124. }
  125. for (int i = 1; i < emptyMap.length - 1; i++) {
  126. for (int j = 1; j < emptyMap[0].length - 1; j++) {
  127. if (emptyMap[i][j] != -1)
  128. emptyMap[i][j] = minesAround(i, j, emptyMap);
  129. }
  130. }
  131.  
  132. return emptyMap;
  133. }
  134.  
  135. public static int minesAround(int x, int y, int[][] map) {
  136. int count = 0;
  137. for (int i = -1; i <= 1; i++) {
  138. for (int j = -1; j <= 1; j++) {
  139. if (map[x + i][y + j] == -1) {
  140. count++;
  141. }
  142. }
  143. }
  144. return count;
  145. }
  146.  
  147. public static void showMap(int[][] cover, int[][] map) {
  148. System.out.print(" ");
  149. for (int i = 0; i < map[0].length; i++) {
  150. if (i < 10) {
  151. System.out.print(" " + i + "");
  152. } else {
  153. System.out.print(i + " ");
  154. }
  155. }
  156. System.out.println();
  157. for (int i = 0; i < map.length; i++) {
  158. if (i < 10 && i >= 0) {
  159. System.out.print(" " + i);
  160. }
  161. if (i >= 10 && i < 100) {
  162. System.out.print(i);
  163. }
  164. for (int j = 0; j < map[0].length; j++) {
  165. if (map[i][j] == -2) {
  166. if (i < 10 && j == 0) {
  167. System.out.print(" ");
  168. } else if (j == 0) {
  169. System.out.print(" ");
  170. } else {
  171. System.out.print(" ");
  172. }
  173. } else if (cover[i][j] == 1) {
  174. if (map[i][j] == -1) {
  175. System.out.print("[X]");
  176.  
  177. } else {
  178. System.out.print("[" + minesAround(i, j, map) + "]");
  179. }
  180. } else if (cover[i][j] == 0) {
  181. System.out.print("[ ]");
  182. } else {
  183. System.out.print("[M]");
  184. }
  185. }
  186. System.out.println();
  187. }
  188. }
  189.  
  190. public static int[][] openEmptyAround(int x, int y, int[][] map, int[][] cover) {
  191. for (int i = -1; i <= 1; i++) {
  192. for (int j = -1; j <= 1; j++) {
  193. if (map[x + i][y + j] == 0 && cover[x + i][y + j] == 0) {
  194. cover[x + i][y + j] = 1;
  195. cover = openEmptyAround(x + i, y + j, map, cover);
  196. } else {
  197. continue;
  198. }
  199. }
  200. }
  201. return cover;
  202. }
  203.  
  204. public static int[][] openCell(int x, int y, int[][] map, int[][] cover) {
  205. int[][] newCover = cover;
  206.  
  207. if (map[x][y] == -2) {
  208. System.out.println("You can't open a border");
  209. } else if (map[x][y] == -1) {
  210. newCover[x][y] = 1;
  211. System.out.println("You opened a mine!!!");
  212. } else if (cover[x][y] == -1) {
  213. System.out.println("You can't open a marked cell. Unmark it first.");
  214. } else {
  215. cover[x][y] = 1;
  216. System.out.println("Successfully opened");
  217. if (map[x][y] == 0) {
  218. cover = openEmptyAround(x, y, map, cover);
  219. }
  220. }
  221. return newCover;
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement