-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
261 lines (231 loc) · 7.41 KB
/
Game.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
/**
* @author Maxx Boehme
* @version 1
*
* Class used to manage the TicTacToe game.
*/
import java.awt.*;
import java.util.Random;
import java.util.concurrent.locks.*;
public class Game implements Runnable{
private Board board;
private Player[] players;
private int[] winPositon;
private GameState state;
private int currentplayer;
private int startingplayer;
private int firstplayer;
private int turn;
private Graphics g;
private Lock stateLock;
Game(Graphics g, Player p1, Player p2, int playerfirst){
this.g = g;
board = new Board(g, 100, 100, 300);
players = new Player[2];
players[0] = p1;
players[1] = p2;
firstplayer = playerfirst;
if(playerfirst != 2){
startingplayer = playerfirst;
currentplayer = playerfirst;
} else {
Random r = new Random();
startingplayer = r.nextInt(2);
currentplayer = startingplayer;
}
turn = 1;
stateLock = new ReentrantLock();
state = GameState.Clear;
}
public void changeState(GameState gs){
stateLock.lock();
this.state = gs;
stateLock.unlock();
}
public GameState getState(){
return state;
}
public void setPlayers(Player p1, Player p2){
players[0] = p1;
players[1] = p2;
}
private void computerTurn() {
if(players[currentplayer].isAI()){
this.changeState(GameState.Running);
ComputerAI ai = players[currentplayer].getAI();
Point p = ai.turn(this.board, players[currentplayer].getType(), this.turn);
board.setCellifEmpty(players[currentplayer], p.x, p.y);
turn++;
if((this.winPositon = board.hasWonArray(players[currentplayer], p.x, p.y)) != null){
this.changeState(GameState.NewGame);
players[currentplayer].win();
this.changePlayer();
players[currentplayer].lost();
System.out.println("Won");
} else if(turn >= 10) {
this.changeState(GameState.NewGame);
players[0].draw();
players[1].draw();
System.out.println("Draw");
} else {
this.changePlayer();
this.changeState(GameState.Turn);
}
} else {
this.changeState(GameState.Turn);
}
}
public boolean playerTurn(int x, int y){
boolean result = false;
if(x >= 0 && y >= 0 && x < 3 && y < 3){
if(state == GameState.Turn){
boolean w = board.setCellifEmpty(players[currentplayer], x, y);
if(w){
result = true;
this.turn++;
if((this.winPositon = board.hasWonArray(players[currentplayer], x, y)) != null){
this.changeState(GameState.NewGame);
players[currentplayer].win();
this.changePlayer();
players[currentplayer].lost();
System.out.println("Won");
} else if(turn >= 10) {
this.changeState(GameState.NewGame);
players[0].draw();
players[1].draw();
System.out.println("Draw");
} else {
this.changePlayer();
if(players[currentplayer].isAI()){
this.changeState(GameState.Running);
Thread aithread = new Thread(this);
aithread.start();
} else {
this.changeState(GameState.Turn);
}
}
}
}
}
return result;
}
private void changePlayer(){
this.currentplayer = (this.currentplayer + 1) % 2;
}
public void createNewGame(){
turn = 1;
board.clear();
if(firstplayer != 2){
startingplayer = firstplayer;
currentplayer = firstplayer;
} else {
startingplayer = (this.startingplayer + 1) % 2;
currentplayer = startingplayer;
}
if(players[currentplayer].isAI()){
this.changeState(GameState.Running);
Thread aithread = new Thread(this);
aithread.start();
} else {
this.changeState(GameState.Turn);
}
}
public void paint(){
this.g.clearRect(0, 0, 500, 450);
this.g.setColor(new Color(56, 168, 169));
this.g.fillRoundRect(100, 10, 300, 50, 25, 25);
this.g.setColor(Color.black);
this.g.fillRoundRect(103, 13, 294, 44, 25, 25);
this.g.setColor(new Color(21, 82, 83));
this.g.fillRoundRect(105, 15, 290, 40, 25, 25);
this.g.setColor(Color.white);
this.g.setFont(new Font("Ariel", Font.BOLD, 35));
this.g.drawString("Tic-Tac-Toe", 171, 48);
this.g.drawString("Tic-Tac-Toe", 169, 46);
this.g.drawString("Tic-Tac-Toe", 171, 46);
this.g.drawString("Tic-Tac-Toe", 169, 48);
this.g.setColor(Color.black);
this.g.setFont(new Font("Ariel", Font.BOLD, 35));
this.g.drawString("Tic-Tac-Toe", 170, 47);
this.board.paint();
if(this.state != GameState.Clear) {
if(this.state == GameState.NewGame && this.winPositon != null){
int cell_size = this.board.getCellSize();
int mid = cell_size /2;
int[] pos = this.board.getPosition();
g.setColor(Color.white);
Graphics2D g2d = (Graphics2D)g;
g.setColor(Color.white);
g2d.setStroke(new BasicStroke(14, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(this.winPositon[1]*cell_size + mid + pos[0], this.winPositon[0]*cell_size + mid + pos[1],
this.winPositon[3]*cell_size + mid + pos[0], this.winPositon[2]*cell_size + mid + pos[1]);
g.setColor(Color.black);
g2d.setStroke(new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(this.winPositon[1]*cell_size + mid + pos[0], this.winPositon[0]*cell_size + mid + pos[1],
this.winPositon[3]*cell_size + mid + pos[0], this.winPositon[2]*cell_size + mid + pos[1]);
g.setColor(Color.white);
g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(this.winPositon[1]*cell_size + mid + pos[0], this.winPositon[0]*cell_size + mid + pos[1],
this.winPositon[3]*cell_size + mid + pos[0], this.winPositon[2]*cell_size + mid + pos[1]);
}
this.g.setColor(Color.white);
this.g.setFont(new Font("Ariel", Font.BOLD, 12));
this.g.drawString("Draws", stringPositon("Draws", 250), 420);
this.g.drawString(players[0].getName(), stringPositon(players[0].getName(), 100), 420);
this.g.drawString(players[1].getName(), stringPositon(players[1].getName(), 400), 420);
this.g.setFont(new Font("Ariel", Font.BOLD, 20));
this.g.drawString(players[0].getWins()+"", stringPositon(players[0].getWins()+"", 100), 440);
this.g.drawString(players[0].getDraws()+"", stringPositon(players[0].getDraws()+"", 250), 440);
this.g.drawString(players[1].getWins()+"", stringPositon(players[1].getWins()+"", 400), 440);
this.g.setFont(new Font("Ariel", Font.BOLD, 12));
if(this.state != GameState.NewGame){
String turn = players[currentplayer].getName()+"'s turn";
this.g.drawString(turn, stringPositon(turn, 250), 80);
} else {
String turn = "Click for new Game";
this.g.drawString("Click for new Game", stringPositon(turn, 250), 80);
}
}
}
public void clearGame(){
this.changeState(GameState.Clear);
this.turn = 1;
board.clear();
}
private int stringPositon(String s, int x){
return x-(this.g.getFontMetrics().stringWidth(s)/2);
}
public void initNewGame(int playerfirst){
this.turn = 1;
board.clear();
this.players[0].clearScore();
this.players[1].clearScore();
firstplayer = playerfirst;
if(playerfirst != 2){
startingplayer = playerfirst;
currentplayer = playerfirst;
} else {
Random r = new Random();
startingplayer = r.nextInt(2);
currentplayer = startingplayer;
}
if(players[currentplayer].isAI()){
this.changeState(GameState.Running);
Thread aithread = new Thread(this);
aithread.start();
} else {
this.changeState(GameState.Turn);
}
//this.paint();
}
@Override
public void run() {
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.computerTurn();
}
}