Developing Minesweeper using Java
It's been a while since I've posted anything on my blog. The reason being that lately, I've been learning new languages like Python and Java. Well anyways, I've taken the time to develop Minesweeper game using Java applets. This game also got GUI which I've created using the components provided by Java. Here are a few pictures of the game.
Here's the video of my game:
There are two classes in my game:
- Board class: This class provides the backend logic of building the board for the minesweeper game. This class has also got methods using which user can play the game on command prompt itself. I've commented out the methods required for running the game on cmd (the * beside a method listed below denotes that the method was commented). If you're interested in playing on cmd, fiddle around with invoking methods in the main method(create one!) and see if you can run it :). The various methods in Board class are:
- Board() *: Constructor to initialize the board size and number of bombs by taking input.
- Board (int rows, int cols, int bombs): Constructor to initialize the board size and number of bombs based on arguments.
- fillBombs(int number): Method to fill the bombs on the board.
- void showBoard() *: Method to draw the board on cmd.
- getInput(int row_num, int col_num): Method to accept user's input(the mine which he has clicked).
- expand (int row_no, int col_no): The most interesting method! How to uncover mines based on user's input.
- MineSweeper class: This is the class where the math and logic take colour and shape! This class implements the MouseListener interface and also extends the Applet class.
- mousePressed(MouseEvent e): Method of the MouseListener interface that needs to be overridden.
- mouseReleased(MouseEvent e): Method of the MouseListener interface that needs to be overridden.
- mouseClicked(MouseEvent e): Method of the MouseListener interface that needs to be overridden. This is the only method among all the overridden methods of MouseListener interface as no other overridden method updates variables nor performs any useful computations.
- mouseEntered(MouseEvent e): Method of the MouseListener interface that needs to be overridden.
- mouseExited(MouseEvent e): Method of the MouseListener interface that needs to be overridden.
- init(): This method initially calls the setup() method.
- setup(): Method used to set up the menu.
- paint(Graphics g): Method used to repaint the screen after each action is performed.
- drawBoard (Graphics g, Board game): Method used to graphically draw the board.
Coming to the implementation of the methods, here's the code for the Board class:
Well that's a lot of code! But that's inevitable :) and finally the MineSweeper class:
Well those we the two classes implementations. If you want to download java files or class files, here is the link (Note; that MineSweeper.java has got code for both the classes in it): DOWNLOAD