Search in sources :

Example 1 with Board

use of cardgame1.Board in project cardgame1 by joey101937.

the class AI method getValueOfCard.

/**
 * gets the value that playing a card represents
 * @param c card to evaluate
 * @return the value playing the card presents
 */
public static int getValueOfCard(Card c) {
    Board.getMainBoard().tick();
    Hero enemy;
    if (c.getOwner() == Board.topHero) {
        enemy = Board.botHero;
    } else {
        enemy = Board.topHero;
    }
    int value = 0;
    switch(c.cardPurpose) {
        case VanillaMinion:
            value = c.summon.attack + c.summon.health + c.intrinsicValue;
            if (c.cost < c.getOwner().minions.numOccupants()) {
                // small minions are penalized for taking a board slot
                value -= c.cost - c.getOwner().minions.numOccupants();
            }
            // if there is no place to summon the minion, it has 0 value.
            if (c.getOwner().minions.isFull())
                return 0;
            if (isVulnerable(c.summon)) {
                value = c.summon.attack + c.intrinsicValue;
            }
            return value;
        case BattlecryMinionDamage:
            // if there is no place to summon the minion, it has 0 value.
            if (c.getOwner().minions.isFull())
                return 0;
            Minion target = AI.getBestTarget(new SimulatedMinion(c.spellDamage, 0, c.getOwner()));
            // System.out.println("best target for " + c + " is " + target);
            if (target != null)
                value += getWorth(target) - AI.getWorthAfterDamage(target, c.spellDamage);
            else
                // there are no minions to use it on so this is the value of using the damage on the opponent's hero
                value += c.spellDamage / 2;
            if (isVulnerable(c.summon)) {
                value += c.summon.attack;
            } else {
                value += c.summon.attack + c.summon.maxHealth;
            }
            return value + c.intrinsicValue;
        case BattlecryMinionHeal:
            // if there is no place to summon the minion, it has 0 value.
            if (c.getOwner().minions.isFull())
                return 0;
            for (Minion m : c.getOwner().minions.getStorage()) {
                if (m == null)
                    continue;
                if (isVulnerable(m)) {
                    if (!isVulnerable(new SimulatedMinion(m.attack, m.health + c.spellDamage, c.getOwner()))) {
                        // if we can save a minion
                        if (getWorth(m) > value) {
                            // the value of the card becomes teh value of the minion saved. use the value of the most important minion we can save
                            value = getWorth(m);
                        }
                    }
                }
            }
            if (value == 0)
                value = c.spellDamage;
            value += getWorth(c.summon);
            return value + c.intrinsicValue;
        case AOEDamage:
            value = 0;
            for (Minion m : c.getOwner().opponent.minions.getOccupants()) {
                if (m.health <= c.spellDamage)
                    value += getWorth(m) + 1;
                else
                    value += c.spellDamage;
            }
            return value + c.intrinsicValue;
        case DirectDamage:
            value = 0;
            Minion optimalTarget = null;
            if (c.spellDamage == 0)
                return -1;
            if (c.getOwner() == Board.topHero) {
                for (Minion m : Board.botHero.minions.getStorage()) {
                    if (m == null)
                        continue;
                    int thisValue = getWorth(m) - AI.getWorthAfterDamage(m, c.spellDamage);
                    if (thisValue > value) {
                        value = thisValue;
                        optimalTarget = m;
                    }
                }
            } else {
                for (Minion m : Board.topHero.minions.getStorage()) {
                    if (m == null)
                        continue;
                    int thisValue = getWorth(m) - AI.getWorthAfterDamage(m, c.spellDamage);
                    if (thisValue > value) {
                        value = thisValue;
                        optimalTarget = m;
                    }
                }
            }
            // System.out.println("best target for " + c + " is " + optimalTarget);
            return value + c.intrinsicValue;
        case Debuff:
            if (c.getOwner() == Board.botHero) {
                if (Board.topHero.minions.numOccupants() == 0)
                    return -1;
                if (AI.canFavorablyTrade(AI.getBiggestThreatOf(Board.topHero))) {
                    if (!canFavorablyTrade(new SimulatedMinion(getBiggestThreatOf(Board.topHero).attack - c.spellDamage, getBiggestThreatOf(Board.topHero).health, c.getOwner()))) {
                        // if the enemy's biggest threat can no longer trade up
                        value += 1;
                    }
                    if (!AI.isVulnerable(getBiggestThreatOf(Board.topHero))) {
                        if (isVulnerable(new SimulatedMinion(getBiggestThreatOf(Board.topHero).attack - c.spellDamage, getBiggestThreatOf(Board.topHero).health, c.getOwner()))) {
                            // if the enemy's biggest threat is now vulnerable
                            value += 1;
                        }
                    }
                }
                if (getWorth(getBiggestThreatOf(Board.topHero)) > c.spellDamage) {
                    value += getBiggestThreatOf(Board.topHero).health;
                } else {
                    value += c.spellDamage;
                }
            } else {
                if (Board.botHero.minions.numOccupants() == 0)
                    return -1;
                if (AI.canFavorablyTrade(AI.getBiggestThreatOf(Board.botHero))) {
                    if (!canFavorablyTrade(new SimulatedMinion(getBiggestThreatOf(Board.botHero).attack - c.spellDamage, getBiggestThreatOf(Board.botHero).health, c.getOwner()))) {
                        // if the enemy's biggest threat can no longer trade up
                        value += 1;
                    }
                    if (!AI.isVulnerable(getBiggestThreatOf(Board.botHero))) {
                        if (isVulnerable(new SimulatedMinion(getBiggestThreatOf(Board.botHero).attack - c.spellDamage, getBiggestThreatOf(Board.botHero).health, c.getOwner()))) {
                            // if the enemy's biggest threat is now vulnerable
                            value += 1;
                        }
                    }
                }
                if (getWorth(getBiggestThreatOf(Board.botHero)) > c.spellDamage) {
                    value += getBiggestThreatOf(Board.botHero).health;
                } else {
                    value += c.spellDamage;
                }
            }
            return value + c.intrinsicValue;
        case AOEHeal:
            for (Minion m : c.getOwner().minions.getStorage()) {
                if (m == null)
                    continue;
                int missingHealth = (m.maxHealth - m.health);
                if (missingHealth > c.spellDamage) {
                    value += c.spellDamage;
                } else {
                    value += missingHealth;
                }
            }
            return value + c.intrinsicValue;
        case DirectHeal:
            // TODO: Analyze hero hp for potential hero heal
            for (Minion m : c.getOwner().minions.getStorage()) {
                if (m == null)
                    continue;
                if (isVulnerable(m)) {
                    if (!isVulnerable(new SimulatedMinion(m.attack, m.health + c.spellDamage, c.getOwner()))) {
                        // if we can save a minion
                        if (getWorth(m) > value) {
                            // the value of the card becomes teh value of the minion saved. use the value of the most important minion we can save
                            value = getWorth(m);
                        }
                    }
                }
            }
            if (value == 0) {
                value = c.spellDamage;
            }
            if (AI.isHeroVulnerable(c.getOwner())) {
                int damagePotential = 0;
                for (Minion m : enemy.minions.getStorage()) {
                    if (m == null)
                        continue;
                    damagePotential += m.attack;
                }
                // if this heal would take the hero out of lethal range, its worth alot
                if (damagePotential < c.getOwner().health + c.spellDamage)
                    return 30;
            }
            return value + c.intrinsicValue;
        case Buff:
            for (Minion m : c.getOwner().minions.getStorage()) {
                if (!canFavorablyTrade(m)) {
                    if (canFavorablyTrade(new SimulatedMinion(m.attack + c.spellDamage, m.health, m.owner))) {
                        value += AI.getTradeValue(new SimulatedMinion(m.attack + c.spellDamage, m.health, m.owner), AI.getBestTarget(new SimulatedMinion(m.attack + c.spellDamage, m.health, m.owner)));
                    }
                }
            }
            if (value == 0)
                value = c.spellDamage - 1;
            return value;
        case BattleCryMinionBuff:
            for (Minion m : c.getOwner().minions.getStorage()) {
                if (!canFavorablyTrade(m)) {
                    if (canFavorablyTrade(new SimulatedMinion(m.attack + c.spellDamage, m.health, m.owner))) {
                        value += AI.getTradeValue(new SimulatedMinion(m.attack + c.spellDamage, m.health, m.owner), AI.getBestTarget(new SimulatedMinion(m.attack + c.spellDamage, m.health, m.owner)));
                    }
                }
            }
            if (value == 0)
                value = c.spellDamage - 1;
            value += AI.getWorth(c.summon);
            return value + c.intrinsicValue;
        case ChargeMinion:
            if (c.getOwner().minions.isFull())
                return 0;
            if (getBestTarget(c.summon) == null)
                return getWorth(c.summon);
            value += getTradeValue(new SimulatedMinion(c.summon), getBestTarget(new SimulatedMinion(c.summon))) + AI.getWorthAfterCombat(c.summon, getBestTarget(c.summon));
            // System.out.println("best target for " + c + " is " + getBestTarget(new SimulatedMinion(c.summon)));
            if (value < 1)
                return getWorth(c.summon);
            return value + c.intrinsicValue;
        case BattlecryMinionDraw:
            if (c.getOwner().minions.isFull())
                return 0;
            value = getWorth(c.summon) + c.intrinsicValue;
            // number of cards we can draw.
            int numDrawable = Hero.maxHandSize - c.getOwner().hand.size();
            if (// reduce value if it will cause overdraw
            c.spellDamage > numDrawable)
                // reduce value if it will cause overdraw
                value -= (c.spellDamage - numDrawable) * 2;
            else
                value += (numDrawable - c.spellDamage);
            return value;
        case Draw:
            value = c.intrinsicValue;
            // number of cards we can draw.
            int amountDrawable = Hero.maxHandSize - c.getOwner().hand.size() + 1;
            if (// reduce value if it will cause overdraw
            c.spellDamage > amountDrawable)
                // reduce value if it will cause overdraw
                value -= (c.spellDamage - amountDrawable) * 2;
            else
                value += (amountDrawable - c.spellDamage);
            return value;
        case Special:
            value = c.getValue();
            return value + c.intrinsicValue;
        case Trap:
            for (Trap t : c.getOwner().traps.getOccupants()) {
                if (t.name.equals(c.name)) {
                    return 0;
                }
            }
            value = c.getValue();
            return value + c.intrinsicValue;
    }
    System.out.println("error finding value for " + c);
    return value + c.intrinsicValue;
}
Also used : Minion(Minions.Minion) Hero(cardgame1.Hero) Trap(Cards.CardPurpose.Trap) Trap(Traps.Trap)

Example 2 with Board

use of cardgame1.Board in project cardgame1 by joey101937.

the class AI method tradeOnBoard.

/**
 * makes favorable trades on the board until there are no more favorable trades
 * @param should we wait between making plays so the player can see whats going on?
 * @param h
 */
private static void tradeOnBoard(Hero h, boolean instant) {
    int damagePotential = 0;
    Hero enemy;
    if (h == Board.topHero)
        enemy = Board.botHero;
    else
        enemy = Board.topHero;
    for (Minion m : h.minions.getStorage()) {
        if (m == null || !m.canAttack())
            continue;
        damagePotential += m.attack;
    }
    if (damagePotential >= enemy.health) {
        for (Minion m : h.minions.getStorage()) {
            if (m == null)
                continue;
            if (!instant)
                Main.wait(speed);
            m.attack(enemy);
        }
    }
    boolean doneTrading = false;
    while (!doneTrading) {
        doneTrading = true;
        for (Minion m : h.minions.getStorage()) {
            if (m == null)
                continue;
            if (AI.canFavorablyTrade(m) && m.canAttack() && m.attack > 0) {
                if (!instant)
                    Main.wait(speed);
                m.attack(AI.getBestTarget(m));
                doneTrading = false;
            }
        }
    }
    if (h == Board.topHero) {
        if (Board.botHero.minions.numOccupants() == 0) {
            for (Minion m : h.minions.getStorage()) {
                if (m == null || !m.canAttack())
                    continue;
                if (!instant)
                    Main.wait(speed);
                m.attack(Board.botHero);
            }
        }
    } else {
        // we are playing as bothero
        if (Board.topHero.minions.numOccupants() == 0) {
            for (Minion m : h.minions.getStorage()) {
                if (m == null || !m.canAttack())
                    continue;
                if (!instant)
                    Main.wait(speed);
                m.attack(Board.topHero);
            }
        }
    }
}
Also used : Minion(Minions.Minion) Hero(cardgame1.Hero)

