-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.java
More file actions
310 lines (268 loc) · 9.2 KB
/
Copy pathGameManager.java
File metadata and controls
310 lines (268 loc) · 9.2 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.example.itay.sumika;
import android.graphics.Point;
import android.os.CountDownTimer;
import android.widget.Button;
import java.util.ArrayList;
public class GameManager {
GameLogic gameLogic;
CountDownTimer timer;
int validSpace, timeForMove;
boolean getUserClicks = true, onePlayer;
int compLevel;
String colorP1, colorP2;
GameScreen gameScreen;
Player player1, player2, lastPlayerPlayed;
final int numOfplayers = 2;
Point lastChoice;
CompAI comp;
int cells[][];
int numOfClick = 0;// should it be static?
private int turn = 0;//1=player 1, 2=player 2
public GameManager() {
}
GameManager(String colorP1, String colorP2, int[][] cells, GameScreen gameScreen, boolean onePlayer, int compLevel, int validSpace, int timeForMove) {
this.colorP1 = colorP1;
this.colorP2 = colorP2;
this.player1 = new Player(1, getImageId(colorP1));
this.player2 = new Player(2, getImageId(colorP2));
lastChoice = new Point(-100, -100);//initialize unrelevant value- to let the user choose first square freely.
this.cells = cells;
this.compLevel = compLevel;
this.validSpace = validSpace;
this.timeForMove = timeForMove;
this.onePlayer = onePlayer;
this.gameScreen = gameScreen;
this.comp = new CompAI(cells, player2);
}
public static int[][] deepCopyIntMatrix(int[][] input) {
if (input == null)
return null;
int[][] result = new int[input.length][];
for (int r = 0; r < input.length; r++) {
result[r] = input[r].clone();
}
return result;
}
public void game(Point p) {
switch (turn % numOfplayers) {
case 0: { //first player turn
if (!checkEndOfTheGame(cells,lastChoice,validSpace)) {
if (isValid(cells,p,lastChoice,validSpace)) {
pauseGame(250);
if(!(numOfClick==0&&checkIfWiningMove(p,cells,validSpace))) {//make sure the user/comp wont win on first move !
addChoice(p);
drawTheMove(p,numOfClick,getLastPlayerPlay());
changeTurn();
if (checkEndOfTheGame(cells,lastChoice,validSpace)) {
announceWinner();
}
}
}
else{ //TODO check if worked
if(!onePlayer)
changeTurn();
}
} else {
announceWinner();
}
break;
}
case 1: {//when player 2 or the comp playing
if (isValid(cells,p,lastChoice,validSpace)) {
pauseGame(250);
addChoice(p);
drawTheMove(p,numOfClick,getLastPlayerPlay());
changeTurn();
}
else{
if(!onePlayer)
changeTurn();
}
if (checkEndOfTheGame(cells,lastChoice,validSpace)) {
announceWinner();
}
break;
}
}
}
public void pauseGame(int timeInMillis){
try {
Thread.sleep(timeInMillis);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void changeTurn() {
if (!onePlayer) {
turn++;
gameScreen.changeImageTurn(turn);
timerCounter();
if (turn % numOfplayers == 1 && compLevel != 0) {
getUserClicks = false;
comp.test2(this, player2, compLevel, cells, lastChoice, validSpace);
} else
getUserClicks = true;
}
else {//TODO check if worked onr player
timerCounter();
}
}
public void addChoice(Point p) {//the function that starts the move
numOfClick++;
cells[p.x][p.y]=numOfClick;
lastChoice = p;
}
public Player getLastPlayerPlay() {
switch (turn % 2) {
case 0:
lastPlayerPlayed = player2;
break;
case 1:
lastPlayerPlayed = player1;
break;
}
return lastPlayerPlayed;
}
public void announceWinner() {
int winNum;
int resForAnnounce1,resForAnnounce2;
if (!onePlayer) // if its one player- the turn need to stay on pink
gameScreen.changeImageTurn(turn+1);
if(timer!=null)
timer.cancel();
if (!onePlayer) {
winNum=getLastPlayerPlay().getNumOfPlayer();
resForAnnounce1=R.string.two_plater_announce1;
resForAnnounce2=R.string.two_plater_announce2;
gameScreen.setAnnounceTv2Players(resForAnnounce1,resForAnnounce2,winNum);
}
else{
gameScreen.setAnnounceTv1Player(R.string.single_player_announce,numOfClick);
}
}
private int getImageId(String colorP) {
int imageId = -1;
switch (colorP) {
case "blue":
imageId = R.drawable.btnshape_game_blue;
break;
case "pink":
imageId = R.drawable.btnshape_game_pink;
break;
}
return imageId;
}
public int getNumOfClick() {
return numOfClick;
}
public void timerCounter() {
if(timer!=null)
timer.cancel();
if (timeForMove != 0) {
timer = new CountDownTimer(timeForMove*1000, 1000) {
public void onTick(long millisUntilFinished) {
long secondsUntilFinished=millisUntilFinished/1000 + 1;
gameScreen.setTimeTv((int)secondsUntilFinished);
}
public void onFinish() {
getUserClicks = false;
gameScreen.setTimeTv(0);
announceWinner();
}
}.start();
}
}
/** using gameLogic **/
public boolean checkEndOfTheGame(int cells[][],Point lastChoice, int validSpace){
return gameLogic.checkEndOfGame(cells,lastChoice,validSpace);
}
public boolean isValid(int cells[][],Point p,Point lastChoice, int validSpace){
return gameLogic.isValid(cells,p,lastChoice,validSpace);
}
public boolean checkIfWiningMove(Point p,int cells[][], int validSpace){
int[][] dpOfCellsInInt=deepCopyIntMatrix(cells);
return gameLogic.checkIfWiningMove(p,dpOfCellsInInt,validSpace);
}
/** using gameLogic **/
/** using board **/
public void drawTheMove(Point p, int numOfClick, Player lastPlayerPlayed) {
gameScreen.board.drawTheMove(p,numOfClick,lastPlayerPlayed);
}
/** using board **/
public int addTurn() {
this.turn++;
return turn;
}
public int getTurn() {
return turn;
}
public void setGetUserClicks(boolean getUserClicks) {
this.getUserClicks = getUserClicks;
}
public void setOnePlayer(boolean onePlayer) {
this.onePlayer = onePlayer;
}
public void setColorP1(String colorP1) {
this.colorP1 = colorP1;
}
public void setColorP2(String colorP2) {
this.colorP2 = colorP2;
}
public void setGameScreen(GameScreen gameScreen) {
this.gameScreen = gameScreen;
}
public void setPlayer1(Player player1) {
this.player1 = player1;
}
public void setPlayer2(Player player2) {
this.player2 = player2;
}
public void setLastChoice(Point lastChoice) {
this.lastChoice = lastChoice;
}
public void setComp(CompAI comp) {
this.comp = comp;
}
public void setCells(int[][] cells) {
this.cells = cells;
}
public void setNumOfClick(int numOfClick) {
this.numOfClick = numOfClick;
}
public void setCompLevel(int compLevel) {
this.compLevel = compLevel;
}
public void setTurn(int turn) {
this.turn = turn;
}
public void setTimer(CountDownTimer timer) {
this.timer = timer;
}
public void setValidSpace(int validSpace) {
this.validSpace = validSpace;
}
public void setTimeForMove(int timeForMove) {
this.timeForMove = timeForMove;
}
public GameManager getDeepCopy() {
GameManager deepCopygm = new GameManager();
deepCopygm.setCells(cells);
deepCopygm.setColorP1(colorP1);
deepCopygm.setColorP2(colorP2);
deepCopygm.setComp(comp);
deepCopygm.setGameScreen(gameScreen);
deepCopygm.setGetUserClicks(getUserClicks);
deepCopygm.setCompLevel(compLevel);
deepCopygm.setLastChoice(lastChoice);
deepCopygm.setNumOfClick(numOfClick);
deepCopygm.setOnePlayer(onePlayer);
deepCopygm.setPlayer1(player1);
deepCopygm.setPlayer2(player2);
deepCopygm.setTurn(turn);
deepCopygm.setValidSpace(validSpace);
deepCopygm.setTimeForMove(timeForMove);
deepCopygm.setTimer(timer);
return deepCopygm;
}
}