Convert project from makefile to maven
This commit is contained in:
31
src/main/java/MTGClone/App.java
Normal file
31
src/main/java/MTGClone/App.java
Normal file
@ -0,0 +1,31 @@
|
||||
package MTGClone;
|
||||
import java.util.Scanner;
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.print("Enter Player One's name: ");
|
||||
String player1Name = in.nextLine();
|
||||
System.out.print("Enter Player Two's name: ");
|
||||
String player2Name = in.nextLine();
|
||||
|
||||
Deck deck=new Deck();
|
||||
Deck deck2=new Deck();
|
||||
Player player1= new Player(deck, player1Name);
|
||||
Player player2= new Player(deck2, player2Name);
|
||||
Game game= new Game(player1, player2);
|
||||
|
||||
|
||||
|
||||
while(player1.lifeTotal>=0&&player2.lifeTotal>=0){
|
||||
if (game.player1.lifeTotal>0)
|
||||
game.Turn(game.player1,game.player2,in);
|
||||
System.out.println("------------------------------------------------------------------------------------------------------------------------------");
|
||||
if (game.player2.lifeTotal>0)
|
||||
game.Turn(game.player2,game.player1,in);
|
||||
System.out.println("------------------------------------------------------------------------------------------------------------------------------");
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
89
src/main/java/MTGClone/Card.java
Normal file
89
src/main/java/MTGClone/Card.java
Normal file
@ -0,0 +1,89 @@
|
||||
package MTGClone;
|
||||
|
||||
public class Card{
|
||||
protected String cardName;
|
||||
protected int manaCost;
|
||||
protected int power;
|
||||
protected int toughness;
|
||||
protected String description;
|
||||
protected String image;
|
||||
protected String creatureType;
|
||||
|
||||
|
||||
|
||||
public String getCardName() {
|
||||
return cardName;
|
||||
}
|
||||
|
||||
public void setCardName(String cardName) {
|
||||
this.cardName = cardName;
|
||||
}
|
||||
|
||||
public int getManaCost() {
|
||||
return manaCost;
|
||||
}
|
||||
|
||||
public void setManaCost(int manaCost) {
|
||||
this.manaCost = manaCost;
|
||||
}
|
||||
|
||||
public int getPower() {
|
||||
return power;
|
||||
}
|
||||
|
||||
public void setPower(int power) {
|
||||
this.power = power;
|
||||
}
|
||||
|
||||
public int getToughness() {
|
||||
return toughness;
|
||||
}
|
||||
|
||||
public void setToughness(int toughness) {
|
||||
this.toughness = toughness;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public String getCreatureType() {
|
||||
return creatureType;
|
||||
}
|
||||
|
||||
public void setCreatureType(String creatureType) {
|
||||
this.creatureType = creatureType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
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;
|
||||
this.manaCost = manaCost;
|
||||
this.power = power;
|
||||
this.toughness = toughness;
|
||||
this.description = description;
|
||||
this.image = image;
|
||||
this.creatureType = creatureType;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
96
src/main/java/MTGClone/Deck.java
Normal file
96
src/main/java/MTGClone/Deck.java
Normal file
@ -0,0 +1,96 @@
|
||||
package MTGClone;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
public class Deck {
|
||||
protected ArrayList<Card> topdeck;
|
||||
protected ArrayList<Card> discard;
|
||||
protected ArrayList<Card> hand;
|
||||
|
||||
public Card drawCard(){
|
||||
if (topdeck.size()<=0)
|
||||
return null;
|
||||
Card card=topdeck.get(0);
|
||||
topdeck.remove(0);
|
||||
hand.add(card);
|
||||
return card;
|
||||
|
||||
}
|
||||
public Deck() {
|
||||
SQLDriver d = new SQLDriver();
|
||||
d.setupTable();
|
||||
topdeck = new ArrayList<Card>();
|
||||
hand = new ArrayList<Card>();
|
||||
discard = new ArrayList<Card>();
|
||||
for (int i = 0; i < 60; i++){
|
||||
topdeck.add(d.getRandomCard());
|
||||
}
|
||||
Shuffle();
|
||||
for (int i = 0; i < 7; i++){
|
||||
drawCard();
|
||||
}
|
||||
}
|
||||
|
||||
private void Shuffle() {
|
||||
Collections.shuffle(topdeck);
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<Card> getTopdeck() {
|
||||
return topdeck;
|
||||
}
|
||||
|
||||
public void setTopdeck(ArrayList<Card> topdeck) {
|
||||
this.topdeck = topdeck;
|
||||
}
|
||||
|
||||
public ArrayList<Card> getDiscard() {
|
||||
return discard;
|
||||
}
|
||||
|
||||
public void setDiscard(ArrayList<Card> discard) {
|
||||
this.discard = discard;
|
||||
}
|
||||
|
||||
public ArrayList<Card> getHand() {
|
||||
return hand;
|
||||
}
|
||||
|
||||
public void setHand(ArrayList<Card> hand) {
|
||||
this.hand = hand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Deck [discard=" + discard + ", hand=" + hand + ", topdeck=" + topdeck + "]";
|
||||
}
|
||||
|
||||
public void showHand() {
|
||||
int maxName = 0;
|
||||
int maxType = 0;
|
||||
for (Card c : hand) {
|
||||
if (c.cardName.length() > maxName) {
|
||||
maxName = c.cardName.length();
|
||||
}
|
||||
if (c.creatureType.length() > maxType) {
|
||||
maxType = c.creatureType.length();
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < hand.size(); i++) {
|
||||
Card c=hand.get(i);
|
||||
System.out.print((i+1) + " " + c.cardName);
|
||||
for (int ii = c.cardName.length(); ii < maxName; ii++) {
|
||||
System.out.print(" ");
|
||||
}
|
||||
System.out.print(" (" + c.creatureType + ")");
|
||||
for (int ii = c.creatureType.length(); ii < maxType; ii++) {
|
||||
System.out.print(" ");
|
||||
}
|
||||
System.out.print("\t" +(c.manaCost>=0? c.manaCost :0) + "M\t" + c.power + "P\t" + c.toughness + "T\n");
|
||||
}
|
||||
System.out.println("");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
31
src/main/java/MTGClone/Driver.java
Normal file
31
src/main/java/MTGClone/Driver.java
Normal file
@ -0,0 +1,31 @@
|
||||
package MTGClone;
|
||||
import java.util.Scanner;
|
||||
public class Driver {
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.print("Enter Player One's name: ");
|
||||
String player1Name = in.nextLine();
|
||||
System.out.print("Enter Player Two's name: ");
|
||||
String player2Name = in.nextLine();
|
||||
|
||||
Deck deck=new Deck();
|
||||
Deck deck2=new Deck();
|
||||
Player player1= new Player(deck, player1Name);
|
||||
Player player2= new Player(deck2, player2Name);
|
||||
Game game= new Game(player1, player2);
|
||||
|
||||
|
||||
|
||||
while(player1.lifeTotal>=0&&player2.lifeTotal>=0){
|
||||
if (game.player1.lifeTotal>0)
|
||||
game.Turn(game.player1,game.player2,in);
|
||||
System.out.println("------------------------------------------------------------------------------------------------------------------------------");
|
||||
if (game.player2.lifeTotal>0)
|
||||
game.Turn(game.player2,game.player1,in);
|
||||
System.out.println("------------------------------------------------------------------------------------------------------------------------------");
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
64
src/main/java/MTGClone/Game.java
Normal file
64
src/main/java/MTGClone/Game.java
Normal file
@ -0,0 +1,64 @@
|
||||
package MTGClone;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Game {
|
||||
protected Player player1;
|
||||
protected Player player2;
|
||||
|
||||
protected ArrayList<Card> player1Field;
|
||||
protected ArrayList<Card> player2Field;
|
||||
|
||||
|
||||
|
||||
public Player getPlayer1() {
|
||||
return player1;
|
||||
}
|
||||
|
||||
public void setPlayer1(Player player1) {
|
||||
this.player1 = player1;
|
||||
}
|
||||
|
||||
public Player getPlayer2() {
|
||||
return player2;
|
||||
}
|
||||
|
||||
public void setPlayer2(Player player2) {
|
||||
this.player2 = player2;
|
||||
}
|
||||
|
||||
public ArrayList<Card> getPlayer1Field() {
|
||||
return player1Field;
|
||||
}
|
||||
|
||||
public void setPlayer1Field(ArrayList<Card> player1Field) {
|
||||
this.player1Field = player1Field;
|
||||
}
|
||||
|
||||
public ArrayList<Card> getPlayer2Field() {
|
||||
return player2Field;
|
||||
}
|
||||
|
||||
public void setPlayer2Field(ArrayList<Card> player2Field) {
|
||||
this.player2Field = player2Field;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Game [player1=" + player1 + ", player1Field=" + player1Field + ", player2=" + player2
|
||||
+ ", player2Field=" + player2Field + "]";
|
||||
}
|
||||
|
||||
public Game(Player player1, Player player2) {
|
||||
this.player1 = player1;
|
||||
this.player2 = player2;
|
||||
this.player1Field=new ArrayList<Card>();
|
||||
this.player2Field=new ArrayList<Card>();
|
||||
}
|
||||
|
||||
public void Turn(Player player,Player target, Scanner in) {
|
||||
player.turn(target, in);
|
||||
}
|
||||
|
||||
}
|
||||
129
src/main/java/MTGClone/Player.java
Normal file
129
src/main/java/MTGClone/Player.java
Normal file
@ -0,0 +1,129 @@
|
||||
package MTGClone;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Player {
|
||||
protected Deck deck;
|
||||
protected String name;
|
||||
protected int lifeTotal;
|
||||
protected int manaTotal;
|
||||
protected int armorTotal;
|
||||
|
||||
public Deck getDeck() {
|
||||
return deck;
|
||||
}
|
||||
|
||||
public void setDeck(Deck deck) {
|
||||
this.deck = deck;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Player(Deck deck, String name) {
|
||||
this.deck = deck;
|
||||
this.name = name;
|
||||
this.lifeTotal = 20;
|
||||
this.manaTotal = 0;
|
||||
this.armorTotal = 0;
|
||||
}
|
||||
|
||||
public boolean playCard(Card card, int currentMana) {
|
||||
if (currentMana < card.getManaCost())
|
||||
return false;
|
||||
currentMana -= card.getManaCost();
|
||||
if (currentMana > manaTotal)
|
||||
manaTotal = currentMana;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public void turn(Player target, Scanner in) {
|
||||
int currentMana = manaTotal;
|
||||
int cardUse = 0;
|
||||
System.out.println("\n\n\tIt is " + name + "'s turn.");
|
||||
deck.drawCard();
|
||||
System.out.println("Your health and armor is " + lifeTotal + " and " + armorTotal
|
||||
+ ". Your opponent's health and armor is " + target.lifeTotal + " and " + target.armorTotal + ".\n");
|
||||
|
||||
do {
|
||||
deck.showHand();
|
||||
System.out.println("You have " + currentMana + " mana.\n");
|
||||
System.out.print("Play your cards. 0 to skip: ");
|
||||
cardUse = Integer.parseInt(in.nextLine());
|
||||
|
||||
if (cardUse > 0 && cardUse <= deck.getHand().size()) {
|
||||
Card card = deck.getHand().get(cardUse - 1);
|
||||
if (playCard(card, currentMana)) {
|
||||
deck.getHand().remove(cardUse - 1);
|
||||
currentMana -= card.manaCost;
|
||||
armorTotal += card.toughness;
|
||||
int damage = card.power;
|
||||
int armor = target.armorTotal;
|
||||
if (target.armorTotal > 0) {
|
||||
target.setArmorTotal(target.getArmorTotal() - damage);
|
||||
damage -= armor;
|
||||
if (target.armorTotal < 0)
|
||||
target.setArmorTotal(0);
|
||||
}
|
||||
if (damage > 0)
|
||||
target.setLifeTotal(target.getLifeTotal() - damage);
|
||||
|
||||
if (card.power > 0) {
|
||||
System.out.println("Your opponent's health and armor is " + target.lifeTotal + " and " + target.armorTotal + ".");
|
||||
}
|
||||
} else {
|
||||
System.out.println("You do not have enough mana to play that card.");
|
||||
}
|
||||
} else if (cardUse!=0) {
|
||||
System.out.println("Invalid selection.");
|
||||
}
|
||||
} while (cardUse != 0);
|
||||
|
||||
while (deck.getHand().size() > 7) {
|
||||
deck.showHand();
|
||||
System.out.print("Discard a card: ");
|
||||
cardUse = Integer.parseInt(in.nextLine());
|
||||
|
||||
if (cardUse > 0 && cardUse <= deck.getHand().size())
|
||||
deck.getHand().remove(cardUse - 1);
|
||||
else
|
||||
System.out.println("You've selected an invalid card.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Player [armorTotal=" + armorTotal + ", deck=" + deck + ", lifeTotal=" + lifeTotal + ", manaTotal="
|
||||
+ manaTotal + ", name=" + name + "]";
|
||||
}
|
||||
|
||||
public int getLifeTotal() {
|
||||
return lifeTotal;
|
||||
}
|
||||
|
||||
public void setLifeTotal(int lifeTotal) {
|
||||
this.lifeTotal = lifeTotal;
|
||||
}
|
||||
|
||||
public int getManaTotal() {
|
||||
return manaTotal;
|
||||
}
|
||||
|
||||
public void setManaTotal(int manaTotal) {
|
||||
this.manaTotal = manaTotal;
|
||||
}
|
||||
|
||||
public int getArmorTotal() {
|
||||
return armorTotal;
|
||||
}
|
||||
|
||||
public void setArmorTotal(int armorTotal) {
|
||||
this.armorTotal = armorTotal;
|
||||
}
|
||||
|
||||
}
|
||||
125
src/main/java/MTGClone/SQLDriver.java
Normal file
125
src/main/java/MTGClone/SQLDriver.java
Normal file
@ -0,0 +1,125 @@
|
||||
package MTGClone;
|
||||
|
||||
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<Card> topdeck = new ArrayList<Card>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user