Minesweeper is a classic video game created in 1990’s., The goal of this game is to find all the squares which dont have mines underneath.
The game is designed with the logic of each squares or cells have following rules.
Each square may be any one of the following:
- Contains a Mine which will explode and end the game, when uncovered.
- Contains an empty cell, which can be uncovered or not.
- Contains a number, which is the count of all the mines adjacent to it.
If the player clicks on the mine, the game ends.
The following is the implementation in Java of Minesweeper game, can be directly run in any version of Java greater than 1.4.
Note: Please include the jpg/png files in appropriate location as mentioned in the java code, to see your Minesweeper game working fine.
InitScreen.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; /** * Author: Karthick * tutorialflow.com */ class InitScreen implements ActionListener, MouseListener { private JFrame screen = null; private JButton smiley = new JButton(""); private JPanel composite = new JPanel(); private JPanel topPanel = new JPanel(); ImageIcon smileyImageIcon = null; ImageIcon tImageIcon = null; ImageIcon pitImageIcon = null; ImageIcon lossImageIcon = null; ImageIcon cryImageIcon = null; ImageIcon oneImageIcon = null; ImageIcon twoImageIcon = null; ImageIcon threeImageIcon = null; ImageIcon fourImageIcon = null; ImageIcon fiveImageIcon = null; public InitScreen() { screen = new JFrame("MineSweeper by Karthick, TutorialFlow.com"); screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); screen.setVisible(true); screen.setResizable(false); screen.setJMenuBar(createMenuBar()); loadMinesweeperImages(); composite.setLayout(new BorderLayout()); smiley.setPreferredSize(new Dimension(25, 25)); smiley.setIcon(smileyImageIcon); topPanel.add(smiley); composite.add(topPanel, BorderLayout.NORTH); smiley.addActionListener(this); smiley.addMouseListener(this); arrangeButtons(); screen.add(composite); screen.pack(); } public void loadMinesweeperImages() { smileyImageIcon = getScaledImage("C:\\Patches\\images\\smiley.png"); tImageIcon = getScaledImage("C:\\images\\t.png"); pitImageIcon = getScaledImage("C:\\images\\pit.png"); lossImageIcon = getScaledImage("C:\\images\\loss.png"); cryImageIcon = getScaledImage("C:\\images\\cry.png"); oneImageIcon = getScaledImage("C:\\images\\1.png"); twoImageIcon = getScaledImage("C:\\images\\2.png"); threeImageIcon = getScaledImage("C:\\images\\3.png"); fourImageIcon = getScaledImage("C:\\images\\4.png"); fiveImageIcon = getScaledImage("C:\\images\\5.png"); } public JMenuBar createMenuBar() { JMenuBar mBar = new JMenuBar(); JMenu game = new JMenu("Game"); JMenu help = new JMenu("Help"); final JMenuItem miNew = new JMenuItem("New"); final JMenuItem miBeg = new JMenuItem("Beginner"); final JMenuItem miInter = new JMenuItem("Intermediate"); final JMenuItem miExp = new JMenuItem("Expert"); final JMenuItem miExit = new JMenuItem("Exit"); final JMenuItem about = new JMenuItem("About MineSweeper...."); game.add(miNew); game.add(miBeg); game.add(miInter); game.add(miExp); game.add(miExit); help.add(about); ActionListener MENULSTNR = new ActionListener() { public void actionPerformed(ActionEvent ae) { if (miNew == ae.getSource()) { btnwdth = 10; btnhgt = 10; mines = 10; reset(); } if (miBeg == ae.getSource()) { btnwdth = 12; btnhgt = 12; mines = 12; reset(); } if (miInter == ae.getSource()) { btnwdth = 15; btnhgt = 15; mines = 50; reset(); } if (miExp == ae.getSource()) { btnwdth = 24; btnhgt = 30; mines = 80; reset(); } if (miExit == ae.getSource()) { if (screen != null) { screen.dispose(); } System.exit(0); } if (about == ae.getSource()) { System.out.println(" Karthick"); } } }; miNew.addActionListener(MENULSTNR); miBeg.addActionListener(MENULSTNR); miInter.addActionListener(MENULSTNR); miExp.addActionListener(MENULSTNR); miExit.addActionListener(MENULSTNR); about.addActionListener(MENULSTNR); mBar.add(game); mBar.add(help); return mBar; } private int btnwdth = 10; private int btnhgt = 10; private int mines = 8; int mineArray[][]; JButton button[][]; JPanel minespan = null; public void arrangeButtons() { mineArray = new int[btnwdth][btnhgt]; button = new JButton[btnwdth][btnhgt]; boolean starting = true; if (minespan != null) { composite.remove(minespan); minespan = null; starting = false; } minespan = new JPanel(); minespan.setLayout(new GridLayout(btnwdth, btnhgt)); for (int i = 0; i < btnwdth; i++) { for (int j = 0; j < btnhgt; j++) { mineArray[i][j] = 0; button[i][j] = new JButton(""); button[i][j].setBackground(Color.WHITE); button[i][j].setPreferredSize(new Dimension(16, 16)); button[i][j].addActionListener(this); button[i][j].addMouseListener(this); minespan.add(button[i][j]); } } minespan.setVisible(true); composite.add(minespan, BorderLayout.CENTER); if (starting) { minesFormat(button); } screen.pack(); } public void reset() { smiley.setIcon(smileyImageIcon); // btnwdth+=1; arrangeButtons(); for (int i = 0; i < btnwdth; i++) { for (int j = 0; j < btnhgt; j++) { mineArray[i][j] = 0; button[i][j].addActionListener(this); button[i][j].addMouseListener(this); button[i][j].setText(""); button[i][j].setBackground(Color.WHITE); //button[i][j].setIcon(tImageIcon); } } minesFormat(button); System.out.println(""); System.out.println(""); } public void minesFormat(JButton button[][]) { int mine[] = getRndmNos(btnwdth, btnhgt, mines); int count = 1; for (int i = 0; i < btnwdth; i++) { for (int j = 0; j < btnhgt; j++) { for (int k = 0; k < mine.length && mine[k] != 0; k++) { if (count == mine[k]) { mineArray[i][j] = 9; } } count++; } } int boxcount = 0; for (int i = 0; i < btnwdth; i++) { for (int j = 0; j < btnhgt; j++) { boxcount = 0; if (mineArray[i][j] != 9) { if (i > 0 && j > 0) { if (mineArray[i - 1][j - 1] == 9) boxcount++; } if (i > 0) { if (mineArray[i - 1][j] == 9) boxcount++; } if (i > 0 && j < btnhgt - 1) { if (mineArray[i - 1][j + 1] == 9) boxcount++; } if (i < btnwdth - 1 && j > 0) { if (mineArray[i + 1][j - 1] == 9) boxcount++; } if (i < btnwdth - 1) { if (mineArray[i + 1][j] == 9) boxcount++; } if (i < btnwdth - 1 && j < btnhgt - 1) { if (mineArray[i + 1][j + 1] == 9) boxcount++; } if (j > 0) { if (mineArray[i][j - 1] == 9) boxcount++; } if (j < btnhgt - 1) { if (mineArray[i][j + 1] == 9) boxcount++; } mineArray[i][j] = boxcount; } } } for (int i = 0; i < btnwdth; i++) { for (int j = 0; j < btnhgt; j++) { System.out.print(" " + mineArray[i][j]); } System.out.println(""); } } public int[] getRndmNos(int btnwdth, int btnhgt, int mines) { Random rand = new Random(); int rndmines[] = new int[btnwdth * btnhgt]; boolean in = false; int count = 0; while (count < mines) { int rndno = (int) ((btnwdth * btnhgt) * (rand.nextDouble())) + 1; in = false; for (int i = 0; i < count; i++) { if (rndmines[i] == rndno) { in = true; break; } } if (!in) { rndmines[count++] = rndno; } } return rndmines; } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == smiley) { reset(); } else { for (int i = 0; i < btnwdth; i++) for (int j = 0; j < btnhgt; j++) { if (button[i][j] == ae.getSource()) { if (mineArray[i][j] == 9) { for (int k = 0; k < btnwdth; k++) { for (int l = 0; l < btnhgt; l++) { if (mineArray[k][l] == 9) { button[k][l].setIcon(pitImageIcon); } button[k][l].removeActionListener(this); button[k][l].removeMouseListener(this); } } } if (mineArray[i][j] == 1) { button[i][j].setIcon(oneImageIcon); } if (mineArray[i][j] == 2) { button[i][j].setIcon(twoImageIcon); } if (mineArray[i][j] == 3) { button[i][j].setIcon(threeImageIcon); } if (mineArray[i][j] == 4) { button[i][j].setIcon(fourImageIcon); } if (mineArray[i][j] == 5) { button[i][j].setIcon(fiveImageIcon); } if (mineArray[i][j] == 0) { findAllEmpty(i, j); } } } } } public void findAllEmpty(int boxX, int boxY) { int arrX[] = new int[(btnwdth) * (btnhgt)]; int arrY[] = new int[(btnwdth) * (btnhgt)]; int cntEmpty = 0; for (int i = 0; i < ((btnwdth) * (btnhgt)); i++) { arrX[i] = -1; arrY[i] = -1; } arrX[cntEmpty] = boxX; arrY[cntEmpty] = boxY; cntEmpty++; for (int i = 0; i < cntEmpty; i++) { if (arrX[i] > 0) { int xxX = arrX[i] - 1; int yyY = arrY[i]; if (mineArray[xxX][yyY] == 0) { if (!findIn(arrX, arrY, cntEmpty, xxX, yyY)) { arrX[cntEmpty] = xxX; arrY[cntEmpty] = yyY; cntEmpty++; } } } if (arrX[i] < (btnwdth - 1)) { int xxX = arrX[i] + 1; int yyY = arrY[i]; if (mineArray[xxX][yyY] == 0) { if (!findIn(arrX, arrY, cntEmpty, xxX, yyY)) { arrX[cntEmpty] = xxX; arrY[cntEmpty] = yyY; cntEmpty++; } } } if (arrY[i] > 0) { int xxX = arrX[i]; int yyY = arrY[i] - 1; if (mineArray[xxX][yyY] == 0) { if (!findIn(arrX, arrY, cntEmpty, xxX, yyY)) { arrX[cntEmpty] = xxX; arrY[cntEmpty] = yyY; cntEmpty++; } } } if (arrY[i] < (btnhgt - 1)) { int xxX = arrX[i]; int yyY = arrY[i] + 1; if (mineArray[xxX][yyY] == 0) { if (!findIn(arrX, arrY, cntEmpty, xxX, yyY)) { arrX[cntEmpty] = xxX; arrY[cntEmpty] = yyY; cntEmpty++; } } } } for (int k = 0; k < cntEmpty; k++) { button[arrX[k]][arrY[k]].setBackground(new Color(200, 200, 250)); } } public boolean findIn(int[] arrX, int[] arrY, int cntEmpty, int xxX, int yyY) { int j = 0; for (j = 0; j < cntEmpty; j++) { if ((arrX[j] == (xxX)) && (arrY[j] == (yyY))) { return true; } } return false; } public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent me) { for (int i = 0; i < btnwdth; i++) for (int j = 0; j < btnhgt; j++) { if (button[i][j] == me.getSource()) { smiley.setIcon(cryImageIcon); } } if (me.getSource() == smiley) { smiley.setIcon(cryImageIcon); } } public void mouseReleased(MouseEvent me) { // TODO Auto-generated method stub if (me.getSource() == smiley) { smiley.setIcon(smileyImageIcon); } for (int i = 0; i < btnwdth; i++) for (int j = 0; j < btnhgt; j++) { if (button[i][j] == me.getSource()) { if (mineArray[i][j] == 9) { smiley.setIcon(lossImageIcon); } else { smiley.setIcon(smileyImageIcon); } } } } public ImageIcon getScaledImage(String imageString) { ImageIcon imageIcon = new ImageIcon(imageString); Image image = imageIcon.getImage(); Image newimg = image.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); imageIcon = new ImageIcon(newimg); return imageIcon; } public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } } |
Driver.java
1 2 3 4 5 6 |
public class Driver { /** * @param args */ public static void main(String[] args) { InitScreen intzeScrn=new InitScreen(); } } |