Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
alishirmohammadi committed May 7, 2019
2 parents 8b7dc1c + 001fdb3 commit 3590ab1
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 88 deletions.
10 changes: 8 additions & 2 deletions src/controllers/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ public static Input getInput() {
}

public static String getAIMove() {
playingMatch.getPlayer2().decide();
return playingMatch.getPlayer2().getDecision();
}

Expand Down Expand Up @@ -190,7 +189,7 @@ public static void setMatchData(boolean isAIMode, int gameMode, String username)
if (!isOpponentNull()) {
Account opponent;
if (isAIMode) {
opponent = Account.getAIAccount();
opponent = AI.getAIAccount();
}
else
opponent = Account.getAccounts().get(username);
Expand All @@ -211,6 +210,9 @@ public static void setMatchData(boolean isAIMode, int gameMode, String username)
opponentUsername = "";
}
playingMatch.setAIMode(isAIMode);
if (isAIMode) {
((AI) opponent).setMatch(playingMatch);
}
}
}

Expand Down Expand Up @@ -335,4 +337,8 @@ public static List<UsableItem> getItem(List<Card> cards) {
card -> (UsableItem) card)
.collect(Collectors.toList());
}

public static void setMapFlags(int result) {
playingMatch.setFlagsInMap(result);
}
}
198 changes: 197 additions & 1 deletion src/models/AI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,201 @@
package models;

