summary refs log tree commit diff
path: root/src/Psymp7Main.java
blob: d3d5ce699154cf8c1bd78e6b6bcc3d8c1e3e098c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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) {}
}