Example 3 with Board

use of cardgame1.Board in project cardgame1 by joey101937.

the class LegacyGUI method playButtonActionPerformed.

// GEN-LAST:event_button720ActionPerformed
private void playButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_playButtonActionPerformed
    if (this.yourDeckCombo.getSelectedItem().equals(useCustomText)) {
        if (this.loadedCustomDeckPlayer == null)
            return;
    }
    if (this.AIDeckCombo.getSelectedItem().equals(useCustomText)) {
        if (this.loadedCustomDeckAI == null)
            return;
    }
    System.out.println("test for commit");
    this.assignDecks();
    int x = 0, y = 0;
    try {
        x = Integer.parseInt(this.resX.getText());
        y = Integer.parseInt(this.resY.getText());
    } catch (NumberFormatException nfe) {
        Main.display("Invalid dimensions. Must be numeric.");
        return;
    } catch (Exception e) {
        e.printStackTrace();
    }
    Hero enemy = new Hero("AI Hero", AIDeck, enemyHeroPortrait);
    enemy.setAIControlled(true);
    Hero player = new Hero("Player Hero", PlayerDeck, SpriteHandler.ashePortrait);
    if (x < 480 || y < 480) {
        Main.display("Warning, window too small");
        return;
    }
    if (x > 2560 || y > 2560) {
        Main.display("Warning, window too big");
        return;
    }
    Board b = new Board(enemy, player, new Dimension(x, y));
    this.dispose();
}
Also used : Board(cardgame1.Board) Hero(cardgame1.Hero) Dimension(java.awt.Dimension)