public class AI {
import controllers.Manager;
import models.map.Cell;
import models.map.Map;
import models.match.Match;
import views.Output;

import java.util.*;

public class AI extends Account {

Match match;
private Queue<String> decisions = new LinkedList<>();
/// state[distanceToHero][oppHealth][myHealth][action]
int[][][][] state = new int[10][10][10][3];
private final int ACTIONS_NUMBER = 3;
int[] lastState = new int[3];

public AI(String username, String password) {
super(username, password);
}

private int distanceToOppHero() {
int distance = 0;
try {
distance = Cell.manhattanDistance(match.getPlayer1().getHero().getCell(),
match.getPlayer2().getHero().getCell()) - 1;
if (distance >= 9)
distance = 9;
} catch (Exception ignored) {
Output.err("Ignored in AI");
}
return distance;
}

private int getHealth() {
int health = 0;
try {
health = match.getPlayer2().getHero().getCurrentHealth() / 5;
if (health >= 9)
health = 9;
} catch (Exception ignored) {
Output.err("Ignored in AI");
}
return health;
}

private int getOppHealth() {
int health = 0;
try {
health = match.getPlayer1().getHero().getCurrentHealth() / 5;
if (health >= 9)
health = 9;
} catch (Exception ignored) {
Output.err("Ignored in AI");
}
return health;
}

int t = 0;

public void decide() {
reward();
int distance = lastState[0] = distanceToOppHero();
int myHealth = lastState[1] = getHealth();
int oppHealth = lastState[2] = getOppHealth();
int max = 0;
for (int i = 0; i < ACTIONS_NUMBER; i++) {
max = Math.max(max, state[distance][oppHealth][myHealth][i]);
}
if (max < 100) {
chooseAction(new Random().nextInt() % 3);
return;
}
for (int i = 0; i < ACTIONS_NUMBER; i++) {
if (state[distance][oppHealth][myHealth][i] == max) {
chooseAction(i);
return;
}
}
// if (t % 2 == 0)
// this.decision = "show hand";
// else
// this.decision = "end turn";
// t = (t + 1) % 2;
}

private void reward() {

}

private void chooseAction(int action) {
switch (action) {
case 0:
insertCard();
break;
case 1:
moveCard();
break;
case 2:
attack();
break;
default:
decisions.add("end turn");
}
}

private void attack() {
selectCard();
try {
decisions.add("attack " + match.getPlayer1().getHero().getID());
} catch (Player.HeroDeadException e) {
Output.err("ignored in attack in AI");
}
}

private void insertCard() {
decisions.add("insert " + match.getPlayer2().getHand().getCards().get(Match.random(0, 4)).getID() + " in " +
Match.random(1, Map.ROW_NUMBER) + ", " + Match.random(1, Map.COLUMN_NUMBER));
}

private void selectCard() {
try {
decisions.add("select " + match.getPlayer2().getHero().getID());
} catch (Player.HeroDeadException e) {
Output.err("ignored in insertCard in AI");
}
}

private void moveCard() {
selectCard();
decisions.add("move to " + Match.random(1, Map.ROW_NUMBER) + ", " + Match.random(1, Map.COLUMN_NUMBER));
}


public String getDecision() {
if (match.getPlayer2().getMana() < 2)
return "end turn";
if (decisions.size() == 0)
decide();
return decisions.poll();
}

public void setMatch(Match playingMatch) {
this.match = playingMatch;
}

public static AI getAIAccount() {
AI aiAccount = new AI("AI", "password");
try {
Shop.getInstance().buy(aiAccount, "rostam");
Shop.getInstance().buy(aiAccount, "fire dragon");
Shop.getInstance().buy(aiAccount, "eagle");
Shop.getInstance().buy(aiAccount, "Hog Head Demon");
Shop.getInstance().buy(aiAccount, "Persian Swordsman");
Shop.getInstance().buy(aiAccount, "Persian Horse Rider");
Shop.getInstance().buy(aiAccount, "Persian Horse Rider");
Shop.getInstance().buy(aiAccount, "Persian Horse Rider");
Shop.getInstance().buy(aiAccount, "Persian Horse Rider");
Shop.getInstance().buy(aiAccount, "Persian Champion");
Shop.getInstance().buy(aiAccount, "Persian Champion");
Shop.getInstance().buy(aiAccount, "Turan Archer");
Shop.getInstance().buy(aiAccount, "Turan Archer");
Shop.getInstance().buy(aiAccount, "Turan Wand");
Shop.getInstance().buy(aiAccount, "Turan Wand");
Shop.getInstance().buy(aiAccount, "persian horse rider");
Shop.getInstance().buy(aiAccount, "persian horse rider");
Shop.getInstance().buy(aiAccount, "persian horse rider");
Shop.getInstance().buy(aiAccount, "persian horse rider");
Shop.getInstance().buy(aiAccount, "persian horse rider");
Shop.getInstance().buy(aiAccount, "persian horse rider");
aiAccount.addDeck(new Deck("AIDeck"));
Deck deck = aiAccount.getDeck("AIDeck");
Manager.addCardToDeck(aiAccount, deck, "rostam");
Manager.addCardToDeck(aiAccount, deck, "fire dragon");
Manager.addCardToDeck(aiAccount, deck, "eagle");
Manager.addCardToDeck(aiAccount, deck, "Hog Head Demon");
Manager.addCardToDeck(aiAccount, deck, "Persian Swordsman");
Manager.addCardToDeck(aiAccount, deck, "Persian Horse Rider");
Manager.addCardToDeck(aiAccount, deck, "Persian Horse Rider");
Manager.addCardToDeck(aiAccount, deck, "Persian Horse Rider");
Manager.addCardToDeck(aiAccount, deck, "Persian Horse Rider");
Manager.addCardToDeck(aiAccount, deck, "Persian Champion");
Manager.addCardToDeck(aiAccount, deck, "Persian Champion");
Manager.addCardToDeck(aiAccount, deck, "Turan Archer");
Manager.addCardToDeck(aiAccount, deck, "Turan Archer");
Manager.addCardToDeck(aiAccount, deck, "Turan Wand");
Manager.addCardToDeck(aiAccount, deck, "Turan Wand");
Manager.addCardToDeck(aiAccount, deck, "persian horse rider");
Manager.addCardToDeck(aiAccount, deck, "persian horse rider");
Manager.addCardToDeck(aiAccount, deck, "persian horse rider");
Manager.addCardToDeck(aiAccount, deck, "persian horse rider");
Manager.addCardToDeck(aiAccount, deck, "persian horse rider");
aiAccount.setMainDeck(deck);
} catch (Exception e) {
Output.err(e);
}
return aiAccount;
}
}
53 changes: 1 addition & 52 deletions src/models/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,7 @@ public class Account {
private int drake = 15000;
private int winCount = 0;

public static Account getAIAccount() {
Account account = new Account("AI", "password");
try {
Shop.getInstance().buy(account, "rostam");
Shop.getInstance().buy(account, "fire dragon");
Shop.getInstance().buy(account, "eagle");
Shop.getInstance().buy(account, "Hog Head Demon");
Shop.getInstance().buy(account, "Persian Swordsman");
Shop.getInstance().buy(account, "Persian Horse Rider");
Shop.getInstance().buy(account, "Persian Horse Rider");
Shop.getInstance().buy(account, "Persian Horse Rider");
Shop.getInstance().buy(account, "Persian Horse Rider");
Shop.getInstance().buy(account, "Persian Champion");
Shop.getInstance().buy(account, "Persian Champion");
Shop.getInstance().buy(account, "Turan Archer");
Shop.getInstance().buy(account, "Turan Archer");
Shop.getInstance().buy(account, "Turan Wand");
Shop.getInstance().buy(account, "Turan Wand");
Shop.getInstance().buy(account, "persian horse rider");
Shop.getInstance().buy(account, "persian horse rider");
Shop.getInstance().buy(account, "persian horse rider");
Shop.getInstance().buy(account, "persian horse rider");
Shop.getInstance().buy(account, "persian horse rider");
Shop.getInstance().buy(account, "persian horse rider");
account.addDeck(new Deck("AIDeck"));
Deck deck = account.getDeck("AIDeck");
Manager.addCardToDeck(account, deck, "rostam");
Manager.addCardToDeck(account, deck, "fire dragon");
Manager.addCardToDeck(account, deck, "eagle");
Manager.addCardToDeck(account, deck, "Hog Head Demon");
Manager.addCardToDeck(account, deck, "Persian Swordsman");
Manager.addCardToDeck(account, deck, "Persian Horse Rider");
Manager.addCardToDeck(account, deck, "Persian Horse Rider");
Manager.addCardToDeck(account, deck, "Persian Horse Rider");
Manager.addCardToDeck(account, deck, "Persian Horse Rider");
Manager.addCardToDeck(account, deck, "Persian Champion");
Manager.addCardToDeck(account, deck, "Persian Champion");
Manager.addCardToDeck(account, deck, "Turan Archer");
Manager.addCardToDeck(account, deck, "Turan Archer");
Manager.addCardToDeck(account, deck, "Turan Wand");
Manager.addCardToDeck(account, deck, "Turan Wand");
Manager.addCardToDeck(account, deck, "persian horse rider");
Manager.addCardToDeck(account, deck, "persian horse rider");
Manager.addCardToDeck(account, deck, "persian horse rider");
Manager.addCardToDeck(account, deck, "persian horse rider");
Manager.addCardToDeck(account, deck, "persian horse rider");
account.setMainDeck(deck);
} catch (Exception e) {
Output.err(e);
}
return account;
}


public int getDrake() {
return drake;
Expand Down
38 changes: 21 additions & 17 deletions src/models/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import models.cards.Attacker;
import models.cards.Card;
import models.cards.Hero;
import models.cards.Minion;
import models.cards.spell.Spell;
import models.items.CollectableItem;
import models.items.Flag;
import models.items.Item;
import models.map.Cell;
import views.Error;
import views.Input;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -21,13 +20,12 @@ public class Player {
private Deck deck;
private Hand hand = new Hand();
private Card selectedCard;
private int flags;
private int flagsNumber;
private List<Item> collectedItems = new ArrayList<>();
private ArrayList<Card> graveYard;
private ArrayList<Attacker> activeCards = new ArrayList<>();
private int mana;
private Input input;
private String decision;
private CollectableItem selectedCollectableItem;

public Account getAccount() {
Expand All @@ -51,6 +49,11 @@ public Player(Account account) {
this.setNextCard();
}

public String getDecision() {
AI ai = (AI)account;
return ai.getDecision();
}

public ArrayList<Card> getGraveYard() {
return graveYard;
}
Expand Down Expand Up @@ -95,7 +98,7 @@ public Deck getDeck() {
}

public void incrementFlags() {
this.flags += flags;
this.flagsNumber += flagsNumber;
}

public void setCollectedItems(Item collectedItems) {
Expand Down Expand Up @@ -149,18 +152,6 @@ public Attacker getActiveCard(String cardID) throws Collection.CardNotFoundExcep
throw new Collection.CardNotFoundException("Card not found with this ID");
}
}
int t = 0;
public void decide() {
if (t % 2 == 0)
this.decision = "show hand";
else
this.decision = "end turn";
t = (t + 1) % 2;
}

public String getDecision() {
return decision;
}

public void selectCard(Card card) {
this.selectedCard = card;
Expand Down Expand Up @@ -199,6 +190,19 @@ public boolean collectableItemIsSelected() {
return selectedCollectableItem != null;
}

public boolean hasFlag() {
return this.flagsNumber > 0;
}

public List<Flag> getFlags() {
return activeCards.stream().map(Attacker::getFlag).collect(Collectors.toList());
}

public int getFlagsNumber() {
return flagsNumber;
}


public static class NotEnoughManaException extends Exception {
public NotEnoughManaException(String message) {
super(message);
Expand Down
Loading

0 comments on commit 3590ab1

Please sign in to comment.