Skip to content

Commit

Permalink
AI is allllll done :)))
Browse files Browse the repository at this point in the history
  • Loading branch information
Saee Saadat authored and Siniorss committed May 6, 2019
1 parent 099f303 commit 42c2c8d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 17 deletions.
88 changes: 73 additions & 15 deletions duelyst/src/Model/account/AI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
import Model.PreProcess;
import Model.card.Card;

import Model.card.hermione.Hermione;
import Model.card.hermione.Hero;
import Model.card.hermione.Minion;
import Model.card.spell.Spell;
import Model.card.spell.Target;
import exeption.*;


import java.util.ArrayList;
import java.util.Random;

public class AI extends Account {
int level;
int move; //0: insert card , 1: move Hero , 2: attack with hero , 3: move minions , 4: attack with minions
int move; //0: insert card , 1&3: select hero, 2: move Hero ,
// 4: attack with hero ,and then : minions : select-move-select-attack
Map map;
Player enemy;

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

public String play() {
move++ ;
String command;
map = Game.battle.getMap();
enemy = Game.battle.getEnemyPlayer();
Random randTypeCard = new Random();
int randCard = randTypeCard.nextInt(2);
if (randCard == 0) {
command = insertMinion();
} else {
command = insertSpell();
switch(move){
case 0:
Random randTypeCard = new Random();
int randCard = randTypeCard.nextInt(2);
if (randCard == 0) {
command = insertMinion();
} else {
command = insertSpell();
}
if (command != null && !command.isEmpty()) return command;
else move++ ;
case 1:
case 3:
command = "Select "+ collection.getMainDeck().getHero().getCardID();
return command ;
case 2:
Hero hero = collection.getMainDeck().getHero();
for (int i = 2 ; i >0 ; i--){
Cell[] cells = map.getCellsInDistance(hero.getLocation() , i) ;
for (Cell cell : cells){
if (cell.getCardOnCell() == null) {
command = "Move to (" + cell.getX() + ", " + cell.getY()+")" ;
return command ;
}
}
}
move++ ;

case 4:
hero = collection.getMainDeck().getHero() ;
command = attack(hero);
if (command != null) return command;
else move++;
default:
if (move > player.getMinionsInGame().size() + 5) {
move = -1 ;
return "End turn";
}
if (move % 2 == 1){
command = "Select " + player.getMinionsInGame().get(move-5) ;
return command ;
}
else{
Minion card = player.getMinionsInGame().get(move-6) ;
command = attack(card) ;
return command ;
}
}
if (command != null && !command.isEmpty()) return command;

Hero hero = collection.getMainDeck().getHero();


return "End turn";
}

private String attack(Hermione card) {
String command;
if (card.canAttackThisCard(enemy.getDeck().getHero())){
command = "Attack " + enemy.getDeck().getHero();
return command ;
}
Random rand = new Random();
ArrayList<Minion> target = new ArrayList<>(enemy.getMinionsInGame()) ;
int counter = 0 ;
for (int i = rand.nextInt(target.size()) ; true ; i = rand.nextInt(target.size())){
counter++ ;
if (card.canAttackThisCard(target.get(i))){
command = "Attack " + target.get(i);
return command ;
}
if (counter > 30) break ;
}
return null ;
}

private String insertMinion() {
String command = "";
Expand Down Expand Up @@ -146,7 +204,7 @@ private Deck getDeck(int level) throws FullDeckException, DeckAlreadyHasThisCard
DeckAlreadyHasThisItemException e) {
throw e;
}

break ;
case 2:
try {
deck.addCardToDeck(PreProcess.getHeroes().get(4));
Expand Down Expand Up @@ -178,7 +236,7 @@ private Deck getDeck(int level) throws FullDeckException, DeckAlreadyHasThisCard
DeckAlreadyHasThisItemException e) {
throw e;
}

break ;
case 3:
try {
deck.addCardToDeck(PreProcess.getHeroes().get(6));
Expand Down
5 changes: 5 additions & 0 deletions duelyst/src/Model/card/hermione/Hermione.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public void counterAttack(Hermione enemyCard){
}
}

public boolean canAttackThisCard(Hermione target){
return this.attackType.canReach(this , target) ;
//TODO if there are more conditions to be checked !
}

private boolean canMove(int x, int y) throws MoveTrunIsOverException, DestinationOutOfreachException, InvalidCellException {
if(this.actionTurn==1)throw new MoveTrunIsOverException();
if(Game.battle.getMap().getCell(x,y).isFull())throw new DestinationOutOfreachException();
Expand Down
4 changes: 2 additions & 2 deletions duelyst/src/Model/card/hermione/Melee.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Melee implements AttackType {

@Override
public boolean canReach(Hermione champCard, Hermione enemyCard) {
if (Map.getRadiusDistance(champCard.getLocation(), enemyCard.getLocation()) == 1) return true;
return false;
return Map.getRadiusDistance(champCard.getLocation(), enemyCard.getLocation()) == 1 ;

}
}

0 comments on commit 42c2c8d

Please sign in to comment.