Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. package com.zetcode;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7.  
  8. import java.util.Random;
  9.  
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13.  
  14. public class Board extends JPanel {
  15.  
  16. private final int NUM_IMAGES = 13;
  17. private final int CELL_SIZE = 15;
  18.  
  19. private final int COVER_FOR_CELL = 10;
  20. private final int MARK_FOR_CELL = 10;
  21. private final int EMPTY_CELL = 0;
  22. private final int MINE_CELL = 9;
  23. private final int COVERED_MINE_CELL = MINE_CELL + COVER_FOR_CELL;
  24. private final int MARKED_MINE_CELL = COVERED_MINE_CELL + MARK_FOR_CELL;
  25.  
  26. private final int DRAW_MINE = 9;
  27. private final int DRAW_COVER = 10;
  28. private final int DRAW_MARK = 11;
  29. private final int DRAW_WRONG_MARK = 12;
  30.  
  31. private final int N_MINES = 40;
  32. private final int N_ROWS = 16;
  33. private final int N_COLS = 16;
  34.  
  35. private int[] field;
  36. private boolean inGame;
  37. private int mines_left;
  38. private Image[] img;
  39.  
  40. private int all_cells;
  41. private JLabel statusbar;
  42.  
  43.  
  44. public Board(JLabel statusbar) {
  45.  
  46. this.statusbar = statusbar;
  47.  
  48. img = new Image[NUM_IMAGES];
  49.  
  50. for (int i = 0; i < NUM_IMAGES; i++) {
  51. img[i] = (new ImageIcon(i + ".png")).getImage();
  52. }
  53.  
  54. setDoubleBuffered(true);
  55.  
  56. addMouseListener(new MinesAdapter());
  57. newGame();
  58. }
  59.  
  60.  
  61. private void newGame() {
  62.  
  63. Random random;
  64. int current_col;
  65.  
  66. int i = 0;
  67. int position = 0;
  68. int cell = 0;
  69.  
  70. random = new Random();
  71. inGame = true;
  72. mines_left = N_MINES;
  73.  
  74. all_cells = N_ROWS * N_COLS;
  75. field = new int[all_cells];
  76.  
  77. for (i = 0; i < all_cells; i++)
  78. field[i] = COVER_FOR_CELL;
  79.  
  80. statusbar.setText(Integer.toString(mines_left));
  81.  
  82.  
  83. i = 0;
  84. while (i < N_MINES) {
  85.  
  86. position = (int) (all_cells * random.nextDouble());
  87.  
  88. if ((position < all_cells) &&
  89. (field[position] != COVERED_MINE_CELL)) {
  90.  
  91.  
  92. current_col = position % N_COLS;
  93. field[position] = COVERED_MINE_CELL;
  94. i++;
  95.  
  96. if (current_col > 0) {
  97. cell = position - 1 - N_COLS;
  98. if (cell >= 0)
  99. if (field[cell] != COVERED_MINE_CELL)
  100. field[cell] += 1;
  101. cell = position - 1;
  102. if (cell >= 0)
  103. if (field[cell] != COVERED_MINE_CELL)
  104. field[cell] += 1;
  105.  
  106. cell = position + N_COLS - 1;
  107. if (cell < all_cells)
  108. if (field[cell] != COVERED_MINE_CELL)
  109. field[cell] += 1;
  110. }
  111.  
  112. cell = position - N_COLS;
  113. if (cell >= 0)
  114. if (field[cell] != COVERED_MINE_CELL)
  115. field[cell] += 1;
  116. cell = position + N_COLS;
  117. if (cell < all_cells)
  118. if (field[cell] != COVERED_MINE_CELL)
  119. field[cell] += 1;
  120.  
  121. if (current_col < (N_COLS - 1)) {
  122. cell = position - N_COLS + 1;
  123. if (cell >= 0)
  124. if (field[cell] != COVERED_MINE_CELL)
  125. field[cell] += 1;
  126. cell = position + N_COLS + 1;
  127. if (cell < all_cells)
  128. if (field[cell] != COVERED_MINE_CELL)
  129. field[cell] += 1;
  130. cell = position + 1;
  131. if (cell < all_cells)
  132. if (field[cell] != COVERED_MINE_CELL)
  133. field[cell] += 1;
  134. }
  135. }
  136. }
  137. }
  138.  
  139.  
  140. public void find_empty_cells(int j) {
  141.  
  142. int current_col = j % N_COLS;
  143. int cell;
  144.  
  145. if (current_col > 0) {
  146. cell = j - N_COLS - 1;
  147. if (cell >= 0)
  148. if (field[cell] > MINE_CELL) {
  149. field[cell] -= COVER_FOR_CELL;
  150. if (field[cell] == EMPTY_CELL)
  151. find_empty_cells(cell);
  152. }
  153.  
  154. cell = j - 1;
  155. if (cell >= 0)
  156. if (field[cell] > MINE_CELL) {
  157. field[cell] -= COVER_FOR_CELL;
  158. if (field[cell] == EMPTY_CELL)
  159. find_empty_cells(cell);
  160. }
  161.  
  162. cell = j + N_COLS - 1;
  163. if (cell < all_cells)
  164. if (field[cell] > MINE_CELL) {
  165. field[cell] -= COVER_FOR_CELL;
  166. if (field[cell] == EMPTY_CELL)
  167. find_empty_cells(cell);
  168. }
  169. }
  170.  
  171. cell = j - N_COLS;
  172. if (cell >= 0)
  173. if (field[cell] > MINE_CELL) {
  174. field[cell] -= COVER_FOR_CELL;
  175. if (field[cell] == EMPTY_CELL)
  176. find_empty_cells(cell);
  177. }
  178.  
  179. cell = j + N_COLS;
  180. if (cell < all_cells)
  181. if (field[cell] > MINE_CELL) {
  182. field[cell] -= COVER_FOR_CELL;
  183. if (field[cell] == EMPTY_CELL)
  184. find_empty_cells(cell);
  185. }
  186.  
  187. if (current_col < (N_COLS - 1)) {
  188. cell = j - N_COLS + 1;
  189. if (cell >= 0)
  190. if (field[cell] > MINE_CELL) {
  191. field[cell] -= COVER_FOR_CELL;
  192. if (field[cell] == EMPTY_CELL)
  193. find_empty_cells(cell);
  194. }
  195.  
  196. cell = j + N_COLS + 1;
  197. if (cell < all_cells)
  198. if (field[cell] > MINE_CELL) {
  199. field[cell] -= COVER_FOR_CELL;
  200. if (field[cell] == EMPTY_CELL)
  201. find_empty_cells(cell);
  202. }
  203.  
  204. cell = j + 1;
  205. if (cell < all_cells)
  206. if (field[cell] > MINE_CELL) {
  207. field[cell] -= COVER_FOR_CELL;
  208. if (field[cell] == EMPTY_CELL)
  209. find_empty_cells(cell);
  210. }
  211. }
  212. }
  213.  
  214. @Override
  215. public void paintComponent(Graphics g) {
  216.  
  217. int cell = 0;
  218. int uncover = 0;
  219.  
  220. for (int i = 0; i < N_ROWS; i++) {
  221. for (int j = 0; j < N_COLS; j++) {
  222.  
  223. cell = field[(i * N_COLS) + j];
  224.  
  225. if (inGame && cell == MINE_CELL)
  226. inGame = false;
  227.  
  228. if (!inGame) {
  229. if (cell == COVERED_MINE_CELL) {
  230. cell = DRAW_MINE;
  231. } else if (cell == MARKED_MINE_CELL) {
  232. cell = DRAW_MARK;
  233. } else if (cell > COVERED_MINE_CELL) {
  234. cell = DRAW_WRONG_MARK;
  235. } else if (cell > MINE_CELL) {
  236. cell = DRAW_COVER;
  237. }
  238.  
  239.  
  240. } else {
  241. if (cell > COVERED_MINE_CELL)
  242. cell = DRAW_MARK;
  243. else if (cell > MINE_CELL) {
  244. cell = DRAW_COVER;
  245. uncover++;
  246. }
  247. }
  248.  
  249. g.drawImage(img[cell], (j * CELL_SIZE),
  250. (i * CELL_SIZE), this);
  251. }
  252. }
  253.  
  254. if (uncover == 0 && inGame) {
  255. inGame = false;
  256. statusbar.setText("Game won");
  257. } else if (!inGame)
  258. statusbar.setText("Game lost");
  259. }
  260.  
  261.  
  262. class MinesAdapter extends MouseAdapter {
  263.  
  264. @Override
  265. public void mousePressed(MouseEvent e) {
  266.  
  267. int x = e.getX();
  268. int y = e.getY();
  269.  
  270. int cCol = x / CELL_SIZE;
  271. int cRow = y / CELL_SIZE;
  272.  
  273. boolean rep = false;
  274.  
  275.  
  276. if (!inGame) {
  277. newGame();
  278. repaint();
  279. }
  280.  
  281.  
  282. if ((x < N_COLS * CELL_SIZE) && (y < N_ROWS * CELL_SIZE)) {
  283.  
  284. if (e.getButton() == MouseEvent.BUTTON3) {
  285.  
  286. if (field[(cRow * N_COLS) + cCol] > MINE_CELL) {
  287. rep = true;
  288.  
  289. if (field[(cRow * N_COLS) + cCol] <= COVERED_MINE_CELL) {
  290. if (mines_left > 0) {
  291. field[(cRow * N_COLS) + cCol] += MARK_FOR_CELL;
  292. mines_left--;
  293. statusbar.setText(Integer.toString(mines_left));
  294. } else
  295. statusbar.setText("No marks left");
  296. } else {
  297.  
  298. field[(cRow * N_COLS) + cCol] -= MARK_FOR_CELL;
  299. mines_left++;
  300. statusbar.setText(Integer.toString(mines_left));
  301. }
  302. }
  303.  
  304. } else {
  305.  
  306. if (field[(cRow * N_COLS) + cCol] > COVERED_MINE_CELL) {
  307. return;
  308. }
  309.  
  310. if ((field[(cRow * N_COLS) + cCol] > MINE_CELL) &&
  311. (field[(cRow * N_COLS) + cCol] < MARKED_MINE_CELL)) {
  312.  
  313. field[(cRow * N_COLS) + cCol] -= COVER_FOR_CELL;
  314. rep = true;
  315.  
  316. if (field[(cRow * N_COLS) + cCol] == MINE_CELL)
  317. inGame = false;
  318. if (field[(cRow * N_COLS) + cCol] == EMPTY_CELL)
  319. find_empty_cells((cRow * N_COLS) + cCol);
  320. }
  321. }
  322.  
  323. if (rep)
  324. repaint();
  325.  
  326. }
  327. }
  328. }
  329. }
  330.  
  331. package com.zetcode;
  332.  
  333. import java.awt.BorderLayout;
  334. import javax.swing.JFrame;
  335. import javax.swing.JLabel;
  336. import javax.swing.SwingUtilities;
  337.  
  338.  
  339. public class Mines extends JFrame {
  340.  
  341. private final int FRAME_WIDTH = 250;
  342. private final int FRAME_HEIGHT = 290;
  343.  
  344. private final JLabel statusbar;
  345.  
  346. public Mines() {
  347.  
  348. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  349. setSize(FRAME_WIDTH, FRAME_HEIGHT);
  350. setLocationRelativeTo(null);
  351. setTitle("Minesweeper");
  352.  
  353. statusbar = new JLabel("");
  354. add(statusbar, BorderLayout.SOUTH);
  355.  
  356. add(new Board(statusbar));
  357.  
  358. setResizable(false);
  359. }
  360.  
  361. public static void main(String[] args) {
  362. SwingUtilities.invokeLater(new Runnable() {
  363.  
  364. @Override
  365. public void run() {
  366. JFrame ex = new Mines();
  367. ex.setVisible(true);
  368. }
  369. });
  370. }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement