From 2552904a6fe49ad2ef2c97f0dd7c86b932a2d60c Mon Sep 17 00:00:00 2001 From: Saee Saadat Date: Tue, 7 May 2019 01:12:33 +0430 Subject: [PATCH] working on AI ! it inserts cards now --- duelyst/src/Model/Map/Map.java | 25 +++++++++ duelyst/src/Model/account/AI.java | 83 ++++++++++++++++++++++++----- duelyst/src/Model/account/Hand.java | 4 ++ 3 files changed, 98 insertions(+), 14 deletions(-) diff --git a/duelyst/src/Model/Map/Map.java b/duelyst/src/Model/Map/Map.java index fa3e7b1..6a390b1 100644 --- a/duelyst/src/Model/Map/Map.java +++ b/duelyst/src/Model/Map/Map.java @@ -1,9 +1,11 @@ package Model.Map; +import Model.account.Collection; import Model.item.Flag; import exeption.InvalidCellException; import java.util.ArrayList; +import java.util.Collections; public class Map { private static ArrayList maps; @@ -15,6 +17,21 @@ public class Map { public static int getManhattanDistance(Cell cell1, Cell cell2) { return Math.abs(cell1.getX()-cell2.getX())+Math.abs(cell1.getY()-cell2.getY()); } + + public Cell[] getCellsInDistance(Cell cell , int distance){ + ArrayList cells = new ArrayList<>() ; + for (Cell[] cel1 : this.board){ + for (Cell cel : cel1){ + if (Map.getManhattanDistance(cell , cel) == distance) cells.add(cel) ; + } + } + Cell[] cellsArray = new Cell[cells.size()] ; + for (int i = 0; i < cells.size(); i++) { + cellsArray[i] = cells.get(i) ; + } + return cellsArray ; + } + public static int getRadiusDistance(Cell cell1, Cell cell2) { return Integer.max(Math.abs(cell1.getX()-cell2.getX()),Math.abs(cell1.getY()-cell2.getY())); @@ -34,6 +51,14 @@ public Cell getCell(Cell cell){ return board[cell.getX()][cell.getY()]; } + public ArrayList getCells(){ + ArrayList cells = new ArrayList<>(); + for (int i =0 ; i < board.length ; i++) { + Collections.addAll(cells, board[i]); + } + return cells ; + } + public void applyFireCellAffect() { for (Cell[] cellRow : this.board) { for (Cell cell : cellRow) { diff --git a/duelyst/src/Model/account/AI.java b/duelyst/src/Model/account/AI.java index 2430e9f..6a1bf6d 100644 --- a/duelyst/src/Model/account/AI.java +++ b/duelyst/src/Model/account/AI.java @@ -1,21 +1,23 @@ package Model.account; import Controller.Game; +import Model.Map.Cell; import Model.Map.Map; import Model.PreProcess; -import Model.card.hermione.Hermione; -import Model.card.hermione.Hero; +import Model.card.Card; + import Model.card.hermione.Minion; import Model.card.spell.Spell; -import Model.item.Item; -import Model.item.Usable; +import Model.card.spell.Target; import exeption.*; -import java.util.ArrayList; -import java.util.Collections; + +import java.util.Random; public class AI extends Account { int level ; + Map map ; + Player enemy ; Deck mode1 = new Deck("mode1"); Deck mode2 = new Deck("mode2"); Deck mode3 = new Deck("mode3"); @@ -118,23 +120,19 @@ public class AI extends Account { } - public AI() throws Exception { + public AI(int level) throws Exception { super("AI", "itsAI", "imAnAIgirlInAnAIWorld"); this.level = level; + try { this.collection = new Collection(); collection.setOwner(this); - Deck deck; if (level == 1) deck = mode1 ; else if (level == 2) deck = mode2 ; else deck = mode3 ; - deck.setCollection(collection); - - - //TODO - collection.setMainDeck("AIMainDeck"); + collection.setMainDeck(deck.getName()); } catch (InvalidDeckException e) { @@ -143,8 +141,65 @@ public AI() throws Exception { } public String play() { - Map map = Game.battle.getMap(); + map = Game.battle.getMap(); + enemy = Game.battle.getEnemyPlayer() ; + Random randTypeCard = new Random(); + int randCard = randTypeCard.nextInt(2) ; + if (randCard == 0){ + insertMinion() ; + }else{ + insertSpell() ; + } return null; } + + private String insertMinion(){ + String command = ""; + for (Card card : this.player.getHand().getCards()) { + if (card.getClass().equals(Minion.class)) { + if (player.getMana() >= card.getPrice()) { + command = "Insert " + card.getName() + " in ("; + break; + } + } + } + if (command.isEmpty()) return command ; + for (int i = 1 ; i < 8 ; i++){ + Cell[] cells = this.map.getCellsInDistance(this.collection.getMainDeck().getHero().getLocation() , i) ; + for (Cell cell : cells){ + if (cell.getCardOnCell() == null){ + command = command + cell.getX() + ", " + cell.getY()+")" ; + return command ; + } + } + } + return null ; + } + + private String insertSpell(){ + String command = ""; + Spell spell = null ; + for (Card card : this.player.getHand().getCards()) { + if (card.getClass().equals(Spell.class)) { + if (player.getMana() >= card.getPrice()) { + command = "Insert " + card.getName() + " in ("; + spell = (Spell) card ; + break; + } + } + } + if (command.isEmpty() || spell == null) return command ; + Target target = spell.getTarget() ; + for (Cell cell : map.getCells()){ + try{ + target.getTarget(cell); + command = command + cell.getX() + ", " + cell.getY()+")" ; + return command ; + }catch (InvalidCellException e){ + continue; + } + } + return null ; + } } diff --git a/duelyst/src/Model/account/Hand.java b/duelyst/src/Model/account/Hand.java index b177e54..51c1960 100644 --- a/duelyst/src/Model/account/Hand.java +++ b/duelyst/src/Model/account/Hand.java @@ -87,4 +87,8 @@ public Card getCard(int cardID) throws InvalidCardException { public Card getNextCard() { return nextCard; } + + public Card[] getCards() { + return cards; + } }