Forge 2.0 - Input and CardID
Post MTG Forge Related Programming Questions Here
Moderators: timmermac, Blacksmith, KrazyTheFox, Agetian, friarsol, CCGHQ Admins
1 post
• Page 1 of 1
Forge 2.0 - Input and CardID
by mtgrares » 22 Jun 2010, 18:27
I have worked some on Forge 2. Trying to get the foundational classes right is really hard. I'm not sure these two classes will make sense to anyone else but feel free to take a peek.
CardID is similar to Card but only "points to" the card information. The Rules object is going to be huge since it implements all of the rules and to get the specific card information like mana cost and abilities, you have to pass CardID to Rules.
On a side note, having a huge object is generally bad but since Magic cards can do anything, I'm putting all of the complexity (and wierdness) into one class. Internally I can change the Rules class as much as I want. Like with creature power/toughness, at first Rules can implement power/toughness as an int without a layer system and later Rules can implement a complicated layer system. In order to do this though Rules will have to be given ALL information, no matter how trivial, at first it will throw away most of the info but later it can be used.
CardID.java
Input.java
I've talked so much about Forge 2 that it is a little frustrating and now I'm trying to do things right (with abstract classes), it is just taking much longer.
CardID is similar to Card but only "points to" the card information. The Rules object is going to be huge since it implements all of the rules and to get the specific card information like mana cost and abilities, you have to pass CardID to Rules.
On a side note, having a huge object is generally bad but since Magic cards can do anything, I'm putting all of the complexity (and wierdness) into one class. Internally I can change the Rules class as much as I want. Like with creature power/toughness, at first Rules can implement power/toughness as an int without a layer system and later Rules can implement a complicated layer system. In order to do this though Rules will have to be given ALL information, no matter how trivial, at first it will throw away most of the info but later it can be used.
CardID.java
- Code: Select all
/**
* Think of CardID as a pointer to a Magic card, CardID "points to"
* a card. CardID is used as a placeholder for a Magic card and
* can be moved from zone to zone.
* CardID holds a card's name and an int that represents the card's
* internal unique id. CardID doesn't hold information about a card
* like mana cost or abilities. In order to get information about
* the card you have to pass CardID to {@link Rules}.<br><br>
*
* Think of this class as being unchangable and immutable like String.
* 99% of the time this class is immutable. CardID.setID(int) should only
* be used for unusual circumstances like copying a card and for testing
* purposes. <br><br>
*
* The reason that I am using CardID instead of just Card is because
* the old Card class became very, very complicated with hundreds
* of methods. And because there are some crazy Magic cards that
* turn all cards in play, in your hand, and in the graveyard white
* (or some color) the old Card class just couldn't do stuff like that.
* Welcome to CardID, the Rules object holds all of the information
* about a Magic game including all of the card info. Rules can be
* internally complicated but externally simple. Rules also can
* easily change a card's info and turn it white or do other crazy
* things that Magic cards require.<br><br>
*
* This class is final which might help out performance.
*/
final public class CardID
{
private static int nextID;
private int myID;
private String cardName;
public CardID(String cardName)
{
this();
this.cardName = cardName;
}
public CardID()
{
myID = nextID;
nextID++;
if(nextID == Integer.MAX_VALUE)
{
ReportError.error
(
"CardID : constructor() error - "
+"variable nextID equals the biggest size an integer can be --"
+"The variable nextID is being reset and the code "
+"may or may not work"
);
//this is a hack but it might work
//reset nextID because more than likely
//there are no CardID objects with the myID of 0
nextID = 0;
}
}//constructor()
public void setCardName(String cardName) {this.cardName = cardName;}
public String getCardName() {return cardName;}
/**
* Changes the card's id. This method should normally NOT be used.
* This method is here for unusual circumstances like copying a card
* and for test purposes.
*/
public void setID(int n)
{
myID = n;
}
public int getID() {return myID;}
private boolean sameName(CardID a)
{
return getCardName().equals(a.getCardName());
}
private boolean sameID(CardID a)
{
return getID() == a.getID();
}
public boolean equals(Object o)
{
//can only be equal if compared to another CardID object
if(getClass() == o.getClass())
{
CardID card = (CardID)o;
if(sameID(card))
{
if(sameName(card))
return true;
else
ReportError.error("CardID : equals() error, 2 CardID objects have the same id but not the same card name: " +getID() +" - " +getCardName() +" , " +card.getID() +" - " +card.getCardName());
}
}//if (same class)
return false;
}//equals()
}
Input.java
- Code: Select all
import java.util.*;
/**
* Process all mouse events and displays a message and the buttons
* that a user can click. This class is used for phases and choosing
* targets for a card.<br><br>
*
* Please note that this class works with {@link TargetChoice}.
* Input controls what the user can click on and TargetChoice
* holds target information for individual cards. <br><br>
*
* Right now the user cannot click on an opponent's mana pool or
* your or your opponent's library. The methods do not exist
* and may be added later. Note, if you add these methods
* you also need to update {@link TargetChoice}.
*/
public interface Input
{
/**
* This is always the first method that is called and it
* acts like a constructor. Code that you would normally
* put into the constructor would go into this method.
*/
public abstract void init();
/**
* A message that is shown to the user like "Main 1" or
* "Choose target creature".
*/
public abstract String getMessage();
/**
* Buttons that are displayed underneath the
* textbox that is showing getMessage().
* Usually the buttons will be "OK" and "Cancel".
* The Strings are converted into buttons by the gui.
*/
public abstract String[] getButtons();
/**
* This is called when the user clicks on a player, either himself
* or the computer.
* @param player is one of the Strings in {@link Constant.Player}
*/
public abstract void clickPlayer(String player);
/**
* This is called when a player clicks on his mana pool
* @param color is one of the Strings in {@link Constant.Color}
*/
public abstract void clickManaPool(String color);
/**
* This is called when a player clicks on a card.
*/
public abstract void clickCard(Card card, Zone zone);
/**
* When an user clicks on a non-dialog button, this method is called.
* @param buttonText is one of the Strings returned by {@link #getButtons()}
*/
public abstract void clickButton(String buttonText);
/**
* If true, this Input should be shown as a dialog box. The
* dialog box should call
* {@link #getMessage()} and
* {@link #getButtons()} in order to show the player a message
* and buttons.
*/
public abstract boolean isDialog();
/**
* Lets the user choose one or more of the options in ArrayList.
* ArrayList may hold Strings or Card objects. If ArrayList
* hold Card objects, the user needs to be able to see all of
* the card information on the right hand part of the user interface
* in the "card detail" panel.<br><br>
*
* Future Improvements: ArrayList may hold other ArrayLists
* for cards like "Do or Die" where the opponent divides
* up the player's creatures into two piles and the player
* decides which pile to keep and which pile that gets destroyed.
*/
public abstract ArrayList dialog_getChoices();
/**
* When an user clicks on a dialog button, this method is called.
* Future Improvement: I'm not really sure how the user should select
* multiple cards like for Gifts Ungiven.
* @param buttonText is one of the Strings returned by {@link #getButtons()}
* @param userChoice is one of the objects returned by {@link #dialog_getChoices()}
*/
public abstract void dialog_clickButton(String buttonText, Object userChoice);
}
I've talked so much about Forge 2 that it is a little frustrating and now I'm trying to do things right (with abstract classes), it is just taking much longer.
- mtgrares
- DEVELOPER
- Posts: 1352
- Joined: 08 Sep 2008, 22:10
- Has thanked: 3 times
- Been thanked: 12 times
1 post
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 38 guests