Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class Minesweeper3 implements ActionListener { //erstellt ein Unterprogramm mit dem Namen Minesweeper 2
- JFrame frame = new JFrame("Minesweeper"); //Erstellt die grafische Oberfläche (das Fenster)
- JButton reset = new JButton("Reset"); // Erstellt einen Button, auf den eine Aktion ausgeführt werden kann
- JButton [][] buttons = new JButton[20][20]; //Erstellt ein Feld aus 20x20 Button
- int[][] counts = new int [20][20]; //Erstellt ein Feld
- Container grid= new Container(); //hilft bei der Strukturierung und der Gruppierung der Bedienelemente
- final int MINE = 10; //Variable, die nicht verändert werden kann
- public Minesweeper3()
- {
- frame.setSize(1000,700); //legt Größe des Fensters fest
- frame.setLayout(new BorderLayout());; //unterteilt das Fenster in vier Abschnitte (Norden, Osten, Süden, Westen)
- frame.add(reset, BorderLayout.NORTH); //setzt den Reset Button an denn oberen Teil des Fensters
- reset.addActionListener(this); //sollte am Reset Button eine Aktion auftreten, wird diese an die Operation reset gemeldet
- grid.setLayout(new GridLayout(20,20)); //erzeugt ein gridlayout mit 20x20 gleich großen Feldern, die sich automatisch verändern,
- //wenn sich das Fenster verändert
- for (int a = 0; a < buttons.length; a++) //geschachtelte For-Schleife, um die 20x20 Button zu realisieren,
- { //die später auf der Console ausgegeben werden
- for (int b = 0; b < buttons[0].length; b++)
- {
- buttons[a][b] = new JButton();
- buttons[a][b].addActionListener(this);
- grid.add(buttons[a][b]);
- }
- }
- frame.add(grid, BorderLayout.CENTER); //setzt den Container in das Zentrum des Fensters
- createRandomMines(); //ruft das Unterprogramm createRandomMines auf
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Mit einem Klick auf ddas X wird die Anwendung beendet
- frame.setVisible(true); //Fenster bleibt geöffnet
- }
- public static void main(String[] args) //Hauptprogramm
- {
- new Minesweeper3(); //öffnet das Unterprogramm Minesweeper2
- }
- public void createRandomMines() //Unterprogramm createRandomMines
- {
- ArrayList <Integer> list = new ArrayList<Integer>(); //Erstellt eine ArrayList, da noch nicht sicher ist, wie groß
- //das Array wird
- for (int x = 0; x < counts.length; x++) //geschachtelte For-Schleife
- {
- for (int y = 0; y < counts[0].length; y++)
- {
- list.add(x*100+y);
- }
- }
- counts = new int [20][20];
- for (int a = 0; a < 30; a++) //erstellt 30 Minen
- {
- int choice = (int)(Math.random() * list.size()); //wählt eine Zahl zwischen >= 0 und < 1 aus und multipliziert diese Zahl mit
- //der Größe der Liste (400)
- counts[list.get(choice)/100][list.get(choice) % 100] = MINE;//setzt eine neue Mine an den neuen Punkt
- list.remove(choice); //verhindert, dass ein Platz doppelt besetzt wird
- }
- //Nachbarn zählen
- for (int x = 0; x < counts.length; x++)
- {
- for (int y = 0; y < counts[0].length; y++)
- {
- if (counts[x][y] != MINE) //solange an der Stelle keine Mine ist, soll gezählt werden
- {
- int neighborcount = 0;
- if(x > 0 && y > 0 && counts[x-1][y-1] == MINE)
- {
- neighborcount++;
- }
- if(y > 0 && counts[x][y-1] == MINE)
- {
- neighborcount++;
- }
- if(x < counts.length - 1 && y > 0 && counts[x+1][y-1] == MINE)
- {
- neighborcount++;
- }
- if(x > 0 && counts[x-1][y] == MINE)
- {
- neighborcount++;
- }
- if ( x < counts.length -1 && counts[x+1][y] == MINE)
- {
- neighborcount++;
- }
- if (x > 0 && y < counts[0].length -1 && counts[x-1][y+1] == MINE)
- {
- neighborcount++;
- }
- if (y < counts[0].length -1 && counts[x][y+1] == MINE)
- {
- neighborcount++;
- }
- if (x < counts.length -1 && y < counts[0].length -1 && counts[x+1][y+1] == MINE)
- {
- neighborcount++;
- }
- counts[x][y] = neighborcount;
- }
- }
- }
- }
- public void lostGame()
- {
- for (int x = 0; x < buttons.length; x++) //geschachtelte For-Schleife
- {
- for (int y = 0; y < buttons[0].length; y++)
- {
- if (buttons[x][y].isEnabled()) //ist der Button an der Stelle [x][y] verfügbar
- {
- if(counts[x][y] != MINE) //befindet sich an der Position keine Mine, dann sollen alle Flächen aufgedeckt werden
- {
- buttons[x][y].setText(counts[x][y] + "");
- buttons[x][y].setEnabled(false);
- }
- else //andernfalls soll ein X angezeigt werden
- {
- buttons[x][y].setText("X");
- buttons[x][y].setEnabled(false);
- }
- }
- }
- }
- }
- public void clearZeros (ArrayList<Integer> toClear)
- {
- if(toClear.size() == 0)
- {
- return;
- }
- else
- {
- int x = toClear.get(0) / 100;
- int y = toClear.get(0) % 100;
- toClear.remove(0);
- if (counts[x][y] == 0)
- {
- if ( x > 0 && y > 0 && buttons[x-1][y-1].isEnabled()) // hoch links
- {
- buttons[x-1][y-1].setText(counts[x-1][y-1] + "");
- buttons[x-1][y-1].setEnabled(false);
- if (counts[x-1][y-1] == 0)
- {
- toClear.add((x-1) * 100 +(y-1));
- }
- }
- if (y > 0 && buttons[x][y-1].isEnabled()) //hoch
- {
- buttons[x][y-1].setText(counts[x][y-1] + "");
- buttons[x][y-1].setEnabled(false);
- if (counts[x][y-1] == 0)
- {
- toClear.add(x * 100 +(y-1));
- }
- }
- if (x < counts.length - 1 && y > 0 && buttons[x+1][y-1].isEnabled()) //hoch rechts
- {
- buttons[x+1][y-1].setText(counts[x+1][y-1] + "");
- buttons[x+1][y-1].setEnabled(false);
- if (counts[x+1][y-1] == 0)
- {
- toClear.add((x+1) * 100 +(y-1));
- }
- }
- if ( x > 0 && buttons[x-1][y].isEnabled())//links
- {
- buttons[x-1][y].setText(counts[x-1][y] + "");
- buttons[x-1][y].setEnabled(false);
- if (counts[x-1][y] == 0)
- {
- toClear.add((x-1) * 100 +y);
- }
- }
- if (x < counts.length - 1 && buttons[x+1][y].isEnabled()) //rechts
- {
- buttons[x+1][y].setText(counts[x+1][y] + "");
- buttons[x+1][y].setEnabled(false);
- if (counts[x+1][y] == 0)
- {
- toClear.add((x+1) * 100 +y);
- }
- }
- if ( x > 0 && y < counts[0].length -1 && buttons[x-1][y+1].isEnabled()) //runter links
- {
- buttons[x-1][y+1].setText(counts[x-1][y+1] + "");
- buttons[x-1][y+1].setEnabled(false);
- if (counts[x-1][y+1] == 0)
- {
- toClear.add((x-1) * 100 +(y+1));
- }
- }
- if (y < counts[0].length -1 && buttons[x][y+1].isEnabled()) //runter
- {
- buttons[x][y+1].setText(counts[x][y+1] + "");
- buttons[x][y+1].setEnabled(false);
- if (counts[x][y+1] == 0)
- {
- toClear.add((x) * 100 +(y+1));
- }
- }
- if (x < counts.length -1 && y < counts[0].length -1 && buttons[x+1][y+1].isEnabled()) //runter rechts
- {
- buttons[x+1][y+1].setText(counts[x+1][y+1] + "");
- buttons[x+1][y+1].setEnabled(false);
- if (counts[x+1][y+1] == 0)
- {
- toClear.add((x+1) * 100 +(y+1));
- }
- }
- }
- clearZeros(toClear);
- }
- }
- public void checkWin()
- {
- boolean won = true;
- for (int x = 0; x < counts.length; x++)
- {
- for (int y = 0; y < counts[0].length; y++)
- {
- if(counts[x][y] != MINE && buttons[x][y].isEnabled() == true)
- {
- won = false;
- }
- }
- }
- if (won == true)
- {
- JOptionPane.showMessageDialog(frame, "You win");
- }
- }
- @Override
- public void actionPerformed(ActionEvent event) {
- if (event.getSource().equals(reset))
- {
- for (int x = 0; x < buttons.length; x++)
- {
- for (int y = 0; y < buttons[0].length; y++)
- {
- buttons[x][y].setEnabled(true);
- buttons[x][y].setText("");
- }
- }
- createRandomMines();
- }
- else
- {
- for (int x = 0; x < buttons.length; x++)
- {
- for (int y = 0; y < buttons[0].length; y++)
- {
- if (event.getSource().equals(buttons[x][y]))
- {
- if(counts[x][y] == MINE)
- {
- buttons[x][y].setForeground(Color.red);
- buttons[x][y].setText("X");
- lostGame();
- }
- else if(counts[x][y] == 0)
- {
- buttons[x][y].setText(counts[x][y] + "");
- buttons[x][y].setEnabled(false);
- ArrayList <Integer> toClear = new ArrayList <Integer>();
- toClear.add(x*100+y);
- clearZeros(toClear);
- checkWin();
- }
- else
- {
- buttons[x][y].setText(counts[x][y] + "");
- buttons[x][y].setEnabled(false);
- checkWin();
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement