In your current case, you check also if the order of your points is the same, otherwise your game won’t finish. My problem is: when I introduce a number of mines bigger then 3 even if I set all mines and I know that points.equals(guess) the game return false and game continue. } else if (fields.get(new Point(x, y)) = '.' & (getCharAt(x, y) = "0" || getCharAt(x, y) = "X")) if this is true this answer go to boolean variables finished and while see that finished is true and show ("Congratulations! You found all mines!") Once the basic layout is taken care of the next thing that needs to be done is build the squares. For simplicity sake we will assume that the grid will always be 10x10. In Java we will do this by creating a JPanel with GridLayout. Private static final char EMPTY_SPACE = '.' For Minesweeper the basic interface for the game is a simple grid with squares. Private static final int NR_OF_FIELDS = 81 The left-hand counter shows the number of mines less the number of squares that. When you've exposed all the vacant squares, you win. Do it again to mark it as questionable, and again to clear the mark. Paint itself based on its status // public void paint() else if (e.getButton() = MouseEvent.Minesweeper minesweeper = new Minesweeper() Shift-click (or right-click) on a square to flag it as a suspected mine. Set JButton's default display properties Int row, col // The row and column number of the cell Public static final Font FONT_NUMBERS = new Font("Monospaced", Font.BOLD, 20) Public static final Color FG_REVEALED = Color.YELLOW // number of mines Public static final Color BG_REVEALED = Color.DARK_GRAY Public static final Color FG_NOT_REVEALED = Color.RED // flag, mines Minesweeper.java class have 2 d array board and flagMatrix for maintaining mines.
Java code for minesweeper game code#
This minesweeper java code have two java classes Minesweeper.java and MainMinesweeper.java. Let’s see simple minesweeper java code : Minesweeper Program in java. Public static final Color BG_NOT_REVEALED = Color.GREEN In this tutorial we used 2d array for minesweeper game in java. Name-constants for JButton's colors and fonts * row/column numbers and status of each cell. * Customize (subclass) the JButton to include Instead of using raw JButton, we shall customize (subclass) JButton called Cell, to include the row/column, and some status fields.
![java code for minesweeper game java code for minesweeper game](https://fiverr-res.cloudinary.com/images/q_auto,f_auto/gigs/179982110/original/05f7b947121b6df007f0479e46c86803db18f5e1/code-your-java-project-and-provide-help-and-insight.jpg)
The Cell Class - Subclass (Customize) JButton
![java code for minesweeper game java code for minesweeper game](https://media.cheggcdn.com/media/ace/aced0cf1-0810-4c3b-af87-10a97b2150d2/phpU7YkhY.png)
If you are using JDK/TextEditor, create a sub-directory called minesweeper. In this project, you will write code to play the MineSweeper game. I have given you code that defines the MineField and MineFieldFileIO (solution to P1 and P2) in the given folder. See the slides from class for pictures of the game. In Eclipse/NetNeans, first create a package called minesweeper, before writing the classes. This project continues creation of the MineSweeper game found on some computers. Class Diagram Package minesweeperĪll the classes are kept under a package called minesweeper. setpanel (savedlevel, savedblockr, savedblockc, savednumofmine) setpanel (savedlevel, savedblockr, savedblockc, savednumofmine) tBorder (BorderFactory. For the basic version with 10x10 cells, construct a 10x10 JButton array and arranged in 10x10 GridLayout. You win if all the cells not containing mines are revealed you lose if you reveal a cell containing a mine. You left-click to reveal a cell and right-click on a cell to plant/remove a flag marking suspicious mine. The objective is to clear a rectangular mine field containing hidden "mines" without detonating any of them, with help from clues about the number of neighboring mines in the revealed cells. Mine Sweeper is a single-player mind game. You could wiki "Mine Sweeper" to understand the rules of the game.