-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
266 lines (230 loc) · 7.86 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
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
/**
* @author Maxx Boehme
* @version 1
*
* Class is used to create and maintain the playing board.
*/
import java.awt.*;
import java.util.concurrent.locks.*;
public class Board {
private final int ROWS = 3;
private final int COLUMS = 3;
private final int SIZE;
private final int Cell_SIZE;
private int[] pos;
private Cell[][] board;
private Graphics g;
private Lock m;
Board(Graphics g, int posx, int posy, int size) {
board = new Cell[ROWS][COLUMS];
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLUMS; j++){
board[i][j] = new Cell();
}
}
pos = new int[2];
pos[0] = posx;
pos[1] = posy;
this.SIZE = size;
this.Cell_SIZE = this.SIZE / this.ROWS;
this.g = g;
m = new ReentrantLock();
this.clear();
}
public int[] getPosition(){
return this.pos;
}
public int getCellSize() {
return this.Cell_SIZE;
}
public Cell getCell(int x, int y){
return board[x][y];
}
public Token getCellType(int x, int y){
return board[x][y].getType();
}
public boolean hasWon(Player p, int x, int y){
Token t = p.getType();
boolean result =false;
int mid = (this.SIZE / this.ROWS) / 2;
m.lock();
g.setColor(Color.white);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
int rowbegin = 0;
int rowend = 0;
int colbegin = 0;
int colend = 0;
if(board[x][0].getType() == t && board[x][1].getType() == t && board[x][2].getType() == t){
rowbegin = rowend = x;
colbegin = 0;
colend = 2;
result = true;
} else if(board[0][y].getType() == t && board[1][y].getType() == t && board[2][y].getType() == t){
colbegin = colend = y;
rowbegin = 0;
rowend = 2;
result = true;
} else if(board[0][0].getType() == t && board[1][1].getType() == t && board[2][2].getType() == t){
rowbegin = colbegin = 0;
rowend = colend = 2;
result = true;
} else if(board[0][2].getType() == t && board[1][1].getType() == t && board[2][0].getType() == t){
rowend = colbegin = 2;
rowbegin = colend = 0;
result = true;
}
if(result){
g2d.drawLine(colbegin*this.Cell_SIZE + mid + pos[0], rowbegin*this.Cell_SIZE + mid + pos[1],
colend*this.Cell_SIZE + mid + pos[0], rowend*this.Cell_SIZE + mid + pos[1]);
}
m.unlock();
return result;
}
public int[] hasWonArray(Player p, int x, int y){
Token t = p.getType();
int[] result = new int[4];
m.lock();
g.setColor(Color.white);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
if(board[x][0].getType() == t && board[x][1].getType() == t && board[x][2].getType() == t){
result[0] = result[2] = x;
result[1] = 0;
result[3] = 2;
} else if(board[0][y].getType() == t && board[1][y].getType() == t && board[2][y].getType() == t){
result[1] = result[3] = y;
result[0] = 0;
result[2] = 2;
} else if(board[0][0].getType() == t && board[1][1].getType() == t && board[2][2].getType() == t){
result[0] = result[1] = 0;
result[2] = result[3] = 2;
} else if(board[0][2].getType() == t && board[1][1].getType() == t && board[2][0].getType() == t){
result[2] = result[1] = 2;
result[0] = result[3] = 0;
} else {
result = null;
}
m.unlock();
return result;
}
public void clear(){
m.lock();
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLUMS; j++){
this.board[i][j].setType(Token.Empty);
}
}
m.unlock();
}
public boolean setCell(Player p, int x, int y){
boolean result = false;
m.lock();
if(x >= 0 && x < ROWS && y >= 0 && y < COLUMS){
board[x][y].set(p.getType(), p.getColor());
result = true;
}
m.unlock();
return result;
}
public boolean setCellifEmpty(Player p, int x, int y){
boolean result = false;
m.lock();
if(x >= 0 && x < ROWS && y >= 0 && y < COLUMS && board[x][y].getType() == Token.Empty){
board[x][y].set(p.getType(), p.getColor());
result = true;
}
m.unlock();
return result;
}
public String toString(){
String result = "";
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLUMS; j++){
result = result.concat(board[i][j]+" ");
}
result = result.concat("\n");
}
return result;
}
public void paint() {
m.lock();
g.setColor(Color.LIGHT_GRAY);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
for(int i = 1; i < ROWS; i++){
int x = this.pos[0] + i * (SIZE/ ROWS);
g.setColor(Color.white);
g2d.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(x, this.pos[1], x, this.pos[1]+this.SIZE);
}
for(int i = 1; i < COLUMS; i++){
int y = this.pos[1] + i *(SIZE/COLUMS);
g.setColor(Color.white);
g2d.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(this.pos[0], y, this.pos[0]+this.SIZE, y);
}
for(int i = 1; i < ROWS; i++){
int x = this.pos[0] + i * (SIZE/ ROWS);
g.setColor(Color.black);
g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(x, this.pos[1], x, this.pos[1]+this.SIZE);
}
for(int i = 1; i < COLUMS; i++){
int y = this.pos[1] + i *(SIZE/COLUMS);
g.setColor(Color.black);
g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(this.pos[0], y, this.pos[0]+this.SIZE, y);
}
for(int i = 1; i < ROWS; i++){
int x = this.pos[0] + i * (SIZE/ ROWS);
g.setColor(Color.LIGHT_GRAY);
g2d.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(x, this.pos[1], x, this.pos[1]+this.SIZE);
}
for(int i = 1; i < COLUMS; i++){
int y = this.pos[1] + i *(SIZE/COLUMS);
g.setColor(Color.LIGHT_GRAY);
g2d.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(this.pos[0], y, this.pos[0]+this.SIZE, y);
}
int cell_padding = 20;
int cell_size = (SIZE/ROWS);
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLUMS; j++){
int x = j * cell_size + cell_padding +pos[1];
int y = i * cell_size + cell_padding + pos[1];
if(board[i][j].getType() == Token.X){
g2d.setColor(board[i][j].getColor());
int x2 = (j + 1) * cell_size - cell_padding + pos[0];
int y2 = (i + 1) * cell_size - cell_padding + pos[1];
g2d.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.setColor(Color.white);
g2d.drawLine(x, y, x2, y2);
g2d.drawLine(x2, y, x, y2);
g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.setColor(Color.black);
g2d.drawLine(x, y, x2, y2);
g2d.drawLine(x2, y, x, y2);
g2d.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.setColor(board[i][j].getColor());
g2d.drawLine(x, y, x2, y2);
g2d.drawLine(x2, y, x, y2);
} else if(board[i][j].getType() == Token.O){
g2d.setColor(board[i][j].getColor());
int symbol_size = cell_size - cell_padding * 2;
g2d.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.setColor(Color.white);
g2d.drawOval(x, y, symbol_size, symbol_size);
g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.setColor(Color.black);
g2d.drawOval(x, y, symbol_size, symbol_size);
g2d.setColor(board[i][j].getColor());
g2d.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawOval(x, y, symbol_size, symbol_size);
}
}
}
m.unlock();
}
}