Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.99 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.ArrayList;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JOptionPane;
  12.  
  13. public class Minesweeper3 implements ActionListener {   //erstellt ein Unterprogramm mit dem Namen Minesweeper 2
  14.    
  15.     JFrame frame = new JFrame("Minesweeper");   //Erstellt die grafische Oberfläche (das Fenster)
  16.     JButton reset = new JButton("Reset");       // Erstellt einen Button, auf den eine Aktion ausgeführt werden kann
  17.     JButton [][] buttons = new JButton[20][20]; //Erstellt ein Feld aus 20x20 Button
  18.     int[][] counts = new int [20][20];          //Erstellt ein Feld
  19.     Container grid= new Container();            //hilft bei der Strukturierung und der Gruppierung der Bedienelemente
  20.     final int MINE = 10;                        //Variable, die nicht verändert werden kann
  21.    
  22.     public Minesweeper3()
  23.     {
  24.         frame.setSize(1000,700);                //legt Größe des Fensters fest
  25.         frame.setLayout(new BorderLayout());;   //unterteilt das Fenster in vier Abschnitte (Norden, Osten, Süden, Westen)
  26.         frame.add(reset, BorderLayout.NORTH);   //setzt den Reset Button an denn oberen Teil des Fensters
  27.         reset.addActionListener(this);          //sollte am Reset Button eine Aktion auftreten, wird diese an die Operation reset gemeldet
  28.        
  29.         grid.setLayout(new GridLayout(20,20));  //erzeugt ein gridlayout mit 20x20 gleich großen Feldern, die sich automatisch verändern,
  30.                                                 //wenn sich das Fenster verändert
  31.        
  32.         for (int a = 0; a < buttons.length; a++)    //geschachtelte For-Schleife, um die 20x20 Button zu realisieren,
  33.         {                                           //die später auf der Console ausgegeben werden
  34.             for (int b = 0; b < buttons[0].length; b++)
  35.             {
  36.                 buttons[a][b] = new JButton();
  37.                 buttons[a][b].addActionListener(this);
  38.                 grid.add(buttons[a][b]);
  39.             }
  40.            
  41.         }
  42.        
  43.         frame.add(grid, BorderLayout.CENTER);   //setzt den Container in das Zentrum des Fensters
  44.         createRandomMines();                    //ruft das Unterprogramm createRandomMines auf
  45.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //Mit einem Klick auf ddas X wird die Anwendung beendet
  46.         frame.setVisible(true);     //Fenster bleibt geöffnet
  47.        
  48.     }
  49.  
  50.     public static void main(String[] args)  //Hauptprogramm
  51.     {
  52.         new Minesweeper3();     //öffnet das Unterprogramm Minesweeper2
  53.     }
  54.    
  55.    
  56.     public void createRandomMines()         //Unterprogramm createRandomMines
  57.     {
  58.        
  59.         ArrayList <Integer> list = new ArrayList<Integer>(); //Erstellt eine ArrayList, da noch nicht sicher ist, wie groß
  60.                                                              //das Array wird
  61.         for (int x = 0; x < counts.length; x++)              //geschachtelte For-Schleife
  62.         {
  63.             for (int y = 0; y < counts[0].length; y++)
  64.             {                                              
  65.                 list.add(x*100+y);                         
  66.             }                                              
  67.            
  68.         }
  69.        
  70.         counts = new int [20][20];
  71.        
  72.         for (int a = 0; a < 30; a++)    //erstellt 30 Minen
  73.         {
  74.             int choice = (int)(Math.random() * list.size()); //wählt eine Zahl zwischen >= 0 und < 1 aus und multipliziert diese Zahl mit
  75.                                                              //der Größe der Liste (400)
  76.             counts[list.get(choice)/100][list.get(choice) % 100] = MINE;//setzt eine neue Mine an den neuen Punkt
  77.             list.remove(choice); //verhindert, dass ein Platz doppelt besetzt wird
  78.            
  79.         }
  80.        
  81.         //Nachbarn zählen
  82.         for (int x = 0; x < counts.length; x++)
  83.         {
  84.             for (int y = 0; y < counts[0].length; y++)
  85.             {
  86.                 if (counts[x][y] != MINE)   //solange an der Stelle keine Mine ist, soll gezählt werden
  87.                 {  
  88.                
  89.                     int neighborcount = 0;
  90.                    
  91.                     if(x > 0 && y > 0 && counts[x-1][y-1] == MINE)
  92.                     {
  93.                         neighborcount++;
  94.                     }
  95.                    
  96.                     if(y > 0 && counts[x][y-1] == MINE)
  97.                     {
  98.                         neighborcount++;
  99.                     }
  100.                    
  101.                     if(x < counts.length - 1 && y > 0 && counts[x+1][y-1] == MINE)
  102.                     {
  103.                         neighborcount++;
  104.                     }
  105.                    
  106.                     if(x > 0 && counts[x-1][y] == MINE)
  107.                     {
  108.                         neighborcount++;
  109.                     }
  110.                    
  111.                     if ( x < counts.length -1 && counts[x+1][y] == MINE)
  112.                     {
  113.                         neighborcount++;
  114.                     }
  115.                     if (x > 0 && y < counts[0].length -1 && counts[x-1][y+1] == MINE)
  116.                     {
  117.                         neighborcount++;
  118.                     }
  119.                    
  120.                     if (y < counts[0].length -1 && counts[x][y+1] == MINE)
  121.                     {
  122.                         neighborcount++;
  123.                     }
  124.                    
  125.                     if (x < counts.length -1 && y < counts[0].length -1 && counts[x+1][y+1] == MINE)
  126.                     {
  127.                         neighborcount++;
  128.                     }
  129.                    
  130.                     counts[x][y] = neighborcount;
  131.                
  132.                 }
  133.                
  134.             }
  135.            
  136.         }
  137.  
  138.        
  139.        
  140.        
  141.     }
  142.    
  143.     public void lostGame()
  144.     {
  145.         for (int x = 0; x < buttons.length; x++)    //geschachtelte For-Schleife
  146.         {
  147.             for (int y = 0; y < buttons[0].length; y++)
  148.             {
  149.                 if (buttons[x][y].isEnabled())      //ist der Button an der Stelle [x][y] verfügbar
  150.                 {
  151.                     if(counts[x][y] != MINE)        //befindet sich an der Position keine Mine, dann sollen alle Flächen aufgedeckt werden
  152.                     {
  153.                         buttons[x][y].setText(counts[x][y] + "");
  154.                         buttons[x][y].setEnabled(false);
  155.                     }
  156.                    
  157.                     else                            //andernfalls soll ein X angezeigt werden
  158.                     {
  159.                         buttons[x][y].setText("X");
  160.                         buttons[x][y].setEnabled(false);
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.     }
  166.    
  167.     public void clearZeros (ArrayList<Integer> toClear)
  168.     {
  169.         if(toClear.size() == 0)
  170.         {
  171.             return;
  172.         }
  173.        
  174.         else
  175.         {
  176.             int x = toClear.get(0) / 100;
  177.             int y = toClear.get(0) % 100;
  178.             toClear.remove(0);
  179.            
  180.            
  181.             if (counts[x][y] == 0)
  182.             {
  183.                 if ( x > 0 && y > 0 && buttons[x-1][y-1].isEnabled()) // hoch links
  184.                 {
  185.                     buttons[x-1][y-1].setText(counts[x-1][y-1] + "");
  186.                     buttons[x-1][y-1].setEnabled(false);
  187.                     if (counts[x-1][y-1] == 0)
  188.                     {
  189.                         toClear.add((x-1) * 100 +(y-1));
  190.                     }
  191.                 }
  192.                 if (y > 0 && buttons[x][y-1].isEnabled()) //hoch
  193.                 {
  194.                     buttons[x][y-1].setText(counts[x][y-1] + "");
  195.                     buttons[x][y-1].setEnabled(false);
  196.                     if (counts[x][y-1] == 0)
  197.                     {
  198.                         toClear.add(x * 100 +(y-1));
  199.                     }
  200.                 }
  201.                 if (x < counts.length - 1 && y > 0 && buttons[x+1][y-1].isEnabled()) //hoch rechts
  202.                 {
  203.                     buttons[x+1][y-1].setText(counts[x+1][y-1] + "");
  204.                     buttons[x+1][y-1].setEnabled(false);
  205.                     if (counts[x+1][y-1] == 0)
  206.                     {
  207.                         toClear.add((x+1) * 100 +(y-1));
  208.                     }
  209.                 }
  210.                 if ( x > 0 && buttons[x-1][y].isEnabled())//links
  211.                 {
  212.                     buttons[x-1][y].setText(counts[x-1][y] + "");
  213.                     buttons[x-1][y].setEnabled(false);
  214.                     if (counts[x-1][y] == 0)
  215.                     {
  216.                         toClear.add((x-1) * 100 +y);
  217.                     }
  218.                 }
  219.                
  220.                 if (x < counts.length - 1 && buttons[x+1][y].isEnabled()) //rechts
  221.                 {
  222.                     buttons[x+1][y].setText(counts[x+1][y] + "");
  223.                     buttons[x+1][y].setEnabled(false);
  224.                     if (counts[x+1][y] == 0)
  225.                     {
  226.                         toClear.add((x+1) * 100 +y);
  227.                     }
  228.                 }
  229.                
  230.                 if ( x > 0 && y < counts[0].length -1 && buttons[x-1][y+1].isEnabled()) //runter links
  231.                 {
  232.                     buttons[x-1][y+1].setText(counts[x-1][y+1] + "");
  233.                     buttons[x-1][y+1].setEnabled(false);
  234.                     if (counts[x-1][y+1] == 0)
  235.                     {
  236.                         toClear.add((x-1) * 100 +(y+1));
  237.                     }
  238.                 }
  239.                 if (y < counts[0].length -1 && buttons[x][y+1].isEnabled()) //runter
  240.                 {
  241.                     buttons[x][y+1].setText(counts[x][y+1] + "");
  242.                     buttons[x][y+1].setEnabled(false);
  243.                     if (counts[x][y+1] == 0)
  244.                     {
  245.                         toClear.add((x) * 100 +(y+1));
  246.                     }
  247.                 }
  248.                 if (x < counts.length -1 && y < counts[0].length -1 && buttons[x+1][y+1].isEnabled()) //runter rechts
  249.                 {
  250.                     buttons[x+1][y+1].setText(counts[x+1][y+1] + "");
  251.                     buttons[x+1][y+1].setEnabled(false);
  252.                     if (counts[x+1][y+1] == 0)
  253.                     {
  254.                         toClear.add((x+1) * 100 +(y+1));
  255.                     }
  256.                 }
  257.                
  258.                
  259.             }
  260.             clearZeros(toClear);
  261.         }
  262.        
  263.     }
  264.    
  265.     public void checkWin()
  266.     {
  267.         boolean won = true;
  268.         for (int x = 0; x < counts.length; x++)
  269.         {
  270.             for (int y = 0; y < counts[0].length; y++)
  271.             {
  272.                 if(counts[x][y] != MINE && buttons[x][y].isEnabled() == true)
  273.                 {
  274.                     won = false;
  275.                 }
  276.             }
  277.            
  278.         }
  279.         if (won == true)
  280.         {
  281.             JOptionPane.showMessageDialog(frame, "You win");
  282.         }
  283.     }
  284.  
  285.     @Override
  286.     public void actionPerformed(ActionEvent event) {
  287.         if (event.getSource().equals(reset))
  288.         {
  289.             for (int x = 0; x < buttons.length; x++)
  290.             {
  291.                 for (int y = 0; y < buttons[0].length; y++)
  292.                 {
  293.                     buttons[x][y].setEnabled(true);
  294.                     buttons[x][y].setText("");
  295.                 }
  296.             }
  297.             createRandomMines();
  298.         }
  299.         else
  300.         {
  301.             for (int x = 0; x < buttons.length; x++)
  302.             {
  303.                 for (int y = 0; y < buttons[0].length; y++)
  304.                 {
  305.                     if (event.getSource().equals(buttons[x][y]))
  306.                     {
  307.                         if(counts[x][y] == MINE)
  308.                         {
  309.                             buttons[x][y].setForeground(Color.red);
  310.                             buttons[x][y].setText("X");
  311.                             lostGame();
  312.                         }
  313.                         else if(counts[x][y] == 0)
  314.                         {
  315.                             buttons[x][y].setText(counts[x][y] + "");
  316.                             buttons[x][y].setEnabled(false);
  317.                             ArrayList <Integer> toClear = new ArrayList <Integer>();
  318.                             toClear.add(x*100+y);
  319.                             clearZeros(toClear);
  320.                             checkWin();
  321.                         }
  322.                         else
  323.                         {
  324.                             buttons[x][y].setText(counts[x][y] + "");
  325.                             buttons[x][y].setEnabled(false);
  326.                             checkWin();
  327.                         }
  328.                     }
  329.                 }
  330.             }
  331.                
  332.         }
  333.        
  334.     }
  335.    
  336.    
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement