-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControl.java
193 lines (179 loc) · 5.33 KB
/
Control.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
import java.util.Random;
public class Control {
Model model;
View view;
Random zufall = new Random();
/** Constructor of the class Control
*
* @throws InterruptedException
*/
public Control() throws InterruptedException {
model = new Model();
view = new View(model, this);
restart();
}
/**
* Method that returns the Model
* @return the model
*/
public Model getModel() {
return model;
}
/**
* Function that sets the Model
* @param model the model to set
*/
public void setModel(Model model) {
this.model = model;
}
/**
* Function that returns the View
* @return the view
*/
public View getView() {
return view;
}
/**
* Function that sets the View
* @param view the view to set
*/
public void setView(View view) {
this.view = view;
}
/**
* Function that checks if someone wins the game
* @return checkWinner
*/
public boolean checkWinner() {
if(calculate() == 11 || calculate() == 12) {
if(calculate() == 11) model.setWinsX(model.getWinsX()+1);
if(calculate() == 12) model.setWinsO(model.getWinsO()+1);
view.setButtonEditable(false);
return true;
}
return false;
}
/**
* Function that checks the board for a draw
* @return
*/
public boolean checkDraw() {
if(calculate() == 1) {
view.setButtonEditable(false);
return true;
}
return false;
}
/**
* Function that switches the player after his turn
*/
public void changePlayer() {
model.setCurrentPlayer1(!model.isCurrentPlayer1());
}
/**
* Function that restarts the game
*/
public void restart() {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
model.setFeld(0, i, j);
}
}
handleAI();
view.setButtonEditable(true);
zufall.nextBoolean();
model.setCurrentPlayer1(zufall.nextBoolean());
view.update();
}
private void handleAI() {
if((model.getPlayerNo() == 0) || (model.getPlayerNo() == 1 && !model.isCurrentPlayer1())) {
}
}
/**
* Function that sets the symbols X and O on the chosen fields on the board
* @param x as X
* @param y as O
*/
public void setField(int x, int y) {
if(model.isCurrentPlayer1()) {
if(model.getFeld(x, y) == 0){
model.setFeld(1, x, y);
if(checkWinner())showPopUp();
if(checkDraw())showPopUp();
changePlayer();
view.update();
}
}else{
if(model.getFeld(x, y) == 0) {
model.setFeld(2, x, y);
if(checkWinner())showPopUp();
if(checkDraw())showPopUp();
changePlayer();
view.update();
}
}
handleAI();
}
/**
* Function that shows the PopUp
*/
public void showPopUp() {
view.showPopUp();
}
/**
* Function for calculating which player has won or if it is a draw
* @return the calculation
*/
public int calculate() {
int zwischenFeld[][] = new int[3][3];
zwischenFeld = model.getFeld();
for(int i = 0; i < 3; i++) {
if(zwischenFeld[i][0] == 1 && zwischenFeld[i][1] == 1 && zwischenFeld[i][2] == 1) return 11;
if(zwischenFeld[i][0] == 2 && zwischenFeld[i][1] == 2 && zwischenFeld[i][2] == 2) return 12;
}
for(int i = 0; i < 3; i++) {
if(zwischenFeld[0][i] == 1 && zwischenFeld[1][i] == 1 && zwischenFeld[2][i] == 1) return 11;
if(zwischenFeld[0][i] == 2 && zwischenFeld[1][i] == 2 && zwischenFeld[2][i] == 2) return 12;
}
if(zwischenFeld[0][0] == 1 && zwischenFeld[1][1] == 1 && zwischenFeld[2][2] == 1) return 11;
if(zwischenFeld[0][0] == 2 && zwischenFeld[1][1] == 2 && zwischenFeld[2][2] == 2) return 12;
if(zwischenFeld[2][0] == 1 && zwischenFeld[1][1] == 1 && zwischenFeld[0][2] == 1) return 11;
if(zwischenFeld[2][0] == 2 && zwischenFeld[1][1] == 2 && zwischenFeld[0][2] == 2) return 12;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(zwischenFeld[i][j] == 0)return 0;
}
}
return 1;
}
/**
* Function for calculating which player has won or if it is a draw
* @return the calculation
*/
public int calculate(int zwischenFeld[][]) {
for(int i = 0; i < 3; i++) {
if(zwischenFeld[i][0] == 1 && zwischenFeld[i][1] == 1 && zwischenFeld[i][2] == 1) return 11;
if(zwischenFeld[i][0] == 2 && zwischenFeld[i][1] == 2 && zwischenFeld[i][2] == 2) return 12;
}
for(int i = 0; i < 3; i++) {
if(zwischenFeld[0][i] == 1 && zwischenFeld[1][i] == 1 && zwischenFeld[2][i] == 1) return 11;
if(zwischenFeld[0][i] == 2 && zwischenFeld[1][i] == 2 && zwischenFeld[2][i] == 2) return 12;
}
if(zwischenFeld[0][0] == 1 && zwischenFeld[1][1] == 1 && zwischenFeld[2][2] == 1) return 11;
if(zwischenFeld[0][0] == 2 && zwischenFeld[1][1] == 2 && zwischenFeld[2][2] == 2) return 12;
if(zwischenFeld[2][0] == 1 && zwischenFeld[1][1] == 1 && zwischenFeld[0][2] == 1) return 11;
if(zwischenFeld[2][0] == 2 && zwischenFeld[1][1] == 2 && zwischenFeld[0][2] == 2) return 12;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(zwischenFeld[i][j] == 0)return 0;
}
}
return 1;
}
/**
* @param playerNo the playerNo to set
*/
public void setPlayerNo(int playerNo) {
model.setPlayerNo(playerNo);
}
}