diff --git a/Card.java b/Card.java index 3876e03..bc83ab5 100644 --- a/Card.java +++ b/Card.java @@ -70,7 +70,8 @@ public class Card{ return "Card [cardName=" + cardName + ", creatureType=" + creatureType + ", description=" + description + ", image=" + image + ", manaCost=" + manaCost + ", power=" + power + ", toughness=" + toughness + "]"; } - + + public Card() {} public Card(String cardName, int manaCost, int power, int toughness, String description, String image, String creatureType) { this.cardName = cardName; diff --git a/Deck.java b/Deck.java index 7d3642b..d7b34e3 100644 --- a/Deck.java +++ b/Deck.java @@ -5,6 +5,7 @@ public class Deck { protected ArrayList topdeck; protected ArrayList discard; protected ArrayList hand; + public Card drawCard(){ if (topdeck.size()<=0) return null; @@ -15,25 +16,15 @@ public class Deck { } public Deck() { - topdeck=new ArrayList(); - discard=new ArrayList(); - hand=new ArrayList(); - for (int i = 0; i < 4; i++){ - topdeck.add(new Card("Devouring Dragon", 5, 5, 6, "He does to people what Greg does to booty.", "", "Dragon")); - topdeck.add(new Card("Dragon Worshipper", 1, 1, 1, "He spends his days praying to dragons.", "", "Human")); - topdeck.add(new Card("Dragon Whelp", 2, 3, 2, "He'll get there.", "", "Dragon")); - topdeck.add(new Card("Dragon Egg", 1, 0, 2, "Close to hatching!", "", "Egg")); - topdeck.add(new Card("Lingering Flame", 3, 4, 3, "He lives among the dragons.", "", "Elemental")); - topdeck.add(new Card("Dragonguard Sentry", 1, 1, 2, "Devoted to the protection of the dragonflight.", "", "Human")); - topdeck.add(new Card("Withering Flamewitch", 2, 4, 2, "Her flame bites deep.", "", "Human")); - topdeck.add(new Card("Final Examination", 6, 9000, 0, "Oh dear, Greg.", "", "Parchment")); - topdeck.add(new Card("Drunken Dragon", 3, 4, 1, "He's trying.", "", "Dragon")); - } - for (int i = 0; i < 24; i++){ - topdeck.add(new Card("Isle of Power", -1, 0, 0, "Invoke this for mana.", "", "Land")); + SQLDriver d = new SQLDriver(); + d.setupTable(); + topdeck = new ArrayList(); + hand = new ArrayList(); + discard = new ArrayList(); + for (int i = 0; i < 60; i++){ + topdeck.add(d.getRandomCard()); } Shuffle(); - for (int i = 0; i < 7; i++){ drawCard(); } diff --git a/Makefile b/Makefile index 01849b9..62884b9 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,9 @@ JCC = javac MAIN_CLASS = Driver OUT_JAR = MTGClone.jar - -JFLAGS = -g +SQLITE_JDBC_JAR = sqlite-jdbc-3.32.3.2.jar +SQLITE_JDBC = https://github.com/xerial/sqlite-jdbc/releases/download/3.32.3.2/$(SQLITE_JDBC_JAR) +JFLAGS = -g -classpath ".:$(SQLITE_JDBC_JAR)" default: @echo \"make build\" to compile Java classes @@ -10,7 +11,10 @@ default: @echo \"make jar\" to compile executable jar @echo \"make clean\" to clean up artifacts -build: +sqlite_jdbc: + wget -N $(SQLITE_JDBC) + +build: sqlite_jdbc $(JCC) $(JFLAGS) $(MAIN_CLASS).java run: jar @@ -18,7 +22,7 @@ run: jar jar: build @echo "Manifest-Version: 1.0" > manifest.txt - @echo "Class-Path: ." >> manifest.txt + @echo "Class-Path: ./sqlite-jdbc-3.32.3.2.jar" >> manifest.txt @echo "Main-Class: $(MAIN_CLASS)" >> manifest.txt @echo "" >> manifest.txt jar -cmf manifest.txt $(OUT_JAR) *.class @@ -27,5 +31,7 @@ jar: build clean: $(RM) *.class + $(RM) cards.db $(RM) manifest.txt $(RM) $(OUT_JAR) + $(RM) $(SQLITE_JDBC_JAR) diff --git a/SQLDriver.java b/SQLDriver.java new file mode 100644 index 0000000..bdfa701 --- /dev/null +++ b/SQLDriver.java @@ -0,0 +1,123 @@ +import java.sql.*; +import java.util.*; + +public class SQLDriver { + protected Connection c = null; + + public boolean tryConnect() { + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:cards.db"); + } catch (Exception e) { + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + return false; + } + return true; + } + + public Card getRandomCard() { + try { + c = DriverManager.getConnection("jdbc:sqlite:cards.db"); + c.setAutoCommit(false); + + Statement stmt = c.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM CARDS ORDER BY RANDOM() LIMIT 1;"); + String cardName = ""; + int manaCost = 0; + int power = 0; + int toughness = 0; + String description = ""; + String image = ""; + String creatureType = ""; + + while (rs.next()) { + cardName = rs.getString("CARDNAME"); + manaCost = rs.getInt("MANACOST"); + power = rs.getInt("POWER"); + toughness = rs.getInt("TOUGHNESS"); + description = rs.getString("DESCRIPTION"); + image = rs.getString("IMAGE"); + creatureType = rs.getString("CREATURETYPE"); + + } + rs.close(); + stmt.close(); + c.close(); + Card card = new Card(cardName, manaCost, power, toughness, description, image, creatureType); + return card; + } catch (Exception e) { + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + System.exit(0); + } + System.err.println("Returning null."); + return null; + } + + public boolean insertCard(Card card) { + Statement stmt = null; + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:cards.db"); + c.setAutoCommit(false); + String baseStmt = "INSERT INTO CARDS (CARDNAME,MANACOST,POWER,TOUGHNESS,DESCRIPTION,IMAGE,CREATURETYPE) VALUES "; + stmt = c.createStatement(); + String sql = "(\'" + card.cardName + "\'," + card.manaCost + "," + card.power + "," + card.toughness + ", \'" + + card.description + "\',\'" + card.image + "\',\'" + card.creatureType + "\');"; + stmt.executeUpdate(baseStmt + sql); + stmt.close(); + c.commit(); + c.close(); + } catch (Exception e) { + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + return false; + } + return true; + + } + + public boolean setupCards() { + ArrayList topdeck = new ArrayList(); + topdeck.add(new Card("Devouring Dragon", 5, 5, 6, "He does to people what Greg does to booty.", "", "Dragon")); + topdeck.add(new Card("Dragon Worshipper", 1, 1, 1, "He spends his days praying to dragons.", "", "Human")); + topdeck.add(new Card("Dragon Whelp", 2, 3, 2, "He will get there.", "", "Dragon")); + topdeck.add(new Card("Dragon Egg", 1, 0, 2, "Close to hatching!", "", "Egg")); + topdeck.add(new Card("Lingering Flame", 3, 4, 3, "He lives among the dragons.", "", "Elemental")); + topdeck.add( + new Card("Dragonguard Sentry", 1, 1, 2, "Devoted to the protection of the dragonflight.", "", "Human")); + topdeck.add(new Card("Withering Flamewitch", 2, 4, 2, "Her flame bites deep.", "", "Human")); + topdeck.add(new Card("Final Examination", 6, 9000, 0, "Oh dear, Greg.", "", "Parchment")); + topdeck.add(new Card("Drunken Dragon", 3, 4, 1, "He is trying.", "", "Dragon")); + topdeck.add(new Card("Isle of Power", -1, 0, 0, "Invoke this for mana.", "", "Land")); + for (Card card : topdeck) { + if (!insertCard(card)) { + return false; + } + } + return true; + } + + public boolean setupTable() { + Statement stmt = null; + try { + Class.forName("org.sqlite.JDBC"); + c = DriverManager.getConnection("jdbc:sqlite:cards.db"); + stmt = c.createStatement(); + String sql = "CREATE TABLE CARDS " + "(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + + " CARDNAME TEXT NOT NULL, " + " MANACOST INT NOT NULL, " + + " POWER INT NOT NULL, " + " TOUGHNESS INT NOT NULL, " + + " DESCRIPTION TEXT NOT NULL, " + " IMAGE TEXT NOT NULL, " + + " CREATURETYPE TEXT NOT NULL)"; + stmt.executeUpdate(sql); + stmt.close(); + c.close(); + if (!setupCards()) { + System.exit(-1); + } + } catch (Exception e) { + if (!e.getMessage().contains("(table CARDS already exists)")) + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + return false; + } + return true; + } +}