public void endTurn(Deck deckPlayer0, Deck deckPlayer1) throws HSInvalidPlayerIndexException {
    p0_hero_.endTurn(0, this, deckPlayer0, deckPlayer1);
    p1_hero_.endTurn(1, this, deckPlayer0, deckPlayer1);
    for (int index = 0; index < p0_minions_.size(); ++index) {
      Minion targetMinion = p0_minions_.get(index);
      try {
        targetMinion.endTurn(0, this, deckPlayer0, deckPlayer1);
      } catch (HSException e) {
        e.printStackTrace();
      }
    }
    for (int index = 0; index < p1_minions_.size(); ++index) {
      Minion targetMinion = p1_minions_.get(index);
      try {
        targetMinion.endTurn(1, this, deckPlayer0, deckPlayer1);
      } catch (HSException e) {
        e.printStackTrace();
      }
    }

    ArrayList<Minion> toRemove = new ArrayList<Minion>();
    for (Minion targetMinion : p0_minions_) {
      if (targetMinion.getTotalHealth() <= 0) toRemove.add(targetMinion);
    }
    for (Minion minion : toRemove) p0_minions_.remove(minion);

    toRemove.clear();
    for (Minion targetMinion : p1_minions_) {
      if (targetMinion.getTotalHealth() <= 0) toRemove.add(targetMinion);
    }
    for (Minion minion : toRemove) p1_minions_.remove(minion);
  }
  public JSONObject toJSON() {
    JSONObject json = new JSONObject();
    json.put("p0_mana", p0_mana_);
    json.put("p1_mana", p1_mana_);
    json.put("p0_maxMana", p0_maxMana_);
    json.put("p1_maxMana", p0_maxMana_);
    json.put("p0_deckPos", p0_deckPos_);
    json.put("p1_deckPos", p1_deckPos_);

    JSONArray p0_minions = new JSONArray();
    for (Minion minion : p0_minions_) p0_minions.put(minion.toJSON());
    json.put("p0_minions", p0_minions);

    JSONArray p1_minions = new JSONArray();
    for (Minion minion : p1_minions_) p1_minions.put(minion.toJSON());
    json.put("p1_minions", p1_minions);

    JSONArray p0_hand = new JSONArray();
    for (Card card : p0_hand_) p0_hand.put(card.toJSON());
    json.put("p0_hand", p0_hand);

    JSONArray p1_hand = new JSONArray();
    for (Card card : p1_hand_) p1_hand.put(card.toJSON());
    json.put("p1_hand", p1_hand);

    json.put("p0_hero", p0_hero_.toJSON());
    json.put("p1_hero", p1_hero_.toJSON());

    json.put("p0_fatigue", p0_fatigueDamage_);
    json.put("p1_fatigue", p1_fatigueDamage_);
    json.put("p0_spellDamage", p0_spellDamage_);
    json.put("p1_spellDamage", p1_spellDamage_);
    return json;
  }
 private String simpleString() {
   StringBuffer stringBuffer = new StringBuffer();
   stringBuffer.append("[");
   stringBuffer.append("P0_health:").append(p0_hero_.getHealth()).append(", ");
   stringBuffer.append("P0_mana:").append(p0_mana_).append(", ");
   stringBuffer.append("P1_health:").append(p1_hero_.getHealth()).append(", ");
   stringBuffer.append("P1_mana:").append(p1_mana_).append(", ");
   stringBuffer.append("]");
   return stringBuffer.toString();
 }
 /**
  * Reset all minions to clear their has_attacked state.
  *
  * <p>Call this function at the beginning of each turn
  */
 public void resetMinions() {
   p0_hero_.hasAttacked(false);
   p0_hero_.hasBeenUsed(false);
   p1_hero_.hasAttacked(false);
   p1_hero_.hasBeenUsed(false);
   for (Minion minion : p0_minions_) {
     minion.hasAttacked(false);
     minion.hasBeenUsed(false);
   }
   for (Minion minion : p1_minions_) {
     minion.hasAttacked(false);
     minion.hasBeenUsed(false);
   }
 }
  public JSONObject toJSON() {
    JSONObject json = new JSONObject();

    json.put("name", name);
    json.put("playerId", playerId);
    json.put("hero", hero.toJSON());
    if (mana != maxMana) json.put("mana", mana);
    if (maxMana > 0) json.put("maxMana", maxMana);
    json.put("deckPos", deckPos);
    if (overload > 0) json.put("overload", overload);
    if (fatigueDamage > 0) json.put("fatigueDamage", fatigueDamage);

    if (minions.size() > 0) {
      JSONArray array = new JSONArray();
      for (Minion minion : minions) {
        array.put(minion.toJSON());
      }
      json.put("minions", array);
    }

    if (hand.size() > 0) {
      JSONArray array = new JSONArray();
      for (Card card : hand) {
        array.put(card.toJSON());
      }
      json.put("hand", array);
    }

    if (numCardsUsed > 0) {
      json.put("numCardsUsed", numCardsUsed);
    }

    return json;
  }
 @Override
 public int hashCode() {
   int hs = p0_hand_.size();
   if (hs < 0) hs = 0;
   int res = hs + p0_minions_.size() * 10 + p1_minions_.size() * 100;
   res += (p0_mana_ <= 0 ? 0 : (p0_mana_ - 1) * 1000);
   res += ((p0_hero_.getHealth() + p1_hero_.getHealth()) % 100) * 10000;
   int th = 0;
   if (hs > 0) {
     Card cc = p0_hand_.get(0);
     try {
       Minion mm = (Minion) cc;
       th += (cc.hasBeenUsed() ? 1 : 0) + mm.getAttack() + mm.getHealth() + cc.getMana();
     } catch (ClassCastException e) {
       th += (cc.hasBeenUsed() ? 1 : 0) + cc.getMana();
     }
   }
   if (hs > 1) {
     Card cc = p0_hand_.get(1);
     try {
       Minion mm = (Minion) cc;
       th += (cc.hasBeenUsed() ? 1 : 0) + mm.getAttack() + mm.getHealth() + cc.getMana();
     } catch (ClassCastException e) {
       th += (cc.hasBeenUsed() ? 1 : 0) + cc.getMana();
     }
   }
   res += (th % 10) * 1000000;
   int mh0 = 0;
   if (p0_minions_.size() > 0) {
     mh0 += p0_minions_.get(0).getHealth();
   }
   if (p0_minions_.size() > 1) {
     mh0 += p0_minions_.get(1).getHealth();
   }
   res += (mh0 % 100) * 10000000;
   int mh1 = 0;
   if (p1_minions_.size() > 0) {
     mh1 += p1_minions_.get(0).getHealth();
   }
   if (p1_minions_.size() > 1) {
     mh1 += p1_minions_.get(1).getHealth();
   }
   res += (mh1 % 20) * 100000000;
   return res;
 }
 public Card drawNextCardFromDeck() {
   Card card = drawFromDeck(deckPos);
   if (card == null) {
     // no more card left in deck, take fatigue damage
     hero.takeDamage(fatigueDamage);
     ++fatigueDamage;
   } else {
     placeCardHand(card);
     ++deckPos;
   }
   return card;
 }
 @Override
 public int hashCode() {
   int hash = 1;
   hash = hash * 31 + (null == name ? 0 : name.hashCode());
   hash = hash * 31 + playerId;
   hash = hash * 31 + (null == hero ? 0 : hero.hashCode());
   hash = hash * 31 + (null == deck ? 0 : deck.hashCode());
   hash = hash * 31 + mana;
   hash = hash * 31 + maxMana;
   hash = hash * 31 + deckPos;
   hash = hash * 31 + fatigueDamage;
   hash = hash * 31 + (null == minions ? 0 : minions.hashCode());
   hash = hash * 31 + (null == hand ? 0 : hand.hashCode());
   hash = hash * 31 + overload;
   hash = hash * 31 + numCardsUsed;
   return hash;
 }
  @Override
  public boolean equals(Object other) {

    if (other == null) return false;

    if (this.getClass() != other.getClass()) return false;

    PlayerModel otherPlayer = (PlayerModel) other;

    if (playerId != otherPlayer.playerId) return false;
    if (mana != otherPlayer.mana) return false;
    if (maxMana != otherPlayer.maxMana) return false;
    if (overload != otherPlayer.overload) return false;
    if (deckPos != otherPlayer.deckPos) return false;
    if (fatigueDamage != otherPlayer.fatigueDamage) return false;

    if (!name.equals(otherPlayer.name)) return false;
    if (!hero.equals(otherPlayer.hero)) return false;
    if (deck != null && !deck.equals(otherPlayer.deck)) return false;
    if (!minions.equals(otherPlayer.minions)) return false;
    if (!hand.equals(otherPlayer.hand)) return false;
    if (numCardsUsed != otherPlayer.numCardsUsed) return false;
    return true;
  }
