Computer Science Canada Need help with calling record in a class |
Author: | DemonZ [ Wed Jan 24, 2007 8:33 pm ] | ||
Post subject: | Need help with calling record in a class | ||
Take a look at this code:
This is a class that I made for the yugioh card game and im trying to display the information, but I wanna know how you output the record when its in the class, and i looked at the tutorial for classes part 1 to 3 already but he didnt mention anything about it, can someone please help me? |
Author: | CodeMonkey2000 [ Wed Jan 24, 2007 9:15 pm ] | ||
Post subject: | Re: Need help with calling record in a class | ||
well here is how I would out put the records:
Dont know if this is the best way. |
Author: | CodeMonkey2000 [ Wed Jan 24, 2007 9:19 pm ] |
Post subject: | RE:Need help with calling record in a class |
By the way, i maybe new to classes myself, but i think having Blue_Eyes_White_Dragon as a class is wrong. call the class Card, and make a funtion/procedure that can change the values that way the same class can be used for different cards. |
Author: | Cervantes [ Thu Jan 25, 2007 12:09 am ] |
Post subject: | RE:Need help with calling record in a class |
I wouldn't recommend doing what spearmonkey2000 suggested. The reason is that typically we avoid letting objects output things. It's part of the design philosophy of separating the output from the engine. There are a few different things you could do here. If you just want to return the data, you should export your MONSTER variable (which, by the way, breaks Turing naming convention: all upper case is for constants) then grab the data out of that. I covered exporting variables in the tutorial; monster is just a variable, albeit of a user defined data type. Shouldn't be an issue, though. Now, here's the real issue. You've defined a Blue_Eyes_White_Dragon objects to have a Stats property, and nothing else. Basically, your object is a wrapper for the Stats type. Unless you were planning on adding extra functionality later, there's no reason to define a class to wrap a single variable with no methods associated with it. |
Author: | DemonZ [ Thu Jan 25, 2007 10:09 am ] |
Post subject: | Re: Need help with calling record in a class |
So then if using a class would be a waste here, then what do you suggest I use to store this information in? Should I just use record or should I put it in a function/procedure? Because the main purpose of this is to store all the information in the class in something so that I can easily access it in the future. Oh and by the way spearmonkey2000 the class is the name of the card because I didnt want to store all the cards in the class type, it would take way to long to fetch all of it, but then again, I need a better method of storing this info, can you please tell me another option? |
Author: | Clayton [ Thu Jan 25, 2007 12:16 pm ] |
Post subject: | Re: Need help with calling record in a class |
plan out the class hierarchy. in yughioh, everything is a card. start with a card class. no matter what, a card has a name, a type, and a description/rules text. Your basic Card class should mirror this. From that, there are Monster cards, Effect Monster Cards, Magic cards, and Trap cards. each of these classes would be subclasses of Card. In each of the child classes, you would add/make any changes required for the card to function as it should. |
Author: | Cervantes [ Thu Jan 25, 2007 1:28 pm ] |
Post subject: | RE:Need help with calling record in a class |
You sure know a lot about yughioh, Freakman. But yeah. He's right. OOP is probably a good idea here. You just haven't used it to its potential. Yet. |
Author: | Clayton [ Thu Jan 25, 2007 1:35 pm ] |
Post subject: | Re: Need help with calling record in a class |
uh.... no comment Anyways, something I forgot to mention before, instead of creating classes for each and every card, have some sort of initialization method within that you can use to set all of the classes variables. Ideally, you shouldn't have to worry about the name of cards until you go to sort them in some sort of list. But that's for another day. This way, we can just create a new instance of your monster card, it doesn't care what monster it is, just that it's a monster. I would recommend going over Cervantes' Classes tutorials again. This time though, write notes down as you read through them. Then, whenever you come upon a problem, look over your notes, and you may find that you wrote down the answer a while ago |
Author: | CodeMonkey2000 [ Thu Jan 25, 2007 2:00 pm ] | ||
Post subject: | Re: Need help with calling record in a class | ||
I found another way of changing the value of things in the MONSTER record.
Is this a good way to manipulate variables in classes? Or should we be using a procedure to change values? I read the tutorial, and I don't remember seeing anything on this. |
Author: | DemonZ [ Thu Jan 25, 2007 2:50 pm ] |
Post subject: | Re: Need help with calling record in a class |
Ok so I read over the tutorial on classes and I understand it more, and have come up with this hierarchy, Basically im going to have a class called card, that will have a name, and description, then I will have child-classes called monster, Magic and Trap, And they will have their own set of variables (for monster There will be Attack, defense, etc). Now Im not sure whether I want to put everysingle card in its own class, but i think i might have to, and it will be a unit and will be saved with .tu so that I can call upon it in my main program. Now the question that I have is: How would I want to make 1 monster card interact with another? as say one has an attack of 3000 and another has an attack of 1400, can I still be able to calculate damage between the two classes? say for example 3000 - 1400 = 1600 and thats how much the player looses. And 1 last problem that comes into mind, how will I be able to create all the effects and rules that each individual card has? Most cards have different rules and effects and they each effect the game differently, How am I going to be able to program all these methods or rules without wasting 1000 lines? If theres a quicker way, that would be helpful. |
Author: | Cervantes [ Thu Jan 25, 2007 3:13 pm ] |
Post subject: | Re: Need help with calling record in a class |
spearmonkey2000: A fundamental idea behind OOP is that of encapsulation and data hiding. We encapsulate our data into an object and typically don't expose that data to the outside world. Sometimes we might have to. More often, however, we make methods that dictate how the world interacts with the object. So it's up to you whether you export that variable or not. Do you really want to allow something in the outside world full access to that card's stats? Do you want to allow some other card to be able to change the stats of that card? That would be a pretty powerful ability, no doubt. DemonZ: That sort of material is covered beginning in Part II of the tutorial: object interaction. Yes, you can do this. One possibility is to make a method called 'attack' that takes as a parameter called 'target' that is another Monster object. Creating the special rules for each card is more difficult. Here's what I'm thinking: each card has an array (unbounded) for special properties. Each element in this array is a compound data type that holds a condition and an action. If Turing had an eval function that could take in a string and evaluate it as Turing code, we'd be in business. As it is, though, we need to write a boolean function. Another problem, though, is what should that function consume? It needs access to the entire state of the game. It's possible to feed it the entire state of the game, but that might not be the best solution. Still, though, it has merit. You could create a Game class that represents the game itself. (This also makes sense because doing this would allow your program to run multiple games at once... think of a server). So, feed the game object (you'd probably have to use self for this) to the condition function and it returns true or false. If it returns true, execute the action. The action would be of type procedure (with no parameters). |
Author: | DemonZ [ Thu Jan 25, 2007 3:24 pm ] |
Post subject: | Re: Need help with calling record in a class |
Ah I understand your logic, this would be the best way to go for it because you I would have all the different procedures waiting to be activated by a boolean array, if possible I might make a 2d or 3d array for multiple rules and circumstances, thanks alot, as for the interactivity, ill go over part 2 of the tutorial again until it makes sense to me. Thanks for the help you guys as I now know how I can organize this code, and your right, its going to be online (if I can get that to work) |