Die Beispiele in diesem Artikel teilen Ihnen den spezifischen Code des Java-Mine-Sweeping-Spiels für Ihre Referenz. Der spezifische Inhalt ist wie folgt
Import Java.awt.BorderLayout; Import Java.awt.Color; Import Java.awt.Container; Import Java.awt.GridLayout; Import Java.awt.Insets; Import Java.awt.label; Java.awt.event.ItemEvent; Import Java.awt.event.ItemListener; Import Java.awt.event.MouseeEvent; Import Java.awt.event.mouselistener; Import Java.util.Random; importieren javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jpanel; öffentliche Klasse Saolei implementiert Mouselistener, actionListener {jpanel p = new jpanel (); JFrame Fram = new JFrame ("Mine-Sweeping"); @SuppressWarnings ("rawtypes") jcomBobox combobox = new JcomBOBOX (); Jbutton Reset = New Jbutton ("Neustart"); Container Container = neuer Container (); // Spieldatenstruktur Saoleiconstant Constant = New Saoleiconstant (); Jbutton [] [] Buttons = new Jbutton [konstant.row] [constant.col]; // Taste int [] [] zählt = new int [konstant.row] [konstant.col]; // Definieren Sie das Integer -Array -Save -Nummer unterhalb der Schaltfläche // Constructor öffentlich Saolei () {// // // //// // Windows -Frame (600, 700); //// // Windows -Frame (600, 700); Frame.SetResizable (falsch); Frame.SetDefaultCloseOperation (jframe.exit_on_close); Frame.SetLayout (New BorderLayout ()); // Wiederholung hinzufügen und den Schwierigkeitsgrad -Taste addtopbutton () aus Wählen; // Minefield -Taste hinzufügen addbuttons (); // Brown Mine addlei (); // Lightning Count Calcneibolei () hinzufügen; Frame.SetVisible (True); } void addtopbutton () {p.removeall (); P.Add (Reset); reset.setbackground (color.green); reset.setopaque (true); reset.addactionListener (this); //combobox.additem("Select Schwierigkeitsgrad "); combobox.additem ("Neuling Schwierigkeitsgrad"); combobox.additem ("grundlegende Schwierigkeit"); Combobox.AddItem ("Zwischenschwierigkeit"); Combobox.AddItem ("Advanced Schwierigkeit"); Combobox.AddItem ("Master -Schwierigkeitsgrad"); combobox.setbackground (color.green); combobox.setopaque (true); combobox.additemListener (neuer itemListener () {@Override public void itemStatEchanged (itemEvent e) {String item = e.getItem (). toString (); if (item == "newbie schwierigen") {konstant. ResetGame (); P.Add (Combobox); Frame.Add (P, BorderLayout.North); //p.add(new Label ("Gesamtzahl der Threads:"+constant.leicount, label.center)); //p.add(new Label ("Gesamtzahl der Threads:"+constant.leicount, label.Right)); } /* void addnandubutton () {nandu.setbackground (color.green); Nandu.Setopaque (wahr); Nandu.AddactionListener (dies); Frame.Add (Nandu, BorderLayout.West); } void adDresetButton () {Reset.Setbackground (color.green); reset.setopaque (true); reset.addactionListener (this); //reset.addmouselistener(this); Frame.Add (Reset, BorderLayout.North); } */ void addlei () {random rand = new random (); int Randrow, Randcol; für (int i = 0; i <constant.leicount; i ++) {randrow = rand.nextint (konstant.row); Randcol = Rand.Nextint (konstant.col); if (zählt [randrow] [randcol] == constant.leiseide) {i--; } else {zählt [Randrow] [Randcol] = constant.leisicode; //buttons -randrow], ° -Randcol weibliche. }}} void addbuttons () {Frame.Add (Container, BorderLayout.Center); Container.SetLayout (neuer GridLayout (konstant.row, constant.col)); für (int i = 0; i <constant.row; i ++) {für (int j = 0; j <constant.col; j ++) {jbutton button = new Jbutton (); Button.Setbackground (color.white); button.setopaque (true); button.addactionListener (this); button.addmouselistener ((Mouselistener) dies); Tasten [i] [j] = Taste; Container.Add (Taste); }}} void CalcNeibolei () {int count; für (int i = 0; i <constant.row; i ++) {für (int j = 0; j <constant.col; j ++) {count = 0; if (zählt [i] [j] == constant.leicode) fortfahren; if (i> 0 && j> 0 && zählt [i-1] [j-1] == constant.leiside) count ++; if (i> 0 && zählt [i-1] [j] == constant.leisicode) count ++; if (i> 0 && j <19 && zählt [i-1] [j+1] == constant.leisicode) count ++; if (j> 0 && zählt [i] [j-1] == constant.leisicode) count ++; if (j <19 && zählt [i] [j+1] == constant.leisicode) count ++; if (i <19 && zählt [i] [j+1] == constant.leisicode) count ++; if (i <19 && j> 0 && zählt [i+1] [j-1] == constant.leisicode) count ++; if (i <19 && zählt [i+1] [j-1] == constant.leisicode) count ++; if (i <19 && zählt [i+1] [j] == constant.leisicode) count ++; if (i <19 && j <19 && zählt [i+1] [j+1] == constant.leisicode) count ++; zählt [i] [j] = count; Schaltflächen [i] [j] .Setmargin (neue Einsätze (0,0,0,0); // Lassen Sie die Schaltfläche mit dem Muster auf der Taste // Schaltflächen [i] [j] .SetText (zählt [i] [j] + ""); }}} @Override public void actionPerformed (actionEvent e) {jbutton button = (jbutton) e.getSource (); if (button.equals (Reset)) {ResetGame (); // Starten Sie das Spiel neu} else {int count = 0; für (int i = 0; i <constant.row; i ++) {für (int j = 0; j <constant.col; j ++) {if (button.equals (buttons [i] [j])) {count = counts [i] [j]; if (count == constant.leisicode) {losegame (); } else {opencell (i, j); checkWin (); } zurückkehren; }}}}}}}}}}}} public void mouseclicked (MouseEvent e) {jbutton button = (jbutton) e.getSource (); if (e.getButton() == MouseEvent.BUTTON3) {//Judge the right-click action of the mouse for(int i=0;i<constant.row;i++) { for(int j=0;j<constant.col;j++) { if(button.equals(buttons[i][j])) { if((buttons[i][j].isEnabled() == true)) { //buttons · i]...Setenabled(False); Schaltflächen [i] [j] .Setmargin (neue Einsätze (0,0,0,0)); // Lassen Sie die Schaltflächen mit dem Muster auf den Schaltflächen der Schaltflächen [i] [j] .SetText ("?") ändern; zurückkehren; }}}}}}}}}} void resetGame () {for (int i = 0; i <constant.row; i ++) {für (int j = 0; j <constant.col; j ++) {Buttons [i] [j] .setText ("); Schaltflächen [i] [j] .SetEnabled (true); Tasten [i] [j] .Setbackground (Farbe.White); zählt [i] [j] = 0; }} addlei (); calcneibolei (); } void checkWin () {for (int i = 0; i <constant.row; i ++) {für (int j = 0; j <constant.col; j ++) {if (buttons [i] [j] .isenabled () == true && counts [i] [j]! = constant.leicode) return; }} Joptionpane.showMessagedialog (Rahmen, "Ja, du hast gewonnen!"); } // Verwenden Sie eine rekursive Methode zu openCell (int i, int j) {if (buttons [i] [j] .Isenabled () == false) return; Knöpfe [i] [j] .Setbackground (color.yelb); Knöpfe [i] [j] .Setopaque (true); Schaltflächen [i] [j] .setEnabled (false); if (zählt [i] [j] == 0) {if (i> 0 && j> 0 && zählt [i-1] [j-1]! if (i> 0 && j <19 && zählt [i-1] [j]! if (i> 0 && j <19 && zählt [i-1] [j+1]! if (j> 0 && zählt [i] [j-1]! if (j <19 && zählt [i] [j+1]! if (i <19 && j> 0 && zählt [i+1] [j-1]! if (i <19 && zählt [i+1] [j]! if (i <19 && j <19 && zählt [i+1] [j+1]! } else {buttons [i] [j] .setmargin (neue Insets (0,0,0,0)); Schaltflächen [i] [j] .setText (zählt [i] [j] + ""); }} void losegame () {für (int i = 0; i <constant.row; i ++) {für (int j = 0; j <constant.col; j ++) {int count = counts [i] [j]; if (count == constant.leisicode) {buttons [i] [j] .setmargin (neue Insets (0,0,0,0)); Schaltflächen [i] [j] .setText ("Lei"); Tasten [i] [j] .Setbackground (color.red); Schaltflächen [i] [j] .setEnabled (false); } else {buttons [i] [j] .setmargin (neue Insets (0,0,0,0)); Schaltflächen [i] [j] .setText (count + ""); Schaltflächen [i] [j] .setEnabled (false); }}} Joptionpane.showMessagedialog (Frame, "Fehler, Sie haben verloren!"); } public static void main (String [] args) {new Saolei (); } @Override public void mousePressed (MouseEvent e) {// Todo Auto-Generated-Methode Stub} @Override public void mouseReased (MouseEvent e) {// todo auto-generatter Methode Stub} @Override public void modentered (Mousevent E) {// / / todo auto-generated modum modentrouvide @{ / // / / todo auto-generated modum methodentrouved | Mouseexited (MouseeEvent e) {// Todo automatisch generierter Methode Stub}}Konstante Klasse
public class saoleiconstant {endgültige int row = 20; // Anzahl der Zeilen 30 endgültig int col = 20; // Anzahl der Spalten 30 endgültig int leicode = 10; // Definieren Sie die Anzahl unter dem donngeschützten int temp = 20; geschützte int leicount = temp; // Anzahl von Donner 30}Reproduktionsdiagramm
Weitere aufregende Spiele finden Sie auf dem speziellen Thema "Java Classic Games".
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.