Write a mine game with Java - code ideas

xiaoxiao2021-03-06  51

Import java.awt. *; import java.awt.event. *; import javax.swing. *;

/ * Press torsion class * /

Class Bomb EXTENDS JBUTTON {Public Int Num_x, Num_Y; // Participation PUBLIC INT BombRoundCount; // Surrounded Muslic Boolean ISBOMB; / / Whether it is a thunder public boolean isclicked; // is clicked public int bombflag; //雷 标 标;;; n;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, }} / * Window and algorithm implementation class * /

class MainBomb extends JFrame implements ActionListener, MouseListener {public JTextField text; public Label nowBomb, setBomb; public int BlockNum, BombNum; // current number of the current block number Ray public Icon icon_bomb = new ImageIcon ( "Bomb.gif"); // step Ray Public icon Icon_bomb_big = new imageicon ("Bomb_BIG.GIF"); // Stepping on the thunder Public icon icon_flag = new imageicon ("flag.gif"); // Motor mark public icon icon_quelstion = new imageicon ("Question.gif" ); // Whether there is Ray Public JButton Start = New JButton ("Start"); public panel menupamel = new panel (); public panel mainpanel = new panel (); public bomb [] [] bombbutton; / * interface design * / Public mainbomb () {Super ("Mei Lei Aaron2004 Production 2004.8"); BlockNum = 64; BombNum = 10; Container C = getContentPane (); C.SetBackground (color.gray); C.SetLayout ()) Text = New JtextField ("10", 3); nowbomb = new label ("Current mine" " bombnum " "); setbomb = new label (" Setting mines "); Start.addActionListener (New ActionListener () {public void actionPerformed (ActionEvent E) {bombnum = integer.parseint (Text.getText (). Trim ()); if (Bombnum> = 10 && Bombnum <50) Replay (); else {JOPANE MSG = New Joptionpane (); JOPTIONPANE.SHOWMESSAGEDIALOG (NULL, "You set the number of mines, please reset!", "Error", 2);}}}); MenuPamel.Add (setbomb); MenuPamel.Add (Text); MenuPamel.Add (start); menupamel.add (nowb); C.Add (MenuPamel, "North");

MAINPANEL.SETLAYOUT (New GridLayout ((int) math.sqrt (block) (int) math.sqrt (block))); bombbutton = new bomb [(int) math.sqrt (blocknum)] []; for (int) i = 0; i <(int) math.sqrt (block); i ) {BombButton [i] = new bomb [(int) math.sqrt (block)];} for (int i = 0; i <(int) ) Math.sqrt (block); i ) for (int J = 0; j <(int) math.sqrt (block); j ) {BombButton [i] [j] = new bomb (i, j); bombbutton [ I] [J] .SetForeground (color.gray); BombButton [i] [j] .addactionListener (this); Bombbutton [i] [j] .addmouseristener (this);} for (int i = 0; i < INT) Math.SQRT (Blocknum); I ) for (int J = 0; j <(int) math.sqrt (block); j ) mainpanel.add (Bombbutton [i] [j]); c.add (mainpanel , "Center"); startbomb (); setsize (400,400); setLocation (350, 200); setResizable (false);} / * Bray * / public void startbomb () {for (int i = 0; i

} StartBomb ();} / * Do you dug all Ray * / public void iswin () {int Findbomb = 0; // Found

For (int 1 = 0; i <(int) math.sqrt (block); i ) for (int J = 0; j <(int) math.sqrt (block); j ) {if (Bombbutton [i] [ J.isbomb == true && bombbutton [i] [j] .sright == true) Findbomb ;} if (Findbomb == Integer.Parseint (text.getText (). Trim ()) {JOPANE MSG = New JOTIONPANE (); JOPTIONPANE.SHOWMESSAGEDIALOG (this, "You dug all the thunders, you won!", "You Victory", 2);}} / * Calculation Square Weighing * / Public void countryBomb () {for (INT i = 0; i <(int) math.sqrt (block); i ) {for (int J = 0; j <(int) math.sqrt (blocknum); J ) {int count = 0; //// When the cells need to be detected, there is no mine, the surrounding mines (BombButton [i] [j] .isbomb! = True) {IF ((i - 1> = 0) && (j - 1 > = 0)) {IF (BombButton [i - 1] [j - 1] .isbomb == true) {count = 1; // Detect whether the upper left space is mine}}}}} ((i - 1> = 0) {IF (Bombbutton [i - 1] [j] .isbomb == true) {count = 1; // Detect whether the above space is mine}} = ((i - 1> = 0) && (j 1 <= (int) math.sqrt (blocknum) -1))) {IF (BombButton [i - 1] [J 1] .isbomb == True) {count = 1; // Detect whether the upper right is mine}}}} f ((j - 1> = 0)) {IF (BombButton [i] [j - 1] .isbombomb == True) {count = 1; // Detects whether the left is mine}} = ((i> = 0) && (J 1 <= (int) Math.SQRT (Blocknum) -1)) {IF ( BombButton [i] [j 1] .isbomb == true) {count = 1; // Right}} = ((j - 1> = 0) && (i 1 <=

(int) math.sqrt (block) -1))) {IF (BombButton [i 1] [j - 1] .isbomb == true) {count = 1; // Left}} IF ((i 1) <= (int) Math.SQRT (Blocknum) -1)) {IF (Bombbutton [i 1] [j] .isbomb == true) {count = 1; //}} = ((j 1 <= (int) Math.SQRT (Blocknum) -1) && (i 1 <= math.sqrt (block) -1)) {if (BombButton [i 1] [J 1] .isbomb == TRUE ) {Count = 1; // Right}} bombbutton [i] [j] .bomBroundCount = count;}}}} / ** When the location is empty, turn off the surrounding map ** / public void ISNULL (Bomb [] [] Bombbutton, Bomb ClickecButton) {INT I, J; I = CLICKECBUTTON.NUM_X; J = ClickecButton.Num_Y; if (ClickecButton.isbomb == True) {} else {IF ((i - 1> = 0) && (j - 1> = 0)) {// Detect whether the left space is empty IF (BombButton [i - 1] [j - 1] .isbomb == false && Bombbutton [i - 1] [J - 1] .sclicked == false && Bombbutton [i - 1] [j - 1] .sright == false) {BombButton [i - 1] [j - 1] .Settext ((BombButton [i - 1] [j - 1] .bomBroundCount) "" "; BombButton [i - 1] [J - 1]. SetENabled (false); Bombbutton [i - 1] [j - 1] .sclicked = true;}}} ((i - 1> = 0)) {// Detection is empty if the above space is empty IF (BombButton [i - 1 ] [j ]bombomb == false && Bombbutton [i - 1] [j] .isclicked == false&bombbutton [i - 1] [j] .ismht == false) {BombButton [i - 1] [] .SETTEXT ((BombButton [i - 1] [j] .bombroundCount) ""); BombButton [i - 1] [j] .setenabled (false); BombButton [i - 1] [j] .isclicked = true; }}} ((I - 1> = 0) && (J 1 <

= ((int) math.sqrt (block) -1))))))))) {// Detect whether the right is empty if (BombButton [i - 1] [j 1] .isbomb == false && Bombbutton [i - 1] [J 1] .isclicked == false && BombButton [i - 1] [j 1] .ismht == false) {BombButton [i - 1] [j 1] .Settext ((BombButton [i - 1] [J 1] .bombBroundCount) ""); BombButton [i - 1] [j 1] .setenabled (false); BombButton [i - 1] [j 1] .isclicked = true;}} = (J - 1> = 0)) {// Detect whether the left is empty if (BombButton [i] [j - 1] .isbomb == false && Bombbutton [i] [j - 1] .isclicked == false&bombbutton [i] [j - 1] .ismht == false) {BombButton [i] [j - 1] .Settext (BombButton [i] [j - 1] .bombroundcount) ""); Bombbutton [i] [ J - 1] .senabled (false); Bombbutton [i] [j - 1] .Isclicked = true;}} = ((i> = 0) && (j 1 <= (int) Math.sqrt (blocknum ) -1))) {// Detect whether the right space is empty IF (BombButton [i] [j 1] .isbomb == false && Bombbutton [i] [j 1] .isclicked == false && Bombbutton [i ] [J 1] .Ist == false) {BombButton [i] [j 1] .Settext ((BombButton [i] [J 1] .bombroundcount) ""); bombbutton [i] [j 1] .setenabled (false); bombbutton [i] [j 1] .isclicked = true;}} IF ((j - 1) > = 0) && (i 1 <= ((int) math.sqrt (block) -1))))))))) {// Detect whether the lower left space is empty IF (BombButton [i 1] [J - 1] .isbombomb == false &&bombbutton [i 1] [j - 1] .sclicked == false&bombbutton [i 1] [j - 1] .Ist == false) {BombButton [i 1] [J - 1] .SETTEXT ((BombButton [i

1] [J - 1] .BombRoundCount) ""); Bombbutton [i 1] [j - 1] .setenabled (false); Bombbutton [i 1] [J - 1] .isclicked = true;}} IF ((i 1 <= ((int) math.sqrt (block)))))))))) {// Detects whether the next space is empty IF (BombButton [i 1] [J] .isbomb == false && Bombbutton [i 1] [j] .isclicked == false && Bombbutton [i 1] [j] .isright == false) {Bombbutton [i 1] [j] .settext ((Bombbutton [i 1] [ J] .bombBroundCount) "" "; Bombbutton [i 1] [j] .setenabled (false); Bombbutton [i 1] [j] .isclicked = true;}} = (((J 1 <= (int) Math.SQRT (Blocknum) -1)) && (i 1 <= ((int) Math.sqrt (block) -1))))))) {// Detects whether the right lower space is empty IF (BombButton [i 1] [J 1] .isbomb == false && BombButton [i 1] [J 1] .isclicked == false && Bombbutton [i 1] [J 1] .Ist == false) {BombButton [i 1] [J 1] .Settext ((BombButton [i 1] [J 1] .bombRoundCount "" ""); Bombbutton [i 1] [J 1] .senabled (false); Bombbutton [i 1] [J 1] .isclicked = true;}}}} ((i - 1> = 0) && (j - 1> = 0)) // Detects left ISNU LL (Bombbutton, Bombbutton [i - 1]); if ((i - 1> = 0)) ISNULL (BombButton, Bombbutton [i - 1] [j]); // Detects above IF ((( I - 1> = 0) && (j 1 <= (int) Math.sqrt (blocknum) -1))) isnull (Bombbutton, Bombbutton [i - 1] [j 1]); // Detect the right IF ( (J - 1> = 0)) ISNULL (Bombbutton, BombButton [i] [j - 1]); // Detect the left IF ((i> = 0) && (j 1 <=

((Int) Math.sqrt (BlockNum) -1))) isNull (bombButton, bombButton [i] [j 1]); // detecting the right if ((j - 1> = 0) && (i 1 < = ((int) Math.SQRT (Blocknum) -1)))))))))))))))))))))) Isnull (Bombbutton, BombBbutton [i 1] [j - 1]); // Detect the lower left IF ((i 1 <= ((int) Math . SQRT (blocknum) -1)))) // Detection ISNULL (Bombbutton, Bombbutton [i 1] [j]); if ((j 1 <= ((int) math.sqrt (blocknum) -1) ) && (i 1 <= ((int) math.sqrt (block) -1)))))) // Detect the lower right isnull (Bombbutton, BombButton [i 1] [J 1]);}} public void actionperformed (ActionEvent E) {Countoundb (); IF ((Bomb) E.GetSource ()). Isbomb == false && ((Bomb) E.GetSource ()). Isclicked == false) {(BOMB) E. GetSource ()). Settext (((Bomb) E.GetSource ()). BomBroundCount) ""); (BOMB) E.GetSource ()). isclicked = true; ((Bomb) E.Getsource () ) .Seticon (NULL); (Bomb) E.GetSource ()). setENABLED (false); if (((Bomb) E.GetSource ()). BomBroundCount) == 0) isnull (Bombbutton, (BOMB) E.getsource ()); iswin (); ((bomb) E.GetSource ()). isbomb == true) {for (int i = 0; i <(int) math.sqrt (block); i ) for (int J = 0; j <(int) math.sqrt (block) {if (Bombbutton [i] [j]. ISBMB == True) Bombbutton [i] [j] .seticon (icon_bomb);} ((bomb) E.GetSource ()). seticon (icon_bomb_big); JOPTIONPANE MSG = New joptionpane (); JOPANE.SHOWMESAGEDIALOG (this, " You step on the landmines, press OK, "You step on the mine", 2); replay ();}} public void mouseclicked (mouseEvent E) {bomb bombsource = (bomb) E.Getsource (); boolean Right = swingutilities.ismightmousebutton (e); if ((right ==)

True) && (bombsource.isclicked == false)) {bombsource.bombflag = (BombSource.bombflag 1)% 3; if (BombSource.bombflag == 1) {if (Bombnum> 0 && BombSource.IsRight == false) {BombSource.seticon (icon_flag); BombSource.Ist = true; bombnum--;} iswin (); nowbomb.Settext ("Current mine" " bombnum " ");} else if (bombsource.bombflag == 2) {IF (BombNum! = 0) || (BombNum == 0 && (BombSource.Geticon () == icon_flag))) Bombnum ; bombsource.seticon (icon_question); nowbomb.setText ("Current Miner "" BombNum "");} else if (bombSource.BombFlag == 0) {bombSource.setIcon (null); bombSource.isRight = false;}}} public void mouseEntered (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseexited (mouseevent e) {} public void mousepressed (mouseevent e) {}} / * main class * /

Public class main {public static void main (String args []) {(new mainbomb ()). show ();}}

转载请注明原文地址:https://www.9cbs.com/read-79875.html

New Post(0)