-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
188 lines (164 loc) · 6.07 KB
/
Board.java
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Board implements ActionListener
{
private JFrame frame;
private JPanel mainPanel;
private JPanel gamePanel;
private JPanel levelPanel;
private Options options;
private Squares squares;
//currentSquare and nextSquare hold positions of the squares user has clicked on, currentNext used to detirmine whether the first square has been selected
private int currentSquare[];
private int nextSquare[];
private int currentNext;
public Board()
{
//contructor
currentNext = 0;
currentSquare = new int[]{0,0};
nextSquare = new int[]{0,0};
frame = new JFrame();
frame.setTitle("Hoppers");
frame.setSize(800,800);
squares = new Squares();
gamePanel = new JPanel();
gamePanel = squares.getPanel();
options = new Options();
levelPanel = new JPanel();
levelPanel = options.getPanel();
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add("Center",gamePanel);
mainPanel.add("South",levelPanel);
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Adding action listeners to all buttons
for (int i=0; i<5; i++) {
for (int j=0; j< 5;j++){
squares.getSquare(new int[]{i,j}).getButton().addActionListener(this);
}
}
options.getNextLevelButton().addActionListener(this);
options.getPreviousLevelButton().addActionListener(this);
options.getResetButton().addActionListener(this);
options.getOpenButton().addActionListener(this);
options.openFile("Levels.txt");
squares.changeLevel(options.getCurrentLevel());
options.updateLevelButton();
}
private void reset()
{
//Reset level
options.inTheProcessOfWinning();
currentNext = 0;
squares.changeLevel(options.getCurrentLevel());
}
private void nextLevel()
{
//Go to next level
options.inTheProcessOfWinning();
currentNext = 0;
options.setLevel(options.getLevel() + 1);
squares.changeLevel(options.getCurrentLevel());
options.updateLevelButton();
}
private void previousLevel()
{
//Go to previous level
options.inTheProcessOfWinning();
currentNext = 0;
options.setLevel(options.getLevel() - 1);
squares.changeLevel(options.getCurrentLevel());
options.updateLevelButton();
}
private void openLevel()
{
//Open level that is typed into text field
//if user has entered invalid data into text field do nothing
try {
options.setLevel(Integer.parseInt(options.getLevelText()) - 1);
options.inTheProcessOfWinning();
currentNext = 0;
squares.changeLevel(options.getCurrentLevel());
options.updateLevelButton();
} catch (Exception e) {}
}
private void selectCurrent(int i,int j)
{
//Check if clicked on square is valid starter, if it is select it and update its image, and also store its position in currentSquare, change currentNext so that game knows a sqaure has already been clicked on
if (squares.getSquare(new int[]{i,j}).validStarter()){
currentSquare[0] = i;
currentSquare[1] = j;
squares.getSquare(currentSquare).setSelected(true);
squares.getSquare(currentSquare).update();
currentNext = 1;
}
}
private void selectNext(int i,int j)
{
//Check if nextSquare is a valid sqaure to jump to, if it is move, if currentSquare and nextSquare are the same, deselect currentSquare
nextSquare[0] = i;
nextSquare[1] = j;
if (squares.getSquare(nextSquare).validNext()){
move();
} else if (currentSquare[0] == nextSquare[0] && currentSquare[1] == nextSquare[1] ){
currentNext = 0;
squares.getSquare(currentSquare).setSelected(false);
squares.getSquare(currentSquare).update();
}
}
private void move()
{
//Work out position of square to be jumped over, check whether it a valid jump over, check whether the move is valid, if move is valid move
int[] midSquare = squares.getMidSquare(currentSquare, nextSquare);
if (squares.getSquare(midSquare).validMid()){
if (squares.validMove(currentSquare,nextSquare)){
squares.getSquare(currentSquare).moveTo( squares.getSquare(nextSquare), squares.getSquare(midSquare));
currentNext = 0;
checkWinner();
}
}
}
private void checkWinner()
{
// Loops around all sqaures checking if there is a single green frog
int total = 0;
for (int i=0; i<5; i++) {
for (int j=0; j< 5;j++){
if (squares.getSquare(new int[]{i,j}).getState() == '2'){
total++;
}
}
}
if (total == 0){
options.winner();
}
}
public void actionPerformed(ActionEvent e)
{
//checks source of all buttons
if (e.getSource() == options.getResetButton()){
reset();
} else if (e.getSource() == options.getNextLevelButton()) {
nextLevel();
} else if (e.getSource() == options.getPreviousLevelButton()) {
previousLevel();
}else if (e.getSource() == options.getOpenButton()) {
openLevel();
} else {
for (int i=0; i<5; i++) {
for (int j=0; j< 5;j++){
if (e.getSource() == squares.getSquare(new int[]{i,j}).getButton())
if (currentNext == 0){
selectCurrent(i, j);;
} else {
selectNext(i,j);
}
}
}
}
}
}