📄 minesweeperz.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// The program creates the game MineSweeperthe.The class MineSweeperz contains almost all the fuctions and attributes
//that are need for the game.such as randMines(),initMines()borderMines ( ),mousePressed(MouseEvent event),showMines(),
//showRemainingMines( int i),checkIsRight(),FindBlankButton(int i,int j) and so on .When player click the left key of
//the mouse,if you find the number of the mines around the square,click the right key ,square will be marked as mines.
//When player mark all the mines ,he/she will find the result .
public class MineSweeperz extends JFrame implements MouseListener {
private JButton buttons[][];
private Container container;
private GridLayout grid1;
private JPanel buttonPane;
private JTextField textField;
private boolean flagIsMine[][] =new boolean[10][10];//when there is mine,it is true
private int NumBorderMine[][] = new int[10][10]; //the number of mines around
private int findMinesNum=10;//the number of mine
private boolean markIsMine[][] =new boolean[10][10];//when marked mine,it become true
private boolean flagTurn[][] = new boolean[10][10];//whether or not the buttoen is touched,true is.
public MineSweeperz()
{
super( "MineSweeper" );
// set up layouts
grid1 = new GridLayout(10,10);
container = getContentPane();
buttons = new JButton[10][10];
buttonPane = new JPanel();
buttonPane.setLayout(grid1);
container.setLayout(new BorderLayout());
showRemainingMines(10);
for ( int i = 0; i < 10;i++ ) {
for ( int j = 0; j < 10; j++ ){
buttons[i][j] = new JButton();
buttons[i][j].addMouseListener(this);
buttonPane.add( buttons[i][j]);
}
}
container.add(buttonPane,BorderLayout.CENTER);
setSize( 350, 350 );
setVisible( true );
setResizable(false);
randMines();//place mines
}
//the function is placing 10 mines randomly
public void randMines()
{
for(int k=0;k<10;k++)
{
//there may be the same 'x' and 'y', so some mines have the same position
int x =(int)(Math.random()*9); //0-9random number
int y =(int)(Math.random()*9);
flagIsMine[x][y] = true; // place mine
}
initMines();//check if there is mines have the same position
}
//the function is to see if there are some mines placed the same position.If there are , replace .If not ,continues.
public void initMines()
{
int MinesNum=0;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(flagIsMine[i][j] ==true)
MinesNum++;
}
}
if(MinesNum==10 )//check if there are ten mines placed at the different positions
{
borderMines();
}
else{//there are some mines placed the same position
for(int s=0;s<10;s++){
for(int t=0;t<10;t++){
flagIsMine[s][t] =false;//clean the mines
}
}
randMines();//replace the mines
}
}
//the function is calculating the number of mines around the square
public void borderMines ( )
{
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(flagIsMine[i][j] ==false)
{
//check if there is a mine on the lower right-hand corner of the square
if( (i+1<10)&& (j+1<10 )&& (flagIsMine[i+1][j+1] == true) )
NumBorderMine[i][j]++;
//check if there is a mine on the lower right-hand corner of the square
if((i+1<10)&& (flagIsMine[i+1][j] == true))
NumBorderMine[i][j]++;
//check if there is a mine on the lower side of the square
if((i+1<10) && (j-1>-1) &&(flagIsMine[i+1][j-1] == true))
NumBorderMine[i][j]++;
//check if there is a mine on the right-hand side of the square
if((j+1<10 )&& (flagIsMine[i][j+1] == true))
NumBorderMine[i][j]++;
//check if there is a mine on the left-hand of side the square
if((j-1>-1 )&& (flagIsMine[i][j-1] == true))
NumBorderMine[i][j]++;
// check if there is a mine on the top right-hand corner of the square
if((i-1>-1)&&(j+1<10 )&& (flagIsMine[i-1][j+1] == true))
NumBorderMine[i][j]++;
//check if there is a mine on the top left-hand corner of the square
if((i-1>-1) && (j-1>-1) &&(flagIsMine[i-1][j-1] == true))
NumBorderMine[i][j]++;
//check if there is a mine on the top of the square
if((i-1>-1) &&(flagIsMine[i-1][j] == true))
NumBorderMine[i][j]++;
}
}
}
}
//the function is controlling the mouse click. If click the right key , the mine will be marked.If click the left
//key set off mine or reveal mine count which is the number of neighbors who have mines. If none of the neighbors
// contain mines, then display nothing so the board does not get too cluttered.
public void mousePressed(MouseEvent event)
{
boolean right = SwingUtilities.isRightMouseButton(event);
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(right&&(event.getSource()==buttons[i][j])){
if( findMinesNum != 0)
{
if(flagTurn[i][j]==false){
buttons[i][j].setIcon(new ImageIcon("Marked.png"));//marked mines
flagTurn[i][j]=true;
markIsMine[i][j]=true;
--findMinesNum;
}
else {
if((flagTurn[i][j]==true)&&(markIsMine[i][j]== true))
{
findMinesNum++;
buttons[i][j].setIcon(new ImageIcon("question1.PNG"));
flagTurn[i][j]=false;
markIsMine[i][j]=false;
}
}
}
else //if (findMinesNum == 0 )
{
checkIsRight();
}
}
else if ((event.getSource()==buttons[i][j])&&(flagTurn[i][j]==false))
{
//touch the mine ,game is over
flagTurn[i][j]=true;
if( flagIsMine[i][j] == true){
Icon bug2=new ImageIcon("10.JPG");
buttons[i][j].setIcon(bug2);
showMines();
}
else
FindBlankButton(i,j);
}
}
}
showRemainingMines(findMinesNum);
}
//the function is showing the positions of the mines when lose the game.
public void showMines()
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(flagIsMine[i][j] == true)
buttons[i][j].setIcon(new ImageIcon("bomb.gif"));
}
}
JOptionPane.showMessageDialog( null,"@_@Sorrr,gameover is over ! ");
}
//the function is showing the numbers of mines that are not marked
public void showRemainingMines( int i)
{
textField = new JTextField( " mine remaining : "+i );
textField.setEditable( false );
container.add(textField,BorderLayout.NORTH);
}
//the function is checking whether or not player marked all mines exactly
public void checkIsRight()
{
int number=0;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if((flagIsMine[i][j] == true )&&(markIsMine[i][j]==true))
number++;
}
}
if(number==10 )
{
JOptionPane.showMessageDialog( null,"Successful!!^_^");
}
else
showMines();
}
//the function is showing the the the number of the mines around the square.If the number is zero,
//all neighbors of the square to reveal themselves
public void FindBlankButton(int i,int j)
{
//show the the the number(0~8) of the mines
if( NumBorderMine[i][j]==1){
buttons[i][j].setIcon(new ImageIcon("button1.PNG"));
flagTurn[i][j]=true;
}
else if(NumBorderMine[i][j]==2){
buttons[i][j].setIcon(new ImageIcon("button2.PNG"));
flagTurn[i][j]=true;
}
else if(NumBorderMine[i][j]==3){
buttons[i][j].setIcon(new ImageIcon("button3.PNG"));
flagTurn[i][j]=true;
}
else if(NumBorderMine[i][j]==4){
buttons[i][j].setIcon(new ImageIcon("button4.PNG"));
flagTurn[i][j]=true;
}
else if(NumBorderMine[i][j]==5){
buttons[i][j].setIcon(new ImageIcon("button5.PNG"));
flagTurn[i][j]=true;
}
else if(NumBorderMine[i][j]==6){
buttons[i][j].setIcon(new ImageIcon("button6.PNG"));
flagTurn[i][j]=true;
}
else if(NumBorderMine[i][j]==7){
buttons[i][j].setIcon(new ImageIcon("button7.PNG"));
flagTurn[i][j]=true;
}
else if(NumBorderMine[i][j]==8){
buttons[i][j].setIcon(new ImageIcon("button8.PNG"));
flagTurn[i][j]=true;
}
else if(NumBorderMine[i][j]==0){
buttons[i][j].setIcon(new ImageIcon("button0.PNG"));
flagTurn[i][j]=true;
//the number is zero,all neighbors of the square to reveal themselves
if((i+1<10)&&(j+1<10)&&(flagIsMine[i+1][j+1]==false)&&(flagTurn[i+1][j+1]==false))
FindBlankButton(i+1,j+1);
if((i+1<10)&&(flagIsMine[i+1][j]==false)&&(flagTurn[i+1][j]==false))
FindBlankButton(i+1,j);
if((i+1<10)&&(j-1>-1)&&(flagIsMine[i+1][j-1]==false)&&(flagTurn[i+1][j-1]==false))
FindBlankButton(i+1,j-1);
if((j+1<10)&&(flagIsMine[i][j+1]==false)&&(flagTurn[i][j+1]==false))
FindBlankButton(i,j+1);
if((j-1>-1)&&(flagIsMine[i][j-1]==false)&&(flagTurn[i][j-1]==false))
FindBlankButton(i,j-1);
if((i-1>-1)&&(j+1<10)&&(flagIsMine[i-1][j+1]==false)&&(flagTurn[i-1][j+1]==false))
FindBlankButton(i-1,j+1);
if(((i-1>-1)&&flagIsMine[i-1][j]==false)&&(flagTurn[i-1][j]==false))
FindBlankButton(i-1,j);
if((i-1>-1)&&(j-1>-1)&&(flagIsMine[i-1][j-1]==false)&&(flagTurn[i-1][j-1]==false))
FindBlankButton(i-1,j-1);
}
}
public void mouseClicked(MouseEvent event){
}
public void mouseReleased(MouseEvent event){
}
public void mouseEntered(MouseEvent event){
}
public void mouseExited(MouseEvent event){
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -