Monday, October 27, 2008

The Mighty Rules Engine

One object to rule them all and in the darkness bind them. OK, enough of my Lord of the Rings paraphrase. In MTG Forge version 2 I have an object named “RulesEngine” that holds all rules of a Magic game. All game actions go through the Rules Engine like drawing a card, destroying a creature or advancing to the next phase.

Let me briefly explain what an object is to non-programmers. An object is a group of code that works on the same data. In Magic terms, a Card object would handle all card operations like changing the toughness, adding keywords and getting the card name. The term “class” is also thrown around and for our purposes “class” means the same as object. In Java object names are typically capitalized so that is why I wrote Card object, instead of card object.

Anyways, back to the Rules Engine. Unfortunately the Rules Engine object will be a very complicated and long, since it has to do everything. Currently MTG Forge tries to group many of the rules into GameAction but I did this mostly to save code. All of the rules should be put in one object. In case some rule needs to be fixed or modified, I know where to go, the Rule Engine object.

The cool thing about the rule engine is that it can be easily modified. Do you want to play type 4 with unlimited mana, done. Any rule can be modified. Complicated cards like Mana Flare (each land produces one extra mana) should be fairly simple to implement using the rules engine. I’m also hoping to be able to program one of Magic’s most powerful cards, Time Walk. (1U, Sorcery, take an extra turn after this one.)

p.s.
I must complete my Lord of the Rings paraphrase. I find this terribly funny. And in case you didn’t get it, the one object to rule them all is the Rules Engine.

One object to rule them all, One object to find them,
One object to bring them all and in the darkness bind them

9 comments:

  1. Yay for Timewalk!!! I can't wait for V2.

    ReplyDelete
  2. I'm not a very experienced programmer, so I may be be completely wrong here, but by using one large block of code to handle every action, wouldn't that mean that every action takes a little longer to execute? I'm sure it wouldn't make a noticeable difference, but wouldn't separate chunks of code for different types of actions make the program run a little faster? Or am I wrong?

    I'm not trying to criticize your programming at all; I'm just trying to satisfy my curiosity.

    ReplyDelete
  3. Objects are not chunks of logic code in that sense Joku...they are by and large data though you can give them members that have procedures (functions). So no it should not take any more time than accessing anything global.

    Also you can set up 'hash' tables to deal with very large chunks of data to ensure they don't take forever to access if that is worrisome.

    The real difficulty with One large object is that it will be hard to debug/read for others.

    ReplyDelete
  4. Having one large object is usually a bad idea, but since everything in Magic can be modified, having one main object that handles all the rules should make some things easier like Time Walk.

    ReplyDelete
  5. Forge, I couldn't agree more: Usually in programming, we try to keep objects small, and have them do a simple task. But in a MTG simulation, at some point, you have to have some centralized part that controls the actions of all the cards...
    In my code, this one is called "GameObserver", not RuleEngine, but it has overall the same functionality as your class.
    However I'm trying to dispatch the rules in distinct classes now, and they "register" in the GameObserver. Hopefully this'll give me more flexibility in future releases

    ReplyDelete
  6. Your postings are getting more lame by the day...

    ReplyDelete
  7. Nothing for nothing mr anonymous...but for a post lambasting lame posts...that one wracks up a lot of irony points...Next time log in with a user name so we can id you and let us have some CONSTRUCTIVE criticism.

    Or not ...you can always just sit on the sidelines and hit us with lame lamentations about the lameness of lamentable posts. :)

    ReplyDelete
  8. Hey Joku,

    by using one large block of code to handle every action, wouldn't that mean that every action takes a little longer to execute?

    To answer your question, I don't know, lol. Generally you don't worry about performance unless you have to, like with a 3D game. "Premature optimization is the root of all evil" --Donald Knuth

    Generally you write your application then you see if it needs to be optimized and then you optimize the part that needs it. Don't worry if you use an extra IF or something, the code needs to be readable by your or other people.

    I'm sure people laugh at my code and comments.

    ReplyDelete
  9. men, men when are you gonna put this program in the network? I want to play with this, in some occassion I can't go to play in the local store, so I have to stay in home, please if you can put a date, I will wait for then.

    ReplyDelete

Note: Only a member of this blog may post a comment.