-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBoard.java
150 lines (138 loc) · 5.01 KB
/
GameBoard.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameBoard extends JFrame implements ActionListener {
Color[] colours;
JButton[][] buttons;
int numClicks;
public GameBoard(String title, int columns, int rows) {
//constructor takes in game title and number of columns and rows for the coloured squares
//adds components to the GUI
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
int width = (columns*50)+((columns-1)*5)+35;
int height = (rows*50)+((rows-1)*5)+120;
setSize(width, height);
setLayout(null);
colours = new Color[] {new Color(18,180,180), new Color(222,31,98), new Color(186,9,244), new Color(10,28,243)};
JPanel p = new JPanel();
p.setLocation(10, 10+(55*rows));
p.setSize((55*columns),60);
p.setBackground(Color.white);
add(p);
JLabel rowLabel = new JLabel("Row: ");
p.add(rowLabel);
JLabel colLabel = new JLabel("Column: ");
p.add(colLabel);
JLabel numLabel = new JLabel("Moves: ");
p.add(numLabel);
buttons = new JButton[rows][columns];
this.initializeBoard();
}
public void initializeBoard() {
//sets up the inital state of the board
int r_counter = 0;
int c_counter = 0;
while (c_counter < buttons[0].length) {
while (r_counter < buttons.length) {
buttons[r_counter][c_counter] = new JButton();
buttons[r_counter][c_counter].setLocation(10+(55*c_counter),10+(55*r_counter));
buttons[r_counter][c_counter].setSize(50,50);
buttons[r_counter][c_counter].setBackground(setColour());
add(buttons[r_counter][c_counter]);
buttons[r_counter][c_counter].addActionListener(this);
r_counter++;
}
r_counter = 0;
c_counter++;
}
}
public void resetBoard() {
//resets the board to random colours and resets counters
for(int row=0; row<buttons.length; row++) {
for (int col=0; col<buttons[0].length; col++) {
buttons[row][col].setBackground(setColour());
}
}
JPanel panel = (JPanel)this.getContentPane().getComponent(0);
Component[] components = panel.getComponents();
JLabel rowLabel = (JLabel)components[0];
JLabel colLabel = (JLabel)components[1];
JLabel numLabel = (JLabel)components[2];
rowLabel.setText("Row: ");
colLabel.setText("Column: ");
numLabel.setText("Moves: ");
numClicks = 0;
}
private Color setColour() {
//returns a random colour from the colours array
int r = (int)(Math.random()*4);
return colours[r];
}
public void actionPerformed(ActionEvent event){
//controls actions when a button is clicked including changing counters and colours
numClicks++;
JPanel panel = (JPanel)this.getContentPane().getComponent(0);
Component[] components = panel.getComponents();
JLabel rowLabel = (JLabel)components[0];
JLabel colLabel = (JLabel)components[1];
JLabel numLabel = (JLabel)components[2];
for(int row=0; row<buttons.length; row++) {
for (int col=0; col<buttons[0].length; col++) {
if (event.getSource() == buttons[row][col]) {
rowLabel.setText("Row: "+(row+1));
colLabel.setText("Column: "+(col+1));
numLabel.setText("Moves: "+numClicks);
this.toggleColour(buttons[row][col]);
if((row-1) >= 0)
this.toggleColour(buttons[row-1][col]);
if((row+1) < buttons.length)
this.toggleColour(buttons[row+1][col]);
if((col-1) >= 0)
this.toggleColour(buttons[row][col-1]);
if((col+1) < buttons[0].length)
this.toggleColour(buttons[row][col+1]);
if(isComplete()) {
this.displayVictoryFrame();
this.resetBoard();
}
}
}
}
}
public void toggleColour(JButton b) {
//changes the colour of a button parameter passed in to the next colour in the colours array
for (int i=0;i<colours.length;i++) {
if (b.getBackground() == colours[i]){
switch (i) {
case 0: b.setBackground(colours[1]); break;
case 1: b.setBackground(colours[2]); break;
case 2: b.setBackground(colours[3]); break;
case 3: b.setBackground(colours[0]); break;
}
break;
}
}
}
public boolean isComplete() {
//checks to see if all buttons on the board are the same colour, if so returns true, else returns false
boolean flag = true;
Color topCorner = buttons[0][0].getBackground();
for(int row=0; row<buttons.length; row++) {
for (int col=0; col<buttons[0].length; col++) {
if (buttons[row][col].getBackground() != topCorner)
flag = false;
}
}
return flag;
}
public void displayVictoryFrame() {
//displays the victory frame
VictoryFrame v = new VictoryFrame("Victory!", numClicks);
v.setVisible(true);
}
public static void main(String[] args) {
GameBoard frame = new GameBoard("Colour Game", 4, 4) ;
frame.setVisible(true);
}
}