Skip to content

Commit

Permalink
simplify AbstractMillAction, remove setCardsToMill have subclass over…
Browse files Browse the repository at this point in the history
…ride getMilledCards
  • Loading branch information
melvinzhang committed Oct 22, 2018
1 parent d7e856d commit 09680d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
21 changes: 11 additions & 10 deletions src/magic/model/action/AbstractMillAction.java
Expand Up @@ -6,6 +6,7 @@
import magic.model.MagicGame;
import magic.model.MagicMessage;
import magic.model.MagicPlayer;
import magic.model.MagicLocationType;

import java.util.List;

Expand All @@ -14,15 +15,12 @@
*/
public abstract class AbstractMillAction extends MagicAction {
protected final MagicPlayer player;
protected final MagicCardList milledCards = new MagicCardList();

protected AbstractMillAction(final MagicPlayer player) {
this.player = player;
}

public List<MagicCard> getMilledCards() {
return milledCards;
}
protected abstract List<MagicCard> getMilledCards();

@Override
public void undoAction(final MagicGame game) {
Expand All @@ -32,14 +30,17 @@ protected String getMillDescription(int finalCount) {
return String.format("top %d cards", finalCount);
}

protected abstract void setCardsToMill(MagicGame game);


@Override
public void doAction(final MagicGame game) {
getMilledCards().clear();
setCardsToMill(game);
final int count = getMilledCards().size();
List<MagicCard> toMill = getMilledCards();
for (final MagicCard card : toMill) {
game.doAction(new ShiftCardAction(
card,
MagicLocationType.OwnersLibrary,
MagicLocationType.Graveyard
));
}
final int count = toMill.size();
if (count > 0) {
setScore(player,ArtificialScoringSystem.getMillScore(count));
game.logAppendMessage(
Expand Down
15 changes: 5 additions & 10 deletions src/magic/model/action/MillLibraryAction.java
Expand Up @@ -6,6 +6,8 @@
import magic.model.MagicLocationType;
import magic.model.MagicPlayer;

import java.util.List;

/**
* Action that removes fixed amount of cards from players library and moves them to that player's graveyard.
*/
Expand All @@ -24,15 +26,8 @@ public MillLibraryAction(final MagicPlayer aPlayer,final int aAmount) {
amount = aAmount;
}

protected void setCardsToMill(MagicGame game) {
final MagicCardList topN = player.getLibrary().getCardsFromTop(amount);
for (final MagicCard card : topN) {
milledCards.add(card);
game.doAction(new ShiftCardAction(
card,
MagicLocationType.OwnersLibrary,
MagicLocationType.Graveyard
));
}
@Override
protected List<MagicCard> getMilledCards() {
return player.getLibrary().getCardsFromTop(amount);
}
}
12 changes: 6 additions & 6 deletions src/magic/model/action/MillLibraryUntilAction.java
Expand Up @@ -7,6 +7,8 @@
import magic.model.MagicPlayer;
import magic.model.MagicType;

import java.util.List;

/**
* Action that removes cards from players library and moves them to that player's graveyard,
* until certain card type is seen given number of times (or whole library is milled).
Expand Down Expand Up @@ -34,22 +36,20 @@ protected String getMillDescription(int finalCount) {
return String.format("top %d cards - until %d %s(s) are seen -", finalCount, cards, cardType.getDisplayName());
}

protected void setCardsToMill(MagicGame game) {
@Override
protected List<MagicCard> getMilledCards() {
final MagicCardList all = player.getLibrary().getCardsFromTop(Integer.MAX_VALUE);
final MagicCardList milledCards = new MagicCardList();
int seenTargets = 0;
for (final MagicCard card : all) {
milledCards.add(card);
game.doAction(new ShiftCardAction(
card,
MagicLocationType.OwnersLibrary,
MagicLocationType.Graveyard
));
if (card.hasType(cardType)) {
seenTargets++;
}
if (seenTargets >= cards) {
break;
}
}
return milledCards;
}
}

0 comments on commit 09680d7

Please sign in to comment.