-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
229 lines (200 loc) · 6.22 KB
/
Player.java
File metadata and controls
229 lines (200 loc) · 6.22 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
package tictactoe;
import java.util.Random;
public interface Player {
void move();
}
interface Ai extends Player {
default void randomMove(int[][] table, int move) {
Random gen = new Random();
int row;
int column;
while (true) {
row = gen.nextInt(table.length);
column = gen.nextInt(table.length);
if (table[row][column] == 0) {
table[row][column] = move;
return;
}
}
}
}
class HumanPlayer implements Player {
TicTacToeNumeric game;
HumanPlayer(TicTacToeNumeric game) {
this.game = game;
}
@Override
public void move() {
this.makingMove();
game.swapPlayer();
}
private static boolean assertInput(String input) {
if (!input.matches("[\\d]")) {
System.out.println("You should enter numbers!");
return true;
} else if (!input.matches("[1-3]")) {
System.out.println("Coordinates should be from 1 to 3!");
return true;
}
return false;
}
private void makingMove() {
while (true) {
int[][] table = game.getTable();
System.out.println("Enter the coordinates:");
String inputI = game.getInput().next();
if (assertInput(inputI)) {
continue;
}
String inputJ = game.getInput().next();
if (assertInput(inputJ)) {
continue;
}
int i = Integer.parseInt(inputI) - 1;
int j = Integer.parseInt(inputJ);
if (table[3 - j][i] == 0) {
table[3 - j][i] = game.getMove();
break;
} else {
System.out.println("This cell is occupied! Choose another one!");
}
}
}
}
class EasyAi implements Ai {
TicTacToeNumeric game;
public EasyAi(TicTacToeNumeric game) {
this.game = game;
}
@Override
public void move() {
System.out.println("Making move level \"easy\"");
randomMove(game.getTable(), game.getMove());
game.swapPlayer();
}
}
class MediumAi implements Ai {
TicTacToeNumeric game;
final int[] movesW = new int[2];
final int[] movesD = new int[2];
final int[] movesS = new int[2];
private boolean defence = false;
private boolean attack = false;
public MediumAi(TicTacToeNumeric game) {
this.game = game;
}
@Override
public void move() {
System.out.println("Making move level \"medium\"");
if (!findPossibleMove()) {
randomMove(game.getTable(), game.getMove());
} else if (!getAttack()) {
game.table[movesS[0]][movesS[1]] = game.getMove();
}
game.swapPlayer();
}
private boolean findPossibleMove() {
this.defence = false;
int[][] table = game.getTable();
int countX;
int countY;
for (int i = 0; i < table.length; i++) {
countX = 0;
countY = 0;
for (int j = 0; j < table.length; j++) {
countX += table[i][j];
countY += table[j][i];
if (table[i][j] == 0) {
this.setMoves(movesW, i, j);
}
if (table[j][i] == 0) {
this.setMoves(movesD, j, i);
}
}
if (checkResult(countX, countY)) {
return true;
}
}
if (checkDial()) {
return true;
}
return getDefence();
}
private boolean checkResult(int countX, int countY) {
if (game.getMove() == 1) {
if (countX == game.getBaseSize() - 1) {
setAttack();
game.table[movesW[0]][movesW[1]] = game.getMove();
return true;
} else if (countY == game.getBaseSize() - 1) {
setAttack();
game.table[movesD[0]][movesD[1]] = game.getMove();
return true;
}
if (!getDefence() && countX == -(game.getBaseSize() -1)) {
this.setMoves(this.movesS, movesW[0], movesW[1]);
this.setDefence();
} else if (!getDefence() && countY == -(game.getBaseSize() - 1)) {
this.setMoves(this.movesS, movesD[0], movesD[1]);
this.setDefence();
}
}
if (game.getMove() == -1) {
if (countX == -(game.getBaseSize() - 1)) {
setAttack();
game.table[movesW[0]][movesW[1]] = game.getMove();
return true;
} else if (countY == -(game.getBaseSize() - 1)) {
setAttack();
game.table[movesD[0]][movesD[1]] = game.getMove();
return true;
}
if (!getDefence() && countX == game.getBaseSize() -1) {
this.setMoves(this.movesS, movesW[0], movesW[1]);
this.setDefence();
} else if (!getDefence() && countY == game.getBaseSize() - 1) {
this.setMoves(this.movesS, movesD[0], movesD[1]);
this.setDefence();
}
}
return false;
}
public boolean getDefence() {
return this.defence;
}
private void setDefence() {
this.defence = true;
}
public void setAttack() {
this.attack = true;
}
public boolean getAttack() {
return this.attack;
}
private boolean checkDial() {
int[][] table = game.getTable();
int countX = 0;
int countY = 0;
for (int i = 0, j = 2; i < 3; i++, j--) {
if (table[i][i] == 0) {
this.setMoves(this.movesW, i, i);
} else if (1 == table[i][i]) {
countX++;
} else {
countX--;
}
if (table[i][j] == 0) {
this.setMoves(this.movesD, i, j);
} else if (1 == table[i][j]) {
countY++;
} else {
countY--;
}
}
return checkResult(countX, countY);
}
private void setMoves(int[] ms, int row, int column) {
ms[0] = row;
ms[1] = column;
}
}