Exemple #10
0
  @Test
  public void test0() throws HSException {

    // null case
    Minion target = board.data_.getCharacter(1, 0);
    Minion minion = board.data_.getMinion_p0(0);
    HearthTreeNode ret = minion.attack(1, target, board, deck, null);

    assertFalse(ret == null);
    assertEquals(board.data_.getNumCards_hand(), 1);
    assertEquals(board.data_.getNumMinions_p0(), 2);
    assertEquals(board.data_.getNumMinions_p1(), 2);
    assertEquals(board.data_.getMana_p0(), 8);
    assertEquals(board.data_.getMana_p1(), 8);
    assertEquals(board.data_.getHero_p0().getHealth(), 30);
    assertEquals(board.data_.getHero_p1().getHealth(), 28);
    assertEquals(board.data_.getMinion_p0(0).getHealth(), 2);
    assertEquals(board.data_.getMinion_p0(1).getHealth(), 7);
    assertEquals(board.data_.getMinion_p1(0).getHealth(), 2);
    assertEquals(board.data_.getMinion_p1(1).getHealth(), 7);

    assertEquals(board.data_.getMinion_p0(0).getTotalAttack(), 2);
    assertEquals(board.data_.getMinion_p0(1).getTotalAttack(), 7);
    assertEquals(board.data_.getMinion_p1(0).getTotalAttack(), 2);
    assertEquals(board.data_.getMinion_p1(1).getTotalAttack(), 7);

    target = board.data_.getCharacter(1, 0);
    Hero hero = board.data_.getHero_p0();
    ret = hero.useHeroAbility(1, target, board, deck, null);
    assertEquals(board.data_.getNumCards_hand(), 1);
    assertEquals(board.data_.getNumMinions_p0(), 2);
    assertEquals(board.data_.getNumMinions_p1(), 2);
    assertEquals(board.data_.getMana_p0(), 6);
    assertEquals(board.data_.getMana_p1(), 8);
    assertEquals(board.data_.getHero_p0().getHealth(), 30);
    assertEquals(board.data_.getHero_p1().getHealth(), 26);
    assertEquals(board.data_.getMinion_p0(0).getHealth(), 2);
    assertEquals(board.data_.getMinion_p0(1).getHealth(), 7);
    assertEquals(board.data_.getMinion_p1(0).getHealth(), 2);
    assertEquals(board.data_.getMinion_p1(1).getHealth(), 7);

    assertEquals(board.data_.getMinion_p0(0).getTotalAttack(), 2);
    assertEquals(board.data_.getMinion_p0(1).getTotalAttack(), 7);
    assertEquals(board.data_.getMinion_p1(0).getTotalAttack(), 2);
    assertEquals(board.data_.getMinion_p1(1).getTotalAttack(), 7);

    minion.hasAttacked(false);
    target = board.data_.getCharacter(1, 2);
    ret = minion.attack(1, target, board, deck, null);
    assertEquals(board.data_.getNumCards_hand(), 1);
    assertEquals(board.data_.getNumMinions_p0(), 1);
    assertEquals(board.data_.getNumMinions_p1(), 2);
    assertEquals(board.data_.getMana_p0(), 6);
    assertEquals(board.data_.getMana_p1(), 8);
    assertEquals(board.data_.getHero_p0().getHealth(), 30);
    assertEquals(board.data_.getHero_p1().getHealth(), 26);
    assertEquals(board.data_.getMinion_p0(0).getHealth(), 7);
    assertEquals(board.data_.getMinion_p1(0).getHealth(), 2);
    assertEquals(board.data_.getMinion_p1(1).getHealth(), 5);

    assertEquals(board.data_.getMinion_p0(0).getTotalAttack(), 6);
    assertEquals(board.data_.getMinion_p1(0).getTotalAttack(), 2);
    assertEquals(board.data_.getMinion_p1(1).getTotalAttack(), 7);

    minion.hasAttacked(false);
    target = board.data_.getCharacter(1, 2);
    ret = hero.useHeroAbility(1, target, board, deck, null);
    assertTrue(ret == null);
    assertEquals(board.data_.getNumCards_hand(), 1);
    assertEquals(board.data_.getNumMinions_p0(), 1);
    assertEquals(board.data_.getNumMinions_p1(), 2);
    assertEquals(board.data_.getMana_p0(), 6);
    assertEquals(board.data_.getMana_p1(), 8);
    assertEquals(board.data_.getHero_p0().getHealth(), 30);
    assertEquals(board.data_.getHero_p1().getHealth(), 26);
    assertEquals(board.data_.getMinion_p0(0).getHealth(), 7);
    assertEquals(board.data_.getMinion_p1(0).getHealth(), 2);
    assertEquals(board.data_.getMinion_p1(1).getHealth(), 5);

    assertEquals(board.data_.getMinion_p0(0).getTotalAttack(), 6);
    assertEquals(board.data_.getMinion_p1(0).getTotalAttack(), 2);
    assertEquals(board.data_.getMinion_p1(1).getTotalAttack(), 7);
  }
  public boolean equals(Object other) {
    if (other == null) {
      return false;
    }

    if (this.getClass() != other.getClass()) {
      return false;
    }

    if (p0_mana_ != ((BoardState) other).p0_mana_) return false;
    if (p1_mana_ != ((BoardState) other).p1_mana_) return false;
    if (p0_maxMana_ != ((BoardState) other).p0_maxMana_) return false;
    if (p1_maxMana_ != ((BoardState) other).p1_maxMana_) return false;

    if (!p0_hero_.equals(((BoardState) other).p0_hero_)) {
      return false;
    }

    if (!p1_hero_.equals(((BoardState) other).p1_hero_)) {
      return false;
    }

    if (p0_deckPos_ != ((BoardState) other).p0_deckPos_) return false;

    if (p1_deckPos_ != ((BoardState) other).p1_deckPos_) return false;

    if (p0_fatigueDamage_ != ((BoardState) other).p0_fatigueDamage_) return false;

    if (p1_fatigueDamage_ != ((BoardState) other).p1_fatigueDamage_) return false;

    if (p0_spellDamage_ != ((BoardState) other).p0_spellDamage_) return false;

    if (p1_spellDamage_ != ((BoardState) other).p1_spellDamage_) return false;

    if (p0_minions_.size() != ((BoardState) other).p0_minions_.size()) return false;
    if (p1_minions_.size() != ((BoardState) other).p1_minions_.size()) return false;
    if (p0_hand_.size() != ((BoardState) other).p0_hand_.size()) return false;

    for (int i = 0; i < p0_minions_.size(); ++i) {
      if (!p0_minions_.get(i).equals(((BoardState) other).p0_minions_.get(i))) {
        return false;
      }
    }

    for (int i = 0; i < p1_minions_.size(); ++i) {
      if (!p1_minions_.get(i).equals(((BoardState) other).p1_minions_.get(i))) {
        return false;
      }
    }

    for (int i = 0; i < p0_hand_.size(); ++i) {
      if (!p0_hand_.get(i).equals(((BoardState) other).p0_hand_.get(i))) {
        return false;
      }
    }

    for (int i = 0; i < p1_hand_.size(); ++i) {
      if (!p1_hand_.get(i).equals(((BoardState) other).p1_hand_.get(i))) {
        return false;
      }
    }

    // More logic here to be discuss below...
    return true;
  }
 public boolean isAlive_p1() {
   return p1_hero_.getHealth() > 0;
 }
  // TODO Unused?
  public HearthTreeNode perform(HearthTreeNode boardState) throws HSException {
    HearthTreeNode toRet = boardState;
    PlayerModel actingPlayer =
        actionPerformerPlayerSide != null
            ? boardState.data_.modelForSide(actionPerformerPlayerSide)
            : null;
    PlayerModel targetPlayer =
        targetPlayerSide != null ? boardState.data_.modelForSide(targetPlayerSide) : null;

    switch (verb_) {
      case USE_CARD:
        {
          Card card = actingPlayer.getHand().get(cardOrCharacterIndex_);
          toRet = card.useOn(targetPlayerSide, targetCharacterIndex, toRet);
        }
        break;
      case HERO_ABILITY:
        {
          Hero hero = actingPlayer.getHero();
          Minion target = targetPlayer.getCharacter(targetCharacterIndex);
          toRet = hero.useHeroAbility(targetPlayerSide, target, toRet);
        }
        break;
      case ATTACK:
        {
          Minion attacker =
              actingPlayer.getCharacter(CharacterIndex.fromInteger(cardOrCharacterIndex_));
          toRet = attacker.attack(targetPlayerSide, targetCharacterIndex, toRet);
        }
        break;
      case UNTARGETABLE_BATTLECRY:
        {
          Minion minion =
              actingPlayer.getCharacter(CharacterIndex.fromInteger(cardOrCharacterIndex_));
          toRet = minion.useUntargetableBattlecry(targetCharacterIndex, toRet);
          break;
        }
      case TARGETABLE_BATTLECRY:
        {
          Minion minion =
              actingPlayer.getCharacter(CharacterIndex.fromInteger(cardOrCharacterIndex_));
          toRet = minion.useTargetableBattlecry(targetPlayerSide, targetCharacterIndex, toRet);
          break;
        }
      case START_TURN:
        {
          toRet = new HearthTreeNode(Game.beginTurn(boardState.data_.deepCopy()));
          break;
        }
      case END_TURN:
        {
          toRet = new HearthTreeNode(Game.endTurn(boardState.data_.deepCopy()).flipPlayers());
          break;
        }
      case DO_NOT_USE_CARD:
        {
          for (Card c : actingPlayer.getHand()) {
            c.hasBeenUsed(true);
          }
          break;
        }
      case DO_NOT_ATTACK:
        {
          for (Minion minion : actingPlayer.getMinions()) {
            minion.hasAttacked(true);
          }
          actingPlayer.getHero().hasAttacked(true);
          break;
        }
      case DO_NOT_USE_HEROPOWER:
        {
          actingPlayer.getHero().hasBeenUsed(true);
          break;
        }
      case RNG:
        {
          // We need to perform the current state again if the children don't exist yet. This can
          // happen in certain replay scenarios.
          // Do not do this if the previous action was *also* RNG or we will end up in an infinite
          // loop.
          if (toRet.isLeaf() && boardState.getAction().verb_ != Verb.RNG) {
            boardState.data_.getCurrentPlayer().addNumCardsUsed((byte) -1); // do not double count
            toRet = boardState.getAction().perform(boardState);
          }
          // RNG has declared this child happened
          toRet = toRet.getChildren().get(cardOrCharacterIndex_);
          break;
        }
      case DRAW_CARDS:
        {
          // Note, this action only supports drawing cards from the deck. Cards like Ysera or
          // Webspinner need to be implemented using RNG children.
          for (int indx = 0; indx < cardOrCharacterIndex_; ++indx) {
            actingPlayer.drawNextCardFromDeck();
          }
          break;
        }
    }
    return toRet;
  }