Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help with calling record in a class
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DemonZ




PostPosted: Wed Jan 24, 2007 8:33 pm   Post subject: Need help with calling record in a class

Take a look at this code:
code:

class Blue_Eyes_White_Dragon
export Stats
    type Stats :
    % Stats of the card
        record
            NAME : string
            ATK : int
            DEF : int
            DESCRIPTION : string
            TYPE : int % The card type will be represented by a number for interactivity
        end record

    var MONSTER : Stats

    MONSTER.NAME := "Blue Eyes White Dragon"
    MONSTER.ATK := 3000
    MONSTER.DEF := 2500
    MONSTER.DESCRIPTION := "Blue Eyes White Dragon Discription coming soon."
    MONSTER.TYPE := 1
end Blue_Eyes_White_Dragon

var BEWD_Pointer : pointer to Blue_Eyes_White_Dragon
new Blue_Eyes_White_Dragon, BEWD_Pointer


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?
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: 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:
Turing:

class Blue_Eyes_White_Dragon
    export Stats, output
    type Stats :
    % Stats of the card
        record
            NAME : string
            ATK : int
            DEF : int
            DESCRIPTION : string
            TYPE : int % The card type will be represented by a number for interactivity
        end record

    var MONSTER : Stats
   
    MONSTER.NAME := "Blue Eyes White Dragon"
    MONSTER.ATK := 3000
    MONSTER.DEF := 2500
    MONSTER.DESCRIPTION := "Blue Eyes White Dragon Discription coming soon."
    MONSTER.TYPE := 1
    proc output
        put MONSTER.NAME
        put MONSTER.ATK
        put MONSTER.DEF
        put MONSTER.DESCRIPTION
        put MONSTER.TYPE
    end output
end Blue_Eyes_White_Dragon
var BEWD_Pointer : pointer to Blue_Eyes_White_Dragon
new Blue_Eyes_White_Dragon, BEWD_Pointer
BEWD_Pointer -> output

Dont know if this is the best way.
CodeMonkey2000




PostPosted: 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.
Cervantes




PostPosted: 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.
DemonZ




PostPosted: 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?
Clayton




PostPosted: 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.
Cervantes




PostPosted: 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. Wink

But yeah. He's right. OOP is probably a good idea here. You just haven't used it to its potential. Yet.
Clayton




PostPosted: Thu Jan 25, 2007 1:35 pm   Post subject: Re: Need help with calling record in a class

uh.... no comment Razz

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 Very Happy
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: 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.
Turing:
class Blue_Eyes_White_Dragon
    export MONSTER, Stats
    type Stats :
    % Stats of the card
        record
            NAME : string
            ATK : int
            DEF : int
            DESCRIPTION : string
            TYPE : int % The card type will be represented by a number for interactivity
        end record

    var MONSTER : Stats

    MONSTER.NAME := "Blue Eyes White Dragon"
    MONSTER.ATK := 3000
    MONSTER.DEF := 2500
    MONSTER.DESCRIPTION := "Blue Eyes White Dragon Discription coming soon."
    MONSTER.TYPE := 1
    proc output
        put MONSTER.NAME
        put MONSTER.ATK
        put MONSTER.DEF
        put MONSTER.DESCRIPTION
        put MONSTER.TYPE
    end output
end Blue_Eyes_White_Dragon

var BEWD_Pointer : pointer to Blue_Eyes_White_Dragon
new Blue_Eyes_White_Dragon, BEWD_Pointer

BEWD_Pointer -> MONSTER.NAME := "lalalla"
put BEWD_Pointer -> MONSTER.NAME

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.
DemonZ




PostPosted: 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.
Cervantes




PostPosted: 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).
DemonZ




PostPosted: 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)
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: