Monday, August 1, 2011

How to Code a Trading Card Videogame

This article is all about the wide world of programming so if your eyes start to glaze over…sorry…come back next week when I cover something more exciting.

from Magic's multiplayer Commander set

Maybe there is a trading card game (TCG) that you want to play.  Maybe you love this TCG with all of your heart but no one else has ever heard of it and you bought a couple of starter decks and you just fell in love with it.  Here are 7 steps that will get you started in the right direction.

1. Learn one programming language well.  It doesn’t matter if you know C, C++, C#, D, Java, Perl, Python, Ruby or Lua.  I would suggest studying for a certification test in your language which will force you to learn all of the tiny details that you otherwise might gloss over and those details become VERY important when you start to program anything over 1,000 lines.  If you want a great book on good coding practices, I would suggest “Code Complete” by Steve McConnell.  This one book taught me more about coding than my four years in college.  (OK, it was really 6 years because I spent 2 years majoring in music which was really “fun” practicing 2 hours a day.)

--If you want to get started right away, you can start programming in a language that you don’t know but it can be frustrating since you have to look everything up in a book or online.

2. You have to become a rules lawyer.  If you don’t know the rules, you obviously can’t program them.  Learn to read the “comprehensive rules” that come with many TCG’s.  Reading the comprehensive rules will open your eyes and will answer many of the details when programming like when your search your deck for a card can the player choose to not find a card even though it is in the deck.  The usual answer to this is yes because otherwise you have to prove to your opponent that the card isn’t in your deck, which is annoying and time consuming.  Many TCG’s share the same nomenclature such as zones.

--A quickstart idea is just to code two starter decks and let other people verify that your program implements the rules correctly.

3. Write the program for only yourself.  By tailoring the program specifically to yourself, you are making many good design decisions.  Not everyone will agree with you 100% but that never happen anyways.

4.  Don’t expect anyone else to help or support you.

I know this sounds a little rough but if you are coding something that you think will make other people happy, you are in for a world of hurt.  By programming something specifically for yourself, you are (hopefully) guaranteed to make yourself happy.  And if you are happy, it will make the work that much more enjoyable.  Internal motivation is much better external motivation, aka writing the program for somebody else.

Coding is really a one-man activity.  And yes you may be working on a team but you will spend most of your time by yourself.  Many open source projects are supported by only one-man so you have to learn to rely on yourself.  (Yes, you can ask other people questions but don’t expect them to do the grunt work, you have to do your own grunt work.)

5.  Spent a lot of time working on the user interface.  People will make snap judgments about your program after using your program for only 30 seconds.  The user interface is what people will be using and it will affect their enjoyment.  With that said, I know that working on the user interface can be hard/annoying.  Making your program look halfway decent is a big challenge.  The reason that Forge has a fantastic, boring gray background is because that is the best that I can do.  Programming the user interface can easily take half of your time.

6. Don’t be afraid to fail.  I didn’t get Forge right the first time.  The current version of Forge is probably by 3rd or 4th version.  I would start coding the best that I could and eventually the project would just crumble right before my eyes.  I would pick up the pieces and confidently try again.  And now if I started Forge again, I think I could program it 80% better because I’ve learned so much.  I’ve read that you learn more from your failures than you do from your successes.

7. Have fun.  If you aren’t having fun programming something…then stop.  Find something that is easier and work on that instead.  By working on easier projects you are flexing your coding muscles so that you can handle that harder stuff later.

Hopefully that helps point somebody in the right direction.  Obviously I left out many subjects like writing the AI, which is a whole book by itself, or stressing that automated tests are your friend (although I admit that I didn't write any).   Coding can be fun, especially if you are working on a videogame.



Anonymous said...

Do step by step tutorials on how to code tcg...pretty please with a cherry on top :D

Anonymous said...

Step 1: Be born a genius.
Step 2: Learn to become an amazing programmer.
Step 3: Spend hundreds (thousands?)of hours coding.

Simple, really.

Forge said...

"Do step by step tutorials on how to code tcg"

I probably could write a book about it. I'm not sure a step by step tutorial would be very useful because every "step" is really hundreds of small steps.

But I know even 20 pages in Word would probably be a better introduction versus a short blog post.

Forge said...

"Spend hundreds (thousands?)of hours coding."

Hundreds sounds right but mostly you have to "enjoy the journey". If you don't like to program, then coding is going to seem like the hardest thing in the world. Personally I like to code so it wasn't a problem. Many of my classmates in college hated coding.

Try to "follow your passion" and hopefully it will lead to a job.

polarisu said...

Great tips! I guess it would also be safe to reiterate that you have to love (I mean really love) what you're doing (coding and the game) because it is a labor of love. Nobody's gonna pay you for it.

I've always wanted to make my own video games ever since I was a kid and as I grew up, I made my keep as a programmer/developer but ironically enough, more for business related software.

I've slightly accomplished a mini MTG game with 2 very basic starter decks back in 2007 in Visual Foxpro 6 (Yes, I know, it's old haha but since it was what the office was using at that time). But even that was easy since I was only working part time and a student on certain days.

I'm looking into L5R (Legend of the 5 rings) in Java --- and like you've said, IF I have the time. That's always the problem with a project you aren't getting paid for that's why I do have much respect for you guys, hats down. You're an inspiration!

I think there's really no way to teach "how to code" this and that. You really have to learn the boring stuff such as concepts first and have a strong foundation which prepare you for more advanced stuff later on. You could get that in a full or short course or all by yourself --- it would just take longer.

For anybody who already has basic grasp for coding in another language, you can grab Core Java 1 and 2 by Cay Horstmann if you want to try Java.

(I've posted something earlier this morning but it seemed to have failed from publishing XD)

masayume said...

I think you underestimated the importance of the interface in point 5.

crapsticko said...

Hi Forge creator. I wish you could add some MtG cards that have complex tech such as Treasure Hunt and such. Cheers :)

P/S: I adore your work!

Forge said...

Another good source of information is the two posts that I have written previously.

Forge's Architecture or "How to program your own Magic engine"
Part 1
Part 2