-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIPlayer.java
More file actions
315 lines (250 loc) · 9.12 KB
/
AIPlayer.java
File metadata and controls
315 lines (250 loc) · 9.12 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
311
312
313
314
315
package structures;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import akka.actor.ActorRef;
import commands.BasicCommands;
import structures.basic.Card;
import structures.basic.Player;
import structures.basic.Tile;
import structures.basic.Unit;
import structures.basic.ability.Spell;
import utils.Attack;
import utils.BasicObjectBuilders;
import utils.Preparation;
import utils.StaticConfFiles;
public class AIPlayer extends Player {
private HashMap<Unit, ArrayList<Integer>> enemyUnitPos; // 敌方场上单位id及其坐标
private HashMap<Unit, ArrayList<Integer>> friendlyUnitPos; // 我方场上单位id及其坐标
private List<Card> hand;
private int i;
private int j;
public void doSomething(ActorRef out, GameState gameState) {
// AI player will do something to modify its cards and units
this.AIHand(gameState);
// play a card on a certain tile
i = 7;
j = 3;
this.checkUnitCostAndPlace(gameState, hand.get(0), out, i, j);
// get enemy and friendly units and their positions
this.getEnemyUnitOnBoard(gameState);
this.getFriendlyUnitOnBoard(gameState);
// get enemy and friendly avatars and their positions
this.getEnemyAvatarOnBoard(gameState);
this.getFriendlyAvatarOnBoard(gameState);
this.moveTo(gameState, getAvatar(), out, i - 1, j);
this.attack(gameState, getAvatar(), out);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// AI finish its round, add 1 to round number
switchToHumanPlayer(out, gameState);
}
private void switchToHumanPlayer(ActorRef out, GameState gameState) {
// human players' turn
gameState.rounds++;
Preparation.turnOverTheCoin(gameState);
// tell human player that it finish its turn
BasicCommands.addPlayer2Notification(out, "I'm done!", 2);
// draw card to human player and distribute Mana
Preparation.drawCardsToPlayer(gameState);
Preparation.distributeMana(out, gameState, gameState.player);
Preparation.resetMoveAndAttackCount(gameState.gameBoard.getPlayer1UnitList());
// clean current Hand and show human player's hand
Preparation.cleanHand(out, gameState.aiPlayer);
Preparation.showHand(out, gameState.player);
// print turn switching notification
BasicCommands.addPlayer1Notification(out, "Player1's turn!", 2);
}
private void getEnemyUnitOnBoard(GameState gs) {
this.enemyUnitPos = new HashMap<>();
for (Unit u : gs.gameBoard.getPlayer1UnitList()) {
ArrayList<Integer> pos = new ArrayList<>();
pos.add(u.getPosition().getTilex());
pos.add(u.getPosition().getTiley());
this.enemyUnitPos.put(u, pos);
}
// for test
System.out.println("Enemy Unit Positions:");
for (Map.Entry<Unit, ArrayList<Integer>> entries : enemyUnitPos.entrySet()) {
System.out.println(entries.getKey().getId() + ": " + entries.getValue());
}
}
private ArrayList<Integer> getEnemyAvatarOnBoard(GameState gs) {
ArrayList<Integer> temp = null;
for (Map.Entry<Unit, ArrayList<Integer>> entries : enemyUnitPos.entrySet()) {
if (entries.getKey().getId() == 40) { // the id of enemy avatar is 40
temp = entries.getValue();
}
}
// for test
System.out.print("Enemy Avatar Position is: ");
for (Integer i : temp) {
System.out.print(i + " ");
}
System.out.println();
return temp;
}
private void AIHand(GameState gameState) {
hand = gameState.aiPlayer.getHand();
}
// 获得己方单位的坐标。Get friendly unit positions
private void getFriendlyUnitOnBoard(GameState gs) {
this.friendlyUnitPos = new HashMap<>();
for (Unit u : gs.gameBoard.getPlayer2UnitList()) {
ArrayList<Integer> pos = new ArrayList<>();
pos.add(u.getPosition().getTilex());
pos.add(u.getPosition().getTiley());
this.friendlyUnitPos.put(u, pos);
}
// for test
System.out.println("Friendly Unit Positions: ");
for (Map.Entry<Unit, ArrayList<Integer>> entries : friendlyUnitPos.entrySet()) {
System.out.println(entries.getKey().getId() + ": " + entries.getValue());
}
}
private ArrayList<Integer> getFriendlyAvatarOnBoard(GameState gs) {
ArrayList<Integer> temp = null;
for (Map.Entry<Unit, ArrayList<Integer>> entries : friendlyUnitPos.entrySet()) {
if (entries.getKey().getId() == 41) { // the id of friendly avatar is 40
temp = entries.getValue();
}
}
// for test
System.out.print("Friendly Avatar Position is: ");
for (Integer i : temp) {
System.out.print(i + " ");
}
System.out.println();
return temp;
}
// 将某个单位放置在棋牌上的某个坐标 place a unit on a tile
private void placeAUnit(GameState gameState, ActorRef out, int x, int y, Card card) {
// initialize the unit object
Unit unit = Preparation.transCardToUnit(card);
// show the unit
if (unit != null) {
// load it to the board
gameState.gameBoard.getTile(x, y).setOccupied(true);
gameState.gameBoard.addplayer2UnitToBoard(unit);
Tile tile = BasicObjectBuilders.loadTile(x, y);
unit.setPositionByTile(tile);
unit.showUnit(out, tile, card.getBigCard().getAttack(), card.getBigCard().getHealth());
// set unit stun
gameState.stunUnitList.add(unit);
}
// discard the summoned card
gameState.aiPlayer.getHand().remove(card);
// fresh the game screen
gameState.gameBoard.drawBoard(out);
System.out.println(card.getCardname() + "is on board");
System.out.println(unit.getAbilities());
}
// check if the target tile is summonable and if the mana is enough to summon
// the unit
private void checkUnitCostAndPlace(GameState gs, Card c, ActorRef out, int tilex, int tiley) {
int mana = gs.aiPlayer.getMana();
gs.selectedCard = c;
// to know where the unit can be placed on
gs.gameBoard.calculateAvailableSummonPlace(gs);
// for test
gs.gameBoard.showAvailableSummonTile(out);
if (gs.gameBoard.getTile(tilex, tiley).isAllowSummon()) {
// if this tile is summonable
// to know whether the cost is larger than mana
if (c.getManacost() <= mana) {
this.placeAUnit(gs, out, tilex, tiley, c);
gs.aiPlayer.setMana(mana -= c.getManacost());
System.out.println(c.getCardname() + "is on board now!");
} else {
System.out.println("Not enough mana!");
}
} else {
System.out.println("This tile is not summonable!");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gs.gameBoard.drawBoard(out);
}
// cast a spell to a unit
private void castSpell() {
}
// move a unit to a tile. If this movement is successful, return true; else
// return false.
private boolean moveTo(GameState gs, Unit u, ActorRef out, int tilex, int tiley) {
gs.selectedUnit = u;
// check if the unit is stunned or not. If it is stunned, abort this move
if (gs.stunUnitList.indexOf(u) != -1) {
System.out.println("Movement failed!");
return false;
}
// calculate which tile the unit can move to
gs.gameBoard.calculateAvaliableMovePlace(gs, gs.gameBoard.getPlayer1UnitList(), u.getPosition().getTilex(),
u.getPosition().getTiley());
// if the destination is out of move range, abort this move
if (!gs.gameBoard.getTile(tilex, tiley).isAllowMove()) {
System.out.println("Movement failed!");
return false;
}
// gs.gameBoard.showAvailableMoveTile(out);
// play animation
BasicCommands.moveUnitToTile(out, gs.selectedUnit, gs.gameBoard.getTile(tilex, tiley));
// set target board occupied
gs.gameBoard.resetUnitTileOccupied(gs.selectedUnit, tilex, tiley);
// set unit position
gs.selectedUnit.setPositionByCoordination(tilex, tiley);
// decrease move chance
gs.selectedUnit.decreaseMoveCount();
// cancel selection
gs.selectedUnit = null;
// gs.gameBoard.drawBoard(out);
gs.gameBoard.cleanBoardFlag();
System.out.println("Movement successful!");
return true;
}
// command a unit to attack another unit (in its attack range)
private void attack(GameState gs, Unit offensive, ActorRef out) {
gs.selectedUnit = offensive;
int x = gs.selectedUnit.getPosition().getTilex();
int y = gs.selectedUnit.getPosition().getTiley();
// get a list of all enemy units on board
ArrayList<Unit> enemyList = new ArrayList<Unit>();
enemyList.addAll(this.enemyUnitPos.keySet());
// calculate the available attack target of friendly units
gs.gameBoard.calculateAvaliableAttackTarget(gs, enemyList);
gs.gameBoard.showAvailableAttackTile(out);
// get all attackable tiles
ArrayList<Tile> attackableTiles = new ArrayList<>();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 5; j++) {
if (gs.gameBoard.getTile(i, j).isAllowAttack() == true) {
attackableTiles.add(gs.gameBoard.getTile(i, j));
}
}
}
if(attackableTiles.size()==0) {
System.out.println("No attackable units!");
return;
}else {
int attackableX = attackableTiles.get(0).getTilex();
int attackableY = attackableTiles.get(0).getTiley();
for(Unit unit : this.enemyUnitPos.keySet()) {
if(unit.getPosition().getTilex() == attackableX && unit.getPosition().getTiley() == attackableY) {
Attack.unitAttackAnotherUnit(out, offensive, unit);
}
}
}
}
// calculate the destination
private void calculateToWhichTile() {
}
}