What can you do?

Source: Internet
Author: User

Package Lu;
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;

Public class Mine extends mouseadapter {

Private jframe mainframe;
Private int [] [] data;
Private jbutton [] [] buttons;
Private jbutton startjb;
Private Label L;
Private int row;
Private int Col;
Private int minenumber;
Private int minecount;
Private Boolean isover;

Public mine (){
Row = 15;
Col = 15;
Mainframe = new jframe (" lu1.0 ");
Data = new int [row] [col];
Buttons = new jbutton [row] [col];
Startjb = new jbutton ("START ");
L = new label ("Welcome to mine ");
Minenumber = row * COL/7;

}

Public void init (){
JPanel north = new JPanel ();
JPanel center = new JPanel ();
JPanel south = new JPanel ();
North. setLayout (new FlowLayout (); // Layout
Center. setLayout (new FlowLayout ());
South. setLayout (new GridLayout (row, col, 4, 1); // minefield
MainFrame. setLayout (new BorderLayout ());
MainFrame. add (north, BorderLayout. NORTH); // add a panel
MainFrame. add (center, BorderLayout. CENTER );
MainFrame. add (south, BorderLayout. SOUTH );
North. add (l); // add a tag
StartJB. addActionListener (new ActionListener () {// start button
Public void actionreceivmed (ActionEvent e) {// MMS starts
For (int I = 0; I <row; I ++ ){
For (int j = 0; j <col; j ++ ){
Buttons [I] [j]. setText ("");
Buttons [I] [J]. setbackground (color. White );
Data [I] [J] = 0;
Isover = false;
}
}
Hashmine ();
Minecount = 0;
L. settext ("Let's go! ");
}
});
Center. Add (startjb );
For (INT I = 0; I <row; I ++) {// initialization button
For (Int J = 0; j <Col; j ++ ){
Buttons [I] [J] = new jbutton ("");
Buttons [I] [J]. setname (I + ":" + J); // The button NAME records rows and columns
Buttons [I] [J]. setbackground (color. White );
Buttons [I] [J]. addmouselistener (this );
South. Add (buttons [I] [J]);

}
}
Hashmine ();
// Random Layout
}

Public void hashmine () {// randomly deploy the minefield
For (INT I = 0; I <minenumber; I ++ ){
Data [(int) (Math. random () * row)] [(int) (Math. random () * col)] =-1;
}
For (int I = 0; I <row; I ++ ){
For (int j = 0; j <col; j ++ ){
If (data [I] [j] =-1)
Continue;
Int sum = 0;
For (int m =-1; m <= 1; m ++ ){
For (int n =-1; n <= 1; n ++ ){
If (I + m> = 0 & j + n> = 0 & I + m <row & I + n <col ){
If (data [I + m] [j + n] =-1)
Sum ++;
}
}
}
Data [I] [j] = sum;
}
}
}

Public void start (){
MainFrame. setSize (800,600 );
MainFrame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
MainFrame. setVisible (true );

}

Private void gameOver (boolean over ){
If (over = true ){
For (int I = 0; I <row; I ++ ){
For (int j = 0; j <col; j ++ ){
If (data [I] [j] =-1 ){
Buttons [I] [j]. setText ("M ");
Buttons [I] [j]. setBackground (Color. RED );
}
}
}
L. setText ("-_-");
IsOver = true;
Return;
}
Int sumPress = 0;
For (int I = 0; I <row; I ++ ){
For (int j = 0; j <col; j ++ ){
If (! Buttons [I] [j]. getText (). equals ("")){
SumPress ++;
}
}

}
If (sumPress = row * col ){
Int sum = 0;
For (int I = 0; I <row; I ++ ){
For (int j = 0; j <col; j ++ ){
If (data [I] [j] =-1 & buttons [I] [j]. getText (). endsWith ("M ")){
Sum ++;
}
}
}
If (sum> = mineNumber ){
System. out. println (mineNumber );
L. setText ("^_^ ");
}
}
}

Public void mousePressed (MouseEvent e ){
Try {
If (isOver)
Return;
If (e. getButton () = MouseEvent. BUTTON3 ){
JButton jb = (JButton) e. getSource ();
If (jb. getText (). endsWith ("M ")){
Jb. setText ("");
MineCount --;
Jb. setBackground (Color. WHITE );
}
Else {
If (mineCount <mineNumber ){
Jb. setText ("M ");
Jb. setBackground (Color. BLUE );
MineCount ++;
}
Else {
L. setText ("The mine glag is over! ");
}
}
}
Else {
JButton jb = (JButton) e. getSource ();
MousePress (jb );
}
}
Catch (Exception ex ){
Ex. printStackTrace ();
}
GameOver (false );
}

Private void mousePress (JButton jb ){
String str [] = jb. getName (). split (":");
Int I = Integer. parseInt (str [0]);
Int j = Integer. parseInt (str [1]);
If (data [I] [j] =-1 ){
GameOver (true );
Return;
}
Else {
Jb. setText (data [I] [j] + "");
Jb. setBackground (Color. YELLOW );
If (data [I] [j] = 0 ){
For (int m =-1; m <= 1; m ++ ){
For (int n =-1; n <= 1; n ++ ){
If (I + m> = 0 & j + n> = 0 & I + m <row & I + n <col ){
If (buttons [I + m] [j + n]. getText (). equals ("") mousePress (buttons [I + m] [j + n]);
}
}
}
}
}
}

Public static void main (String [] args ){
Mine mine = new Mine ();
Mine. init ();
Mine. start ();
}

}

Exception in thread "main" java. lang. ArrayIndexOutOfBoundsException: 15
At lu. Mine. hashMine (Mine. java: 82)
At lu. Mine. init (Mine. java: 67)
At lu. Mine. main (Mine. java: 196)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.