Example 4 with Board

use of cardgame1.Board in project cardgame1 by joey101937.

the class AI method takeTurn.

/**
 * core of the AI player, makes plays and casts cards the most efficient way possible and ends turn when done
 * @param h the AI will make plays on behalf on this hero
 */
public static void takeTurn(Hero h) {
    Hero enemy = null;
    if (h == Board.topHero)
        enemy = Board.botHero;
    else
        enemy = Board.topHero;
    tradeOnBoard(h, false);
    playOutHand(h);
    Main.wait(speed);
    tradeOnBoard(h, false);
    for (Minion m : h.minions.getStorage()) {
        if (!AI.isHeroVulnerable(h)) {
            if (m == null)
                continue;
            Main.wait(speed);
            m.attack(enemy);
        } else {
            if (m == null)
                continue;
            Main.wait(speed);
            // if we are vulnerable, attack on board as best as possible
            m.attack(AI.getBestTarget(m));
        }
    }
    Main.wait(speed);
    Board.controller.nextTurn();
/*
        for(Card c : h.hand){
            System.out.println(c + " == " + AI.getValueOfCard(c));
        }
        */
}
Also used : Minion(Minions.Minion) Hero(cardgame1.Hero)

Aggregations

Hero (cardgame1.Hero)4 Minion (Minions.Minion)3 Trap (Cards.CardPurpose.Trap)1 Trap (Traps.Trap)1 Board (cardgame1.Board)1 Dimension (java.awt.Dimension)1