diff --git a/duelyst/src/Model/account/AI.java b/duelyst/src/Model/account/AI.java index 29c17f9..970eecf 100644 --- a/duelyst/src/Model/account/AI.java +++ b/duelyst/src/Model/account/AI.java @@ -6,6 +6,7 @@ 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; @@ -13,11 +14,13 @@ 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; @@ -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 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 = ""; @@ -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)); @@ -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)); diff --git a/duelyst/src/Model/card/hermione/Hermione.java b/duelyst/src/Model/card/hermione/Hermione.java index 107d457..12a8005 100644 --- a/duelyst/src/Model/card/hermione/Hermione.java +++ b/duelyst/src/Model/card/hermione/Hermione.java @@ -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(); diff --git a/duelyst/src/Model/card/hermione/Melee.java b/duelyst/src/Model/card/hermione/Melee.java index c790d96..a5f6131 100644 --- a/duelyst/src/Model/card/hermione/Melee.java +++ b/duelyst/src/Model/card/hermione/Melee.java @@ -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 ; + } }