import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.InputEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class mineSweeperGame extends JFrame{
private static final long serialVersionUID = 1L;
private int rows, columns = 10;
private int cR, cC = 0;
JToggleButton buttons[][] = new JToggleButton[rows][columns];
int cells[][] = new int[rows][columns];
boolean bombs[][], shown[][] = new boolean[rows][columns];
private Container container = null;
public mineSweeperGame(){
super("Minesweeper");
startGame();
}
private void startGame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
container = this.getContentPane();
for(int r = 0; r < rows; r++){
for(int c = 0; c < columns; c++){
cR= r;
cC = c;
Random b = new Random();
bombs[r][c] = b.nextBoolean() && b.nextBoolean() && b.nextBoolean();
buttons[r][c] = new JToggleButton("");
buttons[r][c].addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseReleased(java.awt.event.MouseEvent e){
if(e.getModifiers() == InputEvent.BUTTON3_MASK){
markCell(e);
}
else if(e.getModifiers() == InputEvent.BUTTON1_MASK){
showCell(e);
}
}
});
container.add(buttons[r][c], BorderLayout.CENTER);
cells[r][c] = bombCount(r,c);
shown[r][c] = false;
}
}
this.setVisible(true);
this.validate();
this.repaint();
}
public void markCell(java.awt.event.MouseEvent e){
JToggleButton button = (JToggleButton)e.getSource();
if(button.isEnabled()){
if(button.getText() != "!"){
if(button.getText() != "!"){
button.setText("!");
}
else{
button.setText("");
}
}
}
}
public void showCell(java.awt.event.MouseEvent e){
JToggleButton buttons = (JToggleButton)e.getSource();
if(buttons.isEnabled()){
buttons.setEnabled(false);
if(isBomb(buttons)){
endGame();
buttons.setEnabled(false);
disableBoard();
}
else{
if(cells[cR][cC] > 0){
buttons.setText(Integer.toString(cells[cR][cC]));
}
else if(cells[cR][cC] == 0){
clearCells(cR, cC);
}
}
}
}
private int bombCount(int x, int y){
int bombCount = 0;
for(int r = -1; r <= 1; r++){
for(int c = -1; c <= -1; c++){
int i = x + r;
int j = y + c;
if(0 <= i && i < cells.length && 0 <= j && j < cells[i].length){
bombCount++;
}
}
}
return bombCount;
}
private void clearCells(int cR, int cC){
for(int r = 0; r < rows; r++){
for(int c = 0; c < columns; c++){
if(!shown[r][c] == false && bombs[r][c] == false){
shown[r][c] = true;
JToggleButton button = findButton(r, c);
if(cells[r][c] > 0){
button.setText(Integer.toString(cells[r][c]));
}
else{
button.setText("");
}
button.setSelected(true);
button.setEnabled(false);
if(cells[r][c] == 0){
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
clearCells(i+r, j+c);
}
}
}
}
}
}
}
private JToggleButton findButton(int r, int c){
return buttons[r][c];
}
private boolean isBomb(JToggleButton button){
for(int r = 0; r < rows; r++){
for(int c = 0; c < columns; c++){
if(button == buttons[r][c]){
cR = r;
cC = c;
return bombs[r][c];
}
}
}
return false;
}
private void endGame(){
for(int r = 0; r < rows; r++){
for(int c = 0; c < columns; c++){
if(bombs[r][c] == true){
JToggleButton button = findButton(r, c);
button.setText("*");
button.setSelected(true);
button.setEnabled(false);
}
}
}
}
private void disableBoard(){
for(int r = 0; r < rows; r++ ){
for(int c = 0; c < columns; c++){
JToggleButton button = findButton(r,c);
button.setEnabled(false);
}
}
}
public static void main(String[] args){
mineSweeperGame go = new mineSweeperGame();
}
}