From 94bb64ad4ca6d0569112f9a4766c92ae755aa330 Mon Sep 17 00:00:00 2001 From: jendave Date: Sat, 6 Aug 2011 08:30:36 +0000 Subject: [PATCH] New Random Deck Generator - user selected colors. Moved CCnt class to inside of GenerateThemeDeck class. Added combo box selections for 2 color random deck. --- .gitattributes | 1 + src/forge/Generate2ColorDeck.java | 315 ++++++++++++++++++++++++++++++ src/forge/GenerateThemeDeck.java | 45 +++-- src/forge/Gui_NewGame.java | 47 +++++ 4 files changed, 388 insertions(+), 20 deletions(-) create mode 100644 src/forge/Generate2ColorDeck.java diff --git a/.gitattributes b/.gitattributes index 83ecd032c9a..421f38d685a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4867,6 +4867,7 @@ src/forge/GUI_Quest_Filter.java -text svneol=native#text/plain src/forge/GameAction.java -text svneol=native#text/plain src/forge/GameActionUtil.java -text svneol=native#text/plain src/forge/GameInfo.java -text svneol=native#text/plain +src/forge/Generate2ColorDeck.java -text svneol=native#text/plain src/forge/GenerateConstructedDeck.java -text svneol=native#text/plain src/forge/GenerateConstructedMultiColorDeck.java -text svneol=native#text/plain src/forge/GenerateDraftDeck.java svneol=native#text/plain diff --git a/src/forge/Generate2ColorDeck.java b/src/forge/Generate2ColorDeck.java new file mode 100644 index 00000000000..e8de69ed955 --- /dev/null +++ b/src/forge/Generate2ColorDeck.java @@ -0,0 +1,315 @@ +package forge; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +import forge.error.ErrorViewer; + +public class Generate2ColorDeck +{ + private String color1 = ""; + private String color2 = ""; + private Random r = null; + private Map ClrMap = null; + private ArrayList notColors = null; + + public Generate2ColorDeck(String Clr1, String Clr2) + { + r = new Random(); + + ClrMap = new HashMap(); + ClrMap.put("White", "W"); + ClrMap.put("Blue", "U"); + ClrMap.put("Black", "B"); + ClrMap.put("Red", "R"); + ClrMap.put("Green", "G"); + + notColors = new ArrayList(); + notColors.add("White"); + notColors.add("Blue"); + notColors.add("Black"); + notColors.add("Red"); + notColors.add("Green"); + + if (Clr1.equals("AI")) + { + // choose first color + color1 = notColors.get(r.nextInt(5)); + + // choose second color + String c2 = notColors.get(r.nextInt(5)); + while (c2.equals(color1)) + c2 = notColors.get(r.nextInt(5)); + color2 = c2; + } + else + { + color1 = Clr1; + color2 = Clr2; + } + + notColors.remove(color1); + notColors.remove(color2); + } + + public CardList get2ColorDeck(int Size) + { + String tmpDeck = ""; + CardList tDeck = new CardList(); + + Map CardCounts = new HashMap(); + + int LandsPercentage = 42; + int CreatPercentage = 34; + int SpellPercentage = 24; + + // start with all cards + CardList AllCards = AllZone.CardFactory.getAllCards(); + + // remove cards that generated decks don't like + AllCards = AllCards.filter(new CardListFilter(){ + public boolean addCard(Card c){ + return (!c.getSVar("RemAIDeck").equals("True")); + } + }); + + // reduce to cards that match the colors + CardList CL1 = AllCards.getColor(ClrMap.get(color1)); + CardList CL2 = AllCards.getColor(ClrMap.get(color2)); + + // remove multicolor cards that don't match the colors + CardListFilter clrF = new CardListFilter(){ + public boolean addCard(Card c){ + for (int i=0; i= MinCMC[0]) && (cCMC <= MaxCMC[0]); + } + }; + + // select cards to build card pools using a mana curve + for (int i=4; i>0; i--) + { + CardList Cr1CMC = Cr1.filter(cmcF); + CardList Cr2CMC = Cr2.filter(cmcF); + CardList Sp1CMC = Sp1.filter(cmcF); + CardList Sp2CMC = Sp2.filter(cmcF); + + for (int j=0; j 3) + c = Cr12.get(r.nextInt(Cr12.size())); + + tDeck.add(AllZone.CardFactory.getCard(c.getName(), Constant.Player.Computer)); + int n = CardCounts.get(c.getName()); + CardCounts.put(c.getName(), n + 1); + tmpDeck += c.getName() + " " + c.getManaCost() + "\n"; + } + + for (int i=0; i 3) + c = Sp12.get(r.nextInt(Sp12.size())); + + tDeck.add(AllZone.CardFactory.getCard(c.getName(), Constant.Player.Computer)); + int n = CardCounts.get(c.getName()); + CardCounts.put(c.getName(), n + 1); + tmpDeck += c.getName() + " " + c.getManaCost() + "\n"; + } + + // Add basic lands + // TODO: dual lands? + int numBLands = 0; + if (LandsPercentage > 0) + { + p = (float)((float)LandsPercentage * .01); + numBLands = (int)(p * (float)Size); + } + else // otherwise, just fill in the rest of the deck with basic lands + numBLands = Size - tDeck.size(); + + tmpDeck += "numBLands:" + numBLands + "\n"; + + if (numBLands > 0) // attempt to optimize basic land counts according to color representation + { + CCnt ClrCnts[] = {new CCnt("Plains", 0), + new CCnt("Island", 0), + new CCnt("Swamp", 0), + new CCnt("Mountain", 0), + new CCnt("Forest", 0)}; + + // count each card color using mana costs + // TODO: count hybrid mana differently? + // TODO: count all color letters? ie: 2 W W counts as 2 + for (int i=0;i 0) + { // calculate number of lands for each color + p = (float)ClrCnts[i].Count / (float)totalColor; + int nLand = (int)((float)numBLands * p); + tmpDeck += "numLand-" + ClrCnts[i].Color + ":" + nLand + "\n"; + + // just to prevent a null exception by the deck size fixing code + CardCounts.put(ClrCnts[i].Color, nLand); + + for (int j=0; j<=nLand; j++) + tDeck.add(AllZone.CardFactory.getCard(ClrCnts[i].Color, Constant.Player.Computer)); + } + } + } + tmpDeck += "DeckSize:" + tDeck.size() + "\n"; + + // fix under-sized or over-sized decks, due to integer arithmetic + if (tDeck.size() < Size) + { + int diff = Size - tDeck.size(); + + for (int i=0; i= 4) + c = tDeck.get(r.nextInt(tDeck.size())); + + int n = CardCounts.get(c.getName()); + tDeck.add(AllZone.CardFactory.getCard(c.getName(), Constant.Player.Computer)); + CardCounts.put(c.getName(), n + 1); + tmpDeck += "Added:" + c.getName() + "\n"; + } + } + else if (tDeck.size() > Size) + { + int diff = tDeck.size() - Size; + + for (int i=0; i Cardnames = new ArrayList(); + public int MaxCnt; + public int Percentage; + } } -class CCnt -{ - public String Color; - public int Count; - - public CCnt(String clr, int cnt) - { - Color = clr; - Count = cnt; - } -} - -class Grp -{ - public ArrayList Cardnames = new ArrayList(); - public int MaxCnt; - public int Percentage; -} \ No newline at end of file diff --git a/src/forge/Gui_NewGame.java b/src/forge/Gui_NewGame.java index 8b0e215385d..6f049af120c 100644 --- a/src/forge/Gui_NewGame.java +++ b/src/forge/Gui_NewGame.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.Random; import javax.swing.AbstractAction; import javax.swing.Action; @@ -520,6 +521,7 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA boolean humanGenerateMulti3 = human.equals("Generate 3-Color Deck"); boolean humanGenerateMulti5 = human.equals("Generate 5-Color Gold Deck"); boolean humanGenerateTheme = human.equals("Generate Theme Deck"); + boolean humanGenerate2Color = human.equals("Generate 2 Color Deck"); boolean humanRandom = human.equals("Random"); if(humanGenerate) { if(constructed) Constant.Runtime.HumanDeck[0] = generateConstructedDeck(); @@ -530,6 +532,8 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA if(constructed) Constant.Runtime.HumanDeck[0] = generateConstructed5ColorDeck(); } else if (humanGenerateTheme) { if (constructed) Constant.Runtime.HumanDeck[0] = generateConstructedThemeDeck(); + } else if (humanGenerate2Color) { + if (constructed) Constant.Runtime.HumanDeck[0] = generate2ColorDeck("H"); } else if(humanRandom) { Constant.Runtime.HumanDeck[0] = getRandomDeck(getDecks(format)); JOptionPane.showMessageDialog(null, String.format("You are using deck: %s", @@ -543,6 +547,7 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA boolean computerGenerateMulti3 = computer.equals("Generate 3-Color Deck"); boolean computerGenerateMulti5 = computer.equals("Generate 5-Color Gold Deck"); boolean computerGenerateTheme = computer.equals("Generate Theme Deck"); + boolean computerGenerate2Color = computer.equals("Generate 2 Color Deck"); boolean computerRandom = computer.equals("Random"); if(computerGenerate) { @@ -554,6 +559,8 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA if(constructed) Constant.Runtime.ComputerDeck[0] = generateConstructed5ColorDeck(); } else if (computerGenerateTheme) { if (constructed) Constant.Runtime.ComputerDeck[0] = generateConstructedThemeDeck(); + } else if (computerGenerate2Color) { + if (constructed) Constant.Runtime.ComputerDeck[0] = generate2ColorDeck("C"); } else if(computerRandom) { Constant.Runtime.ComputerDeck[0] = getRandomDeck(getDecks(format)); JOptionPane.showMessageDialog(null, String.format("The computer is using deck: %s", @@ -637,6 +644,43 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA return deck; } + private Deck generate2ColorDeck(String p) + { + ArrayList colors = new ArrayList(); + colors.add("White"); + colors.add("Blue"); + colors.add("Black"); + colors.add("Red"); + colors.add("Green"); + + Object c1 = null, c2 = null; + if (p.equals("H")) + { + c1 = AllZone.Display.getChoice("Select first color.", colors.toArray()); + + colors.remove(c1); + + c2 = AllZone.Display.getChoice("Select second color.", colors.toArray()); + } + else if (p.equals("C")) + { + Random r = new Random(); + c1 = colors.get(r.nextInt(colors.size())); + colors.remove(c1); + c2 = colors.get(r.nextInt(colors.size())); + } + Generate2ColorDeck gen = new Generate2ColorDeck(c1.toString(), c2.toString()); + CardList d = gen.get2ColorDeck(60); + + Deck deck = new Deck(Constant.GameType.Constructed); + + for (int i=0; i