From e0f9e2ec9a6433bf898d1fe41d77a9378d491990 Mon Sep 17 00:00:00 2001 From: jendave Date: Sat, 6 Aug 2011 15:18:15 +0000 Subject: [PATCH] - Some more cleanup related to PlayerZone.remove and moveTo --- src/forge/AIPlayer.java | 20 ++++++++++---------- src/forge/CardFactory.java | 8 ++++++-- src/forge/CardFactory_Instants.java | 12 ++++-------- src/forge/CardFactory_Sorceries.java | 20 ++++++++++---------- src/forge/GameAction.java | 28 +++++++--------------------- src/forge/HumanPlayer.java | 14 +++++++------- src/forge/MagicStack.java | 3 +-- src/forge/Player.java | 11 +++++------ src/forge/PlayerZone.java | 1 - 9 files changed, 50 insertions(+), 67 deletions(-) diff --git a/src/forge/AIPlayer.java b/src/forge/AIPlayer.java index 2f8d65b3382..94c05f29da7 100644 --- a/src/forge/AIPlayer.java +++ b/src/forge/AIPlayer.java @@ -180,7 +180,7 @@ public class AIPlayer extends Player{ protected void doScry(final CardList topN, final int N) { int num = N; for(int i = 0; i < num; i++) { - boolean b = false; + boolean bottom = false; if(topN.get(i).getType().contains("Basic")) { CardList bl = AllZoneUtil.getPlayerCardsInPlay(AllZone.ComputerPlayer); bl = bl.filter(new CardListFilter() { @@ -191,8 +191,7 @@ public class AIPlayer extends Player{ } }); - if(bl.size() > 5) // if control more than 5 Basic land, probably don't need more - b = true; + bottom = bl.size() > 5; // if control more than 5 Basic land, probably don't need more } else if(topN.get(i).getType().contains("Creature")) { CardList cl = AllZoneUtil.getPlayerCardsInPlay(AllZone.ComputerPlayer); cl = cl.filter(new CardListFilter() { @@ -203,20 +202,21 @@ public class AIPlayer extends Player{ } }); - if(cl.size() > 5) // if control more than 5 Creatures, probably don't need more - b = true; + bottom = cl.size() > 5; // if control more than 5 Creatures, probably don't need more } - if(b == true) { - AllZone.Computer_Library.add(topN.get(i)); - topN.remove(i); + if(bottom) { + Card c = topN.get(i); + AllZone.GameAction.moveToBottomOfLibrary(c); + topN.remove(c); } } num = topN.size(); - if(num > 0) for(int i = 0; i < num; i++) // put the rest on top in random order + for(int i = 0; i < num; i++) // put the rest on top in random order { Random rndm = new Random(); int r = rndm.nextInt(topN.size()); - AllZone.Computer_Library.add(topN.get(r), 0); + Card c = topN.get(r); + AllZone.GameAction.moveToLibrary(c); topN.remove(r); } } diff --git a/src/forge/CardFactory.java b/src/forge/CardFactory.java index 5d8e93b6146..3dd5406b033 100644 --- a/src/forge/CardFactory.java +++ b/src/forge/CardFactory.java @@ -5713,8 +5713,12 @@ public class CardFactory implements NewConstants { PlayerZone library = AllZone.getZone(Constant.Zone.Library, card.getController()); if(library.size() != 0) { - Card c = library.get(0); - library.remove(0); + Card c = library.get(0); + library.remove(c); + + // TODO: Necro really exiles face down, but for now we'll just do it this way + // c.setIsFaceDown(true); + // AllZone.GameAction.exile(c); necroCards.add(c); //add card to necro so that it goes into hand at end of turn AllZone.EndOfTurn.addAt(necro); } diff --git a/src/forge/CardFactory_Instants.java b/src/forge/CardFactory_Instants.java index 3612923631c..71c9a727355 100644 --- a/src/forge/CardFactory_Instants.java +++ b/src/forge/CardFactory_Instants.java @@ -1432,28 +1432,24 @@ public class CardFactory_Instants { CardList top = new CardList(); PlayerZone library = AllZone.getZone(Constant.Zone.Library, card.getController()); - Card c; int j = 4; if(library.size() < 4) j = library.size(); for(int i = 0; i < j; i++) { - c = library.get(0); - library.remove(0); - top.add(c); + top.add(library.get(0)); } - if(top.size() >= 1) { + if(top.size() > 0) { //let user get choice Card chosen = AllZone.Display.getChoice("Choose a card to put into your hand", top.toArray()); top.remove(chosen); //put card in hand - PlayerZone hand = AllZone.getZone(Constant.Zone.Hand, card.getController()); - hand.add(chosen); + AllZone.GameAction.moveToHand(chosen); //add cards to bottom of library for(int i = 0; i < top.size(); i++) - library.add(top.get(i)); + AllZone.GameAction.moveToBottomOfLibrary(top.get(i)); } }//resolve() };//SpellAbility diff --git a/src/forge/CardFactory_Sorceries.java b/src/forge/CardFactory_Sorceries.java index 6ab7f356224..1d29d639d64 100644 --- a/src/forge/CardFactory_Sorceries.java +++ b/src/forge/CardFactory_Sorceries.java @@ -2818,19 +2818,20 @@ public class CardFactory_Sorceries { for(int i = 0; i < 5 && i < limit; i++) { top.add(AllZone.Computer_Library.get(0)); - AllZone.Computer_Library.remove(0); } //put creature card in hand, if there is one CardList creature = top.getType("Creature"); - if(creature.size() != 0) { - AllZone.Computer_Hand.add(creature.get(0)); - top.remove(creature.get(0)); + if(creature.size() > 0) { + Card best = CardFactoryUtil.AI_getBestCreature(creature); + top.remove(best); + AllZone.GameAction.moveToHand(best); } //put cards on bottom of library for(int i = 0; i < top.size(); i++) - AllZone.Computer_Library.add(top.get(i)); + AllZone.GameAction.moveToBottomOfLibrary(top.get(i)); + }//computerResolve() public void humanResolve() { @@ -2844,15 +2845,14 @@ public class CardFactory_Sorceries { //optional, select a creature Object o = AllZone.Display.getChoiceOptional("Select a creature", list.toArray()); if(o != null && ((Card) o).isCreature()) { - AllZone.GameAction.moveTo(hand, (Card) o); list.remove((Card) o); + AllZone.GameAction.moveToHand((Card) o); } //put remaining cards on the bottom of the library - for(int i = 0; i < list.size(); i++) { - library.remove(list.get(i)); - library.add(list.get(i)); - } + for(int i = 0; i < list.size(); i++) + AllZone.GameAction.moveToBottomOfLibrary(list.get(i)); + }//resolve() }; card.clearSpellAbility(); diff --git a/src/forge/GameAction.java b/src/forge/GameAction.java index b82f0637d25..e307cd421c3 100644 --- a/src/forge/GameAction.java +++ b/src/forge/GameAction.java @@ -192,14 +192,13 @@ public class GameAction { return moveTo(play, c); } + public void moveToBottomOfLibrary(Card c) { + int pos = AllZoneUtil.getPlayerCardsInLibrary(c.getOwner()).size(); + moveToLibrary(c, pos); + } + public void moveToTopOfLibrary(Card c) { - PlayerZone p = AllZone.getZone(c); - PlayerZone library = AllZone.getZone(Constant.Zone.Library, c.getOwner()); - - if(p != null) p.remove(c); - if(!c.isToken()) c = AllZone.CardFactory.copyCard(c); - - library.add(c, 0); + moveToLibrary(c, 0); } public void moveToLibrary(Card c) { @@ -215,20 +214,7 @@ public class GameAction { library.add(c, libPosition); } - - /** - * moves a card from whichever Zone it's in to the bottom of its owner's library - * - * @param c the card to move - */ - public void moveToBottomOfLibrary(Card c) { - PlayerZone p = AllZone.getZone(c); - PlayerZone lib = AllZone.getZone(Constant.Zone.Library, c.getOwner()); - if( p != null ) p.remove(c); - if(!c.isToken()) lib.add(c); - } - - + public boolean AI_discardNumType(int numDiscard, String[] uTypes, SpellAbility sa) { CardList hand = new CardList(); hand.addAll(AllZone.getZone(Constant.Zone.Hand, AllZone.ComputerPlayer).getCards()); diff --git a/src/forge/HumanPlayer.java b/src/forge/HumanPlayer.java index 0f8881039d3..f67d2a1d24c 100644 --- a/src/forge/HumanPlayer.java +++ b/src/forge/HumanPlayer.java @@ -102,23 +102,23 @@ public class HumanPlayer extends Player{ protected void doScry(final CardList topN, final int N) { int num = N; for(int i = 0; i < num; i++) { - Object o; - o = AllZone.Display.getChoiceOptional("Put on bottom of library.",topN.toArray()); + Object o = AllZone.Display.getChoiceOptional("Put on bottom of library.",topN.toArray()); if(o != null) { Card c = (Card) o; topN.remove(c); - AllZone.Human_Library.add(c); - } else // no card chosen for the bottom - break; + AllZone.GameAction.moveToBottomOfLibrary(c); + } + else // no card chosen for the bottom + break; } num = topN.size(); - if(num > 0) for(int i = 0; i < num; i++) { + for(int i = 0; i < num; i++) { Object o; o = AllZone.Display.getChoice("Put on top of library.", topN.toArray()); if(o != null) { Card c = (Card) o; topN.remove(c); - AllZone.Human_Library.add(c, 0); + AllZone.GameAction.moveToLibrary(c); } // no else - a card must have been chosen } diff --git a/src/forge/MagicStack.java b/src/forge/MagicStack.java index c3bd288d59e..e21372202c7 100644 --- a/src/forge/MagicStack.java +++ b/src/forge/MagicStack.java @@ -492,7 +492,7 @@ public class MagicStack extends MyObservable { continue; } Card revealed = AllZone.Human_Library.get(0); - AllZone.Human_Library.remove(0); + revealMsg.append(revealed.getName()); if(!revealed.isType("Creature")) { @@ -522,7 +522,6 @@ public class MagicStack extends MyObservable { continue; } Card revealed = AllZone.Computer_Library.get(0); - AllZone.Computer_Library.remove(0); revealMsg.append(revealed.getName()); if(!revealed.isType("Creature")) { diff --git a/src/forge/Player.java b/src/forge/Player.java index 84203959478..2dd9d814daa 100644 --- a/src/forge/Player.java +++ b/src/forge/Player.java @@ -530,20 +530,20 @@ public abstract class Player extends MyObservable{ public void drawCards(int n) { PlayerZone library = AllZone.getZone(Constant.Zone.Library, this); - PlayerZone hand = AllZone.getZone(Constant.Zone.Hand, this); + for(int i = 0; i < n; i++) { // todo: any draw replacements would go here, not just Dredge if(getDredge().size() == 0 || !dredge()) { - doDraw(library, hand); + doDraw(library); } } } - private void doDraw(PlayerZone library, PlayerZone hand) { + private void doDraw(PlayerZone library) { if(library.size() != 0) { Card c = library.get(0); - library.remove(0); - hand.add(c); + AllZone.GameAction.moveToHand(c); + setLastDrawnCard(c); c.setDrawnThisTurn(true); numDrawnThisTurn++; @@ -740,7 +740,6 @@ public abstract class Player extends MyObservable{ numScry = Math.min(numScry, library.size()); for(int i = 0; i < numScry; i++) { topN.add(library.get(0)); - library.remove(0); } doScry(topN, topN.size()); } diff --git a/src/forge/PlayerZone.java b/src/forge/PlayerZone.java index 83c84773038..ab4c7e4680b 100644 --- a/src/forge/PlayerZone.java +++ b/src/forge/PlayerZone.java @@ -19,7 +19,6 @@ interface IPlayerZone public Card get(int index); public void remove(Object o); - public void remove(int index); public void setCards(Card c[]); public Card[] getCards();