summary refs log tree commit diff
path: root/src/Board.java
blob: 69bc123cce08e91912e107181755e94c28212252 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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);
    }
}