Monday, March 31, 2008

Programming and card games

So I am going to go over something that makes sense to me, and the reason behind it. In my programming class, we learned about recursion and stacks and queues. Now, everybody has experienced and can understand queues. Waiting in a line is an example of queues. Queues work in that the first person in is the first person out (FIFO). Now, this seems to be the way a lot of things work. However, stacks work differently and they are LIFO, or FILO. Something that goes in last comes out first or something that goes in first comes out last. Now, this seems kind of bizarre, but makes sense also. In recursion, which is also LIFO, you have a function that calls itself. Let's say that it calls itself and adds 1 every time you call it, and when it reaches five, it stops calling itself.

void addone(int &x){
x++;
if (x<5){
addone(x);
}
}

int main(){
int x=0;
addone(x);
cout << x << endl;
return 0;
}

This is a C++ program that has recursion in the function addone. Basically, it does what I said above and at the end prints out the final value of x. This is how it works:
x=0.
call adddone(1):
call addone(2):
call addone(3):
call addone(4):
call addone(5):
addone(5) finishes
addone(4) finishes
addone(3) finishes
addone(2) finishes
addone(1) finishes
x is printed out as 5.

In TCGs I play, the chain (or stack) works exactly the same way. Now, a difference is that when it finishes in a TCG, the effect (in this case, x+1) happens as opposed to this where the effect happens, then calls. Still, the basic concept remains the same. If I play Savage Beatdown on my 4/4 attacker, then my opponent tries to play System Failure in response to my Savage, that is stupid because of how this all works:
Savage Beatdown called (attacker at 4/4)
System Failure called (attacker at 4/4)
System Failure resolves (attacker still at 4/4 and fizzles)
Savage Beatdown resolves (attacker at 9/4, and smacking down)

I forgot exactly where I was going, but I know that I was able to understand programming better because of how TCGs work.

No comments:

slots
web counter