Skip to content

Commit

Permalink
working on AI ! it inserts cards now
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 42af235 commit 2552904
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 14 deletions.
25 changes: 25 additions & 0 deletions duelyst/src/Model/Map/Map.java
Original file line number Diff line number Diff line change
@@ -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<Map> maps;
Expand All @@ -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<Cell> 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()));

Expand All @@ -34,6 +51,14 @@ public Cell getCell(Cell cell){
return board[cell.getX()][cell.getY()];
}

public ArrayList<Cell> getCells(){
ArrayList<Cell> 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) {
Expand Down
83 changes: 69 additions & 14 deletions duelyst/src/Model/account/AI.java
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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) {
Expand All @@ -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 ;
}
}
4 changes: 4 additions & 0 deletions duelyst/src/Model/account/Hand.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ public Card getCard(int cardID) throws InvalidCardException {
public Card getNextCard() {
return nextCard;
}

public Card[] getCards() {
return cards;
}
}

0 comments on commit 2552904

Please sign in to comment.