summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Board.java221
-rw-r--r--src/BoardSquareButton.java202
-rw-r--r--src/Psymp7Main.java83
3 files changed, 506 insertions, 0 deletions
diff --git a/src/Board.java b/src/Board.java
new file mode 100644
index 0000000..69bc123
--- /dev/null
+++ b/src/Board.java
@@ -0,0 +1,221 @@
+import javax.swing.*;
+import java.util.ArrayList;
+import java.util.Random;
+
+public class Board {
+
+    private int x;
+    private int y;
+    private int mines;
+    private ArrayList<BoardSquareButton> buttons = new ArrayList<>();
+    private ArrayList<BoardSquareButton> investigateQueue = new ArrayList<>();
+
+    public Board(int x, int y, int mines)
+    {
+        this.x = x;
+        this.y = y;
+        this.mines = mines;
+
+        System.out.println("Board x: " + getColumns() + " y: " + getRows());
+
+        this.createButtons(x, y);
+        this.initialiseButtons();
+        this.createMines(mines);
+        this.updateSurrounding();
+    }
+
+    public void importButtons(JFrame frame)
+    {
+        for(BoardSquareButton button : buttons)
+        {
+            frame.add(button);
+        }
+    }
+
+    public void initialiseButtons()
+    {
+        for(BoardSquareButton button : buttons)
+        {
+            button.initialise();
+        }
+    }
+
+    public void finished()
+    {
+        for(BoardSquareButton button : buttons)
+        {
+            button.setInvestigated(true);
+        }
+    }
+
+    public void reset()
+    {
+        this.initialiseButtons();
+        this.createMines(mines);
+        this.updateSurrounding();
+    }
+
+    private void createButtons(int x, int y) {
+        for (int i = 1; i <= x; i++) {
+            for(int j = 1; j <= y; j++) {
+                this.storeButton(new BoardSquareButton(i, j));
+                System.out.println("Adding button with column: " + i + "and row: " + j);
+            }
+        }
+    }
+
+    private void createMines(int mines)
+    {
+        Random random = new Random();
+        System.out.println("Creating "+ mines + " mines!");
+        while(mines > 0)
+        {
+            int x = random.nextInt(this.getColumns()) + 1;
+            int y = random.nextInt(this.getRows()) + 1;
+
+            System.out.print("(Setting mines) Querying for button with x: " + y + "and y: " + x);
+
+            BoardSquareButton button = this.getButton(y, x);
+            if(!button.isMine())
+            {
+                button.setMine(true);
+                mines--;
+                System.out.println("Is now mine!");
+            }
+        }
+    }
+
+    public void investigateButton(BoardSquareButton button)
+    {
+        System.out.println("Investigating button with row: " + button.getRow() + " and column: " + button.getColumn());
+        if(button.getSurroundingMines() == 0 && !button.isMine())
+            investigateQueue.add(button);
+        preformInvestigation();
+        for(BoardSquareButton btn : investigateQueue)
+        {
+            btn.setInvestigated(true);
+        }
+        investigateQueue.clear();
+    }
+
+    private void preformInvestigation() {
+        System.out.println("Investigating...");
+        boolean done;
+        do {
+            done = true;
+            for (int i = 0; i < investigateQueue.size(); i++)
+            {
+                BoardSquareButton button = investigateQueue.get(i);
+                for (BoardSquareButton btn : getSurroundingButtons(button))
+                {
+                    if (!investigateQueue.contains(btn) && btn.getSurroundingMines() == 0 && !btn.isMine())
+                    {
+                        System.out.println("adding to the queue");
+                        investigateQueue.add(btn);
+                        done = false;
+                    }
+                }
+            }
+        }
+        while(!done);
+    }
+    public boolean hasWon()
+    {
+        boolean won = true;
+
+        for(BoardSquareButton button : buttons)
+        {
+            if(!button.isInvestigated() && !button.isMine())
+                won = false;
+        }
+
+        return won;
+    }
+
+    public boolean hasLost()
+    {
+        boolean lost = false;
+
+        for(BoardSquareButton button : buttons)
+        {
+            if(button.isInvestigated() && button.isMine())
+                lost = true;
+        }
+
+        return lost;
+    }
+
+    public ArrayList<BoardSquareButton> getSurroundingButtons(BoardSquareButton button)
+    {
+        ArrayList<BoardSquareButton> returnarray = new ArrayList<>();
+        try { returnarray.add(getButton(button.getColumn() - 1, button.getRow() - 1).getSelf()); } catch(NullPointerException e) {}
+        try { returnarray.add(getButton(button.getColumn(), button.getRow() - 1).getSelf()); } catch(NullPointerException e) {}
+        try { returnarray.add(getButton(button.getColumn() + 1, button.getRow() - 1).getSelf()); } catch(NullPointerException e) {}
+
+        try { returnarray.add(getButton(button.getColumn() - 1, button.getRow()).getSelf()); } catch(NullPointerException e) {}
+        try { returnarray.add(getButton(button.getColumn() + 1, button.getRow()).getSelf()); } catch(NullPointerException e) {}
+
+        try { returnarray.add(getButton(button.getColumn() - 1, button.getRow() + 1).getSelf()); } catch(NullPointerException e) {}
+        try { returnarray.add(getButton( button.getColumn(), button.getRow() + 1).getSelf()); } catch(NullPointerException e) {}
+        try { returnarray.add(getButton( button.getColumn() + 1, button.getRow() + 1).getSelf()); } catch(NullPointerException e) {}
+
+        return returnarray;
+    }
+
+    public int countSurrounding(BoardSquareButton button)
+    {
+        int count = 0;
+        for(BoardSquareButton btn : getSurroundingButtons(button))
+        {
+            if(btn.isMine())
+                count++;
+        }
+
+        System.out.println("Button with x: " + button.getRow() + " and y: " + button.getColumn() + " has " + count + " surrounding mines!");
+
+        return count;
+    }
+
+    public void updateSurrounding()
+    {
+        for(BoardSquareButton button : buttons)
+        {
+            System.out.println("Counting surrounding for button with x: " + button.getRow() + " and y: " + button.getColumn());
+            button.setSurroundingMines(countSurrounding(button));
+        }
+    }
+
+    /*
+        GETTERS AND SETTERS
+    */
+    public int getColumns()
+    {
+        return x;
+    }
+
+    public int getRows()
+    {
+        return y;
+    }
+
+    public ArrayList<BoardSquareButton> getButtons()
+    {
+        return buttons;
+    }
+
+    public BoardSquareButton getButton(int x, int y)
+    {
+        for(BoardSquareButton button : buttons)
+        {
+            if(button.getRow() == y && button.getColumn() == x)
+                return button;
+        }
+
+        return null;
+    }
+
+    public void storeButton(BoardSquareButton button)
+    {
+        buttons.add(button);
+    }
+}
diff --git a/src/BoardSquareButton.java b/src/BoardSquareButton.java
new file mode 100644
index 0000000..9c0a1e4
--- /dev/null
+++ b/src/BoardSquareButton.java
@@ -0,0 +1,202 @@
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+
+/*
+* TODO: move the mouselistener to main so we can call restart from there + we can investigate surrounding buttons from there
+*
+* */
+
+public class BoardSquareButton extends JButton {
+
+    public static final int DEFAULT_WIDTH = 100;
+    public static final int DEFAULT_HEIGHT = 100;
+    public static final String DEFAULT_TEXT = "?";
+    public static final String MINE_TEXT = "O";
+    public static final Color DEFAULT_COLOR = Color.GRAY;
+    public static final Color INVESTIGATED_COLOR = Color.GREEN;
+    public static final Color MINE_COLOR = Color.BLACK;
+    public static final Color POTENTIAL_COLOR = Color.RED;
+
+    private int width;
+    private int height;
+    private Color color;
+    private int x;
+    private int y;
+    private int surroundingMines;
+    private boolean mine;
+    private boolean investigated;
+    private boolean potential;
+
+    /*
+        CONSTRUCTORS
+    */
+
+    public BoardSquareButton(int x, int y, int width, int height, Color color)
+    {
+        this.x = x;
+        this.y = y;
+
+        initialise();
+
+        this.setDefaultDimension(width, height);
+        this.setColor(color);
+
+        this.addNewMouseListener();
+    }
+
+    public BoardSquareButton(int x, int y)
+    {
+        this.x = x;
+        this.y = y;
+
+        initialise();
+
+        this.addNewMouseListener();
+    }
+
+    private void addNewMouseListener()
+    {
+        super.addMouseListener(new Psymp7Main());
+    }
+
+    public void initialise()
+    {
+        this.setDefaultDimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
+        super.setFont(new Font("Ariel", Font.PLAIN, 40));
+        super.setText("?");
+        super.setBackground(DEFAULT_COLOR);
+        this.setColor(DEFAULT_COLOR);
+        this.setMine(false);
+        this.setInvestigated(false);
+        this.setPotential(false);
+    }
+
+    /*
+        GETTERS AND SETTERS
+    */
+    public BoardSquareButton getSelf()
+    {
+        return this;
+    }
+
+    public void setDefaultDimension(int x, int y)
+    {
+        this.setWidth(x);
+        this.setHeight(y);
+        super.setPreferredSize(new Dimension(x, y));
+    }
+
+    public int getWidth()
+    {
+        return width;
+    }
+
+    public void setWidth(int width)
+    {
+        this.width = width;
+    }
+
+    public int getHeight()
+    {
+        return height;
+    }
+
+    public void setHeight(int height)
+    {
+        this.height = height;
+    }
+
+    public int getSurroundingMines()
+    {
+        return surroundingMines;
+    }
+
+    public void setSurroundingMines(int mines)
+    {
+        this.surroundingMines = mines;
+    }
+
+    public Color getColor()
+    {
+        return color;
+    }
+
+    public void setColor(Color color)
+    {
+        this.color = color;
+        super.setBackground(color);
+    }
+
+    public int getRow()
+    {
+        return x;
+    }
+
+    public void setRow(int x) { this.x = x; }
+
+    public int getColumn()
+    {
+        return y;
+    }
+
+    public void setColumn(int y)
+    {
+        this.y = y;
+    }
+
+    public boolean isMine() {
+        return mine;
+    }
+
+    public void setMine(boolean mine)
+    {
+        this.mine = mine;
+    }
+
+    public boolean isInvestigated()
+    {
+        return investigated;
+    }
+
+    public void setInvestigated(boolean investigated)
+    {
+        this.investigated = investigated;
+        if (investigated)
+        {
+            this.setPotential(false);
+            if (mine)
+            {
+                this.setColor(MINE_COLOR);
+                this.setText(MINE_TEXT);
+            } else
+            {
+                this.setColor(INVESTIGATED_COLOR);
+                this.setText("" + this.getSurroundingMines());
+            }
+        }
+        else
+        {
+            this.setColor(DEFAULT_COLOR);
+            this.setText(DEFAULT_TEXT);
+        }
+    }
+    public boolean isPotential()
+    {
+        return potential;
+    }
+
+    public void setPotential(boolean potential)
+    {
+        this.potential = potential;
+        if(potential)
+            this.setColor(POTENTIAL_COLOR);
+        else
+            this.setColor(DEFAULT_COLOR);
+    }
+
+}
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