-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
287 lines (255 loc) · 7.87 KB
/
Matrix.java
File metadata and controls
287 lines (255 loc) · 7.87 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Matrix extends JPanel implements ActionListener{
private final int ROWS = 6; // rows in grid
private final int COLUMNS = 7; // columns in grid
JButton clear = new JButton("Clear"); // clear grid button
ArrayList<JButton> Buttons = new ArrayList<>(); // column buttons from 1 to 7
private final Color P1 = Color.RED;
private final Color P2 = Color.YELLOW;
private Color currentColor = P1; // current player, starts with player 1
private Cell [][] grid = new Cell[ROWS+2][COLUMNS]; // checker's grid
private JLabel title = new JLabel("Player 1's turn");
public Matrix()
{
setLayout(new BorderLayout());
Cell c;
JPanel buttonsPanel = new JPanel(); // column buttons panel
JPanel gridPanel = new JPanel(); // grid panel
JPanel clearPanel = new JPanel(); // clear button panel
JPanel allButtons = new JPanel(); // column + clear buttons panel
clear.addActionListener(this);
buttonsPanel.setLayout(new GridLayout(1,7));
gridPanel.setLayout(new GridLayout(ROWS,COLUMNS));
allButtons.setLayout(new BorderLayout());
for(int i=0; i<ROWS; i++) // generates matrix and adds to panel
{
for(int j =0; j<COLUMNS ; j++) {
c = new Cell();
grid[i][j] = c;
gridPanel.add(c);
}
}
for(int i=0; i<COLUMNS; i++) // generates buttons and adds to array list
{
Buttons.add(new JButton(String.valueOf(i+1)));
Buttons.get(i).addActionListener(this);
buttonsPanel.add(Buttons.get(i));
}
clearPanel.add(clear); // adds clear button to panel
allButtons.add(buttonsPanel,BorderLayout.NORTH); // adds column buttons panel to all buttons panel
allButtons.add(clearPanel,BorderLayout.SOUTH); // adds clear buttons panel to all buttons panel
add(title,BorderLayout.NORTH);
title.setHorizontalAlignment(SwingConstants.CENTER);
add(gridPanel,BorderLayout.CENTER); // adds grid to panel
add(allButtons,BorderLayout.SOUTH); // adds all buttons to panel
}
public void actionPerformed(ActionEvent e)
{
int k=-5;
for(int i=0;i<Buttons.size();i++) // checking which button was pressed
{
if(e.getSource()==Buttons.get(i)) {
k = i;
break;
}
}
if(k>=0) // adding a checker in this column
{
addChecker(k);
}
else if(e.getSource()==clear) // clear grid was pressed
{
clearGrid();
}
}
public void addChecker(int column)
{
boolean won;
int row = ROWS-1;
int option;
String winner ="";
while (row>=0)
{
if(grid[row][column].getColor() == Color.WHITE)
{
grid[row][column].setColor(currentColor);
break;
}
else row--;
}
won = checkWin(row,column);
if(won)
{
if(currentColor == P1) // checking who won
{
winner = "Player 1 WON";
}
else
winner = "Player 2 WON";
option = JOptionPane.showConfirmDialog(null, "Do you want to play" +
" again?",winner,JOptionPane.YES_NO_OPTION);
if(option == 0) // user wants to play again
clearGrid();
else System.exit(0);
}
if(row == 0) // we filled the last place in this column, disable the button
blockButton(column);
if(!checkEnabled() && !won) // all buttons are disabled = grid is full and there's no win = Draw
{
option = JOptionPane.showConfirmDialog(null, "Do you want to play" +
" again?","It's a DRAW",JOptionPane.YES_NO_OPTION);
if(option == 0) // wants to play again
clearGrid();
else System.exit(0);
}
switchPlayer();
}
public void clearGrid()
{
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLUMNS; j++)
{
grid[i][j].setColor(Color.WHITE); // initializing all cells
}
}
enableButtons();
}
private void switchPlayer() // switching between colors + title at the end of a turn
{
if(currentColor == P1) {
title.setText("Player 2's turn");
currentColor = P2;
}
else {
title.setText("Player 1's turn");
currentColor = P1;
}
}
private boolean checkWin(int row, int column) // checking if there's a strike
{
if(checkRow(row, column)) // check rows win
{
return true;
}
if(checkColumn(row, column)) // check column win
{
return true;
}
if(checkDiagonal(row, column)) // check diagonal win
{
return true;
}
return false;
}
private void blockButton(int i)
{
Buttons.get(i).setEnabled(false); // when a column is full, blocking the option to fill it
}
private boolean checkRow(int row, int column) // checking row strike
{
int count = 0;
int checkFrom = column-3;
int checkTo = column+3;
if(checkFrom<0)
checkFrom = 0;
if(checkTo>=COLUMNS)
checkTo= COLUMNS-1;
for(int i = checkFrom; i<=checkTo ; i++)
{
if(grid[row][i].getColor()==currentColor)
{
count++;
if (count == 4)
return true;
}
else count = 0;
}
return false;
}
private boolean checkColumn(int row, int column) // checking column strike
{
int count = 0;
int checkFrom = row-3;
int checkTo = row+3;
if(checkFrom<0)
checkFrom = 0;
if(checkTo>=ROWS)
checkTo= ROWS-1;
for(int i = checkFrom; i<=checkTo ; i++)
{
if(grid[i][column].getColor()==currentColor)
{
count++;
if (count == 4)
return true;
}
else count = 0;
}
return false;
}
private boolean checkDiagonal(int row, int column) // checking diagonal strike
{
int c = column; // column to check from
int r = row; // row to check from
int count = 0;
while(c>0 && r>0)
{
c--;
r--;
}
while(c<COLUMNS && r<ROWS)
{
if(grid[r][c].getColor()==currentColor)
{
count++;
if (count == 4)
return true;
}
else count = 0;
c++;
r++;
}
c = column;
r = row;
count = 0;
while(c>0 && r<ROWS-1)
{
c--;
r++;
}
while(c<COLUMNS && r>=0)
{
if(grid[r][c].getColor()==currentColor)
{
count++;
if (count == 4)
return true;
}
else count = 0;
c++;
r--;
}
return false;
}
private boolean checkEnabled() // checking if some buttons are enabled
{
for(int i=0 ; i<Buttons.size() ; i++)
{
if(Buttons.get(i).isEnabled())
return true;
}
return false; // all buttons are disabled
}
private void enableButtons() // enabling all buttons after draw
{
for(int i=0 ; i<Buttons.size() ; i++)
{
Buttons.get(i).setEnabled(true);
}
}
}