summary refs log tree commit diff
path: root/src/Psymp7Main.java
diff options
context:
space:
mode:
authorManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2020-09-30 00:08:24 +0100
committerManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2020-09-30 00:08:24 +0100
commitc3797ca7acf4b4c089c08523e78072f9aef5f8a3 (patch)
treef750cfda0f16e251390ff347bea71175d389b401 /src/Psymp7Main.java
downloadMinesweeper-c3797ca7acf4b4c089c08523e78072f9aef5f8a3.tar.gz
Minesweeper-c3797ca7acf4b4c089c08523e78072f9aef5f8a3.tar.bz2
Minesweeper-c3797ca7acf4b4c089c08523e78072f9aef5f8a3.zip
Release HEAD master
Diffstat (limited to 'src/Psymp7Main.java')
-rw-r--r--src/Psymp7Main.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/Psymp7Main.java b/src/Psymp7Main.java
new file mode 100644
index 0000000..d3d5ce6
--- /dev/null
+++ b/src/Psymp7Main.java
@@ -0,0 +1,83 @@
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+public class Psymp7Main implements MouseListener
+{
+    public final static int WIDTH = 600;
+    public final static int HEIGHT = 600;
+    public final static int NUM_BUTTONS_X = 6;
+    public final static int NUM_BUTTONS_Y = 6;
+    public final static int NUM_MINES = 5;
+
+    private static Board board;
+
+    public static void main(String[] args) {
+        new Psymp7Main().start();
+    }
+
+    public void start() {
+        if(NUM_MINES > (NUM_BUTTONS_X * NUM_BUTTONS_Y))
+        {
+            System.out.println("Too many mines!");
+            return;
+        }
+        JFrame mainframe = new JFrame("MineSweeper");
+
+        mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        mainframe.setResizable(false);
+        mainframe.setSize(WIDTH, HEIGHT);
+        mainframe.setLayout(new GridLayout(NUM_BUTTONS_Y, NUM_BUTTONS_X));
+
+        board = new Board(NUM_BUTTONS_Y, NUM_BUTTONS_X, NUM_MINES);
+        board.importButtons(mainframe);
+        mainframe.pack();
+        mainframe.setVisible(true);
+    }
+
+    @Override
+    public void mousePressed(MouseEvent mouseEvent)
+    {
+        BoardSquareButton source = (BoardSquareButton) mouseEvent.getSource();
+
+        if (mouseEvent.getButton() == MouseEvent.BUTTON1) // left click
+        {
+            source.setInvestigated(true);
+            if(board.hasWon())
+            {
+                board.finished();
+                JOptionPane.showMessageDialog(null, "You won!");
+                board.reset();
+            }
+            else if(board.hasLost())
+            {
+                board.finished();
+                JOptionPane.showMessageDialog(null, "You loose!");
+                board.reset();
+            }
+            else if(source.getSurroundingMines() == 0 && !source.isMine())
+            {
+                board.investigateButton(source);
+            }
+            System.out.println("Clicked in x: " + source.getColumn() + " y: " + source.getRow());
+        }
+        if (mouseEvent.getButton() == MouseEvent.BUTTON3)//right click
+        {
+            if(!source.isInvestigated())
+                source.setPotential(!source.isPotential());
+        }
+    }
+
+    @Override
+    public void mouseClicked(MouseEvent mouseEvent) {}
+
+    @Override
+    public void mouseReleased(MouseEvent mouseEvent) {}
+
+    @Override
+    public void mouseEntered(MouseEvent mouseEvent) {}
+
+    @Override
+    public void mouseExited(MouseEvent mouseEvent) {}
+}
\ No newline at end of file