Skip to content

Commit 42c2c8d

Browse files
Saee SaadatSiniorss
authored andcommitted
AI is allllll done :)))
1 parent 099f303 commit 42c2c8d

3 files changed

Lines changed: 80 additions & 17 deletions

File tree

duelyst/src/Model/account/AI.java

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66
import Model.PreProcess;
77
import Model.card.Card;
88

9+
import Model.card.hermione.Hermione;
910
import Model.card.hermione.Hero;
1011
import Model.card.hermione.Minion;
1112
import Model.card.spell.Spell;
1213
import Model.card.spell.Target;
1314
import exeption.*;
1415

1516

17+
import java.util.ArrayList;
1618
import java.util.Random;
1719

1820
public class AI extends Account {
1921
int level;
20-
int move; //0: insert card , 1: move Hero , 2: attack with hero , 3: move minions , 4: attack with minions
22+
int move; //0: insert card , 1&3: select hero, 2: move Hero ,
23+
// 4: attack with hero ,and then : minions : select-move-select-attack
2124
Map map;
2225
Player enemy;
2326

@@ -43,24 +46,79 @@ public AI(int level) throws FullDeckException, DeckAlreadyHasThisCardException,
4346
}
4447

4548
public String play() {
49+
move++ ;
4650
String command;
4751
map = Game.battle.getMap();
4852
enemy = Game.battle.getEnemyPlayer();
49-
Random randTypeCard = new Random();
50-
int randCard = randTypeCard.nextInt(2);
51-
if (randCard == 0) {
52-
command = insertMinion();
53-
} else {
54-
command = insertSpell();
53+
switch(move){
54+
case 0:
55+
Random randTypeCard = new Random();
56+
int randCard = randTypeCard.nextInt(2);
57+
if (randCard == 0) {
58+
command = insertMinion();
59+
} else {
60+
command = insertSpell();
61+
}
62+
if (command != null && !command.isEmpty()) return command;
63+
else move++ ;
64+
case 1:
65+
case 3:
66+
command = "Select "+ collection.getMainDeck().getHero().getCardID();
67+
return command ;
68+
case 2:
69+
Hero hero = collection.getMainDeck().getHero();
70+
for (int i = 2 ; i >0 ; i--){
71+
Cell[] cells = map.getCellsInDistance(hero.getLocation() , i) ;
72+
for (Cell cell : cells){
73+
if (cell.getCardOnCell() == null) {
74+
command = "Move to (" + cell.getX() + ", " + cell.getY()+")" ;
75+
return command ;
76+
}
77+
}
78+
}
79+
move++ ;
80+
81+
case 4:
82+
hero = collection.getMainDeck().getHero() ;
83+
command = attack(hero);
84+
if (command != null) return command;
85+
else move++;
86+
default:
87+
if (move > player.getMinionsInGame().size() + 5) {
88+
move = -1 ;
89+
return "End turn";
90+
}
91+
if (move % 2 == 1){
92+
command = "Select " + player.getMinionsInGame().get(move-5) ;
93+
return command ;
94+
}
95+
else{
96+
Minion card = player.getMinionsInGame().get(move-6) ;
97+
command = attack(card) ;
98+
return command ;
99+
}
55100
}
56-
if (command != null && !command.isEmpty()) return command;
57-
58-
Hero hero = collection.getMainDeck().getHero();
59-
60-
61-
return "End turn";
62101
}
63102

103+
private String attack(Hermione card) {
104+
String command;
105+
if (card.canAttackThisCard(enemy.getDeck().getHero())){
106+
command = "Attack " + enemy.getDeck().getHero();
107+
return command ;
108+
}
109+
Random rand = new Random();
110+
ArrayList<Minion> target = new ArrayList<>(enemy.getMinionsInGame()) ;
111+
int counter = 0 ;
112+
for (int i = rand.nextInt(target.size()) ; true ; i = rand.nextInt(target.size())){
113+
counter++ ;
114+
if (card.canAttackThisCard(target.get(i))){
115+
command = "Attack " + target.get(i);
116+
return command ;
117+
}
118+
if (counter > 30) break ;
119+
}
120+
return null ;
121+
}
64122

65123
private String insertMinion() {
66124
String command = "";
@@ -146,7 +204,7 @@ private Deck getDeck(int level) throws FullDeckException, DeckAlreadyHasThisCard
146204
DeckAlreadyHasThisItemException e) {
147205
throw e;
148206
}
149-
207+
break ;
150208
case 2:
151209
try {
152210
deck.addCardToDeck(PreProcess.getHeroes().get(4));
@@ -178,7 +236,7 @@ private Deck getDeck(int level) throws FullDeckException, DeckAlreadyHasThisCard
178236
DeckAlreadyHasThisItemException e) {
179237
throw e;
180238
}
181-
239+
break ;
182240
case 3:
183241
try {
184242
deck.addCardToDeck(PreProcess.getHeroes().get(6));

duelyst/src/Model/card/hermione/Hermione.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public void counterAttack(Hermione enemyCard){
7474
}
7575
}
7676

77+
public boolean canAttackThisCard(Hermione target){
78+
return this.attackType.canReach(this , target) ;
79+
//TODO if there are more conditions to be checked !
80+
}
81+
7782
private boolean canMove(int x, int y) throws MoveTrunIsOverException, DestinationOutOfreachException, InvalidCellException {
7883
if(this.actionTurn==1)throw new MoveTrunIsOverException();
7984
if(Game.battle.getMap().getCell(x,y).isFull())throw new DestinationOutOfreachException();

duelyst/src/Model/card/hermione/Melee.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Melee implements AttackType {
77

88
@Override
99
public boolean canReach(Hermione champCard, Hermione enemyCard) {
10-
if (Map.getRadiusDistance(champCard.getLocation(), enemyCard.getLocation()) == 1) return true;
11-
return false;
10+
return Map.getRadiusDistance(champCard.getLocation(), enemyCard.getLocation()) == 1 ;
11+
1212
}
1313
}

0 commit comments

Comments
 (0)