Computer Science Canada Advice on how to organize my class's? |
Author: | GreatPumpkin [ Sat Feb 22, 2014 1:50 am ] | ||
Post subject: | Advice on how to organize my class's? | ||
What is it you are trying to achieve? I'm working on my overhead civilization-like game after taking a break for a little while. I've decided to rewrite my code again completely, switching certain aspects of it into functions, classes and modules. What is the problem you are having? My problem is that I am unsure of how to organize my code. Before my rewrite I had a large class which contained cities, along with the units (and soon to be farms) that belong to the city. It doesn't seem proper to have two separate sets of objects in the same class, but the units belong to the cities. I originally had an array of a custom record type called cityType; containing the cities x, y, maxUnits etc. along with an array of another custom type called units; containing an x and y. This large class was called playerObjects, however I was also going to use it in the long run for the enemy units. This was working fine but like I said seems somewhat improper. So it's kind of like the units are a child of the cities, they are reliant on the cityType and it's information. I have no Idea how to separate them into different classes, or if I even should. Any input would be appreciated. Describe what you have tried to solve this problem <Answer Here> Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) <Answer Here>
Please specify what version of Turing you are using 4.1.1 |
Author: | Insectoid [ Sat Feb 22, 2014 12:36 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
You could include a pointer to city in the unit class, which points to the parent city. Any information the units need from the city should be available in the public functions of the city, which are accessed via the pointer. The city needs only an array of units. |
Author: | GreatPumpkin [ Sat Feb 22, 2014 7:21 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
So the unitsClass holds only the variables of the stats, methods of interaction and a pointer to the citiesClass, with no arrays inside? and by pointer do you mean the "pointer to" or a variable that holds the number of the city it belongs to ? |
Author: | evildaddy911 [ Sat Feb 22, 2014 7:41 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
you could probably do either one. however, insectoid most likely meant "pointer to" |
Author: | Insectoid [ Sat Feb 22, 2014 8:53 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
Quote: So the unitsClass holds only the variables of the stats, methods of interaction and a pointer to the citiesClass, with no arrays inside?
The unit class contains any variables and functions that it requires. The pointer gives it access to all of the city class's public variables and functions. |
Author: | GreatPumpkin [ Sat Feb 22, 2014 10:40 pm ] | ||
Post subject: | Re: Advice on how to organize my class's? | ||
I don't understand what you mean by an array of units in the city in your first post. If my citiesClass comes before my unitsClass, and the record for the type unitsType is within the unitsClass, how do I make an array of unitsType? Also if theres only a pointer from unitsClass to citiesClass, how do I manipulate the array of units in the city with methods in unit's class? Nor do I understand what you mean by "a pointer to city in the unit class, which points to the parent city" Is the pointer to the citiesClass? or is it a pointer to the array of cityTypes inside the citiesClass? I really don't understand at all, can you give me an example ? Here is the code for my unitsClass I've made.
|
Author: | Dreadnought [ Sun Feb 23, 2014 2:20 am ] | ||||||||||
Post subject: | Re: Advice on how to organize my class's? | ||||||||||
GreatPumpkin wrote: I don't understand what you mean by an array of units in the city in your first post. If my citiesClass comes before my unitsClass, and the record for the type unitsType is within the unitsClass, how do I make an array of unitsType? Also if theres only a pointer from unitsClass to citiesClass, how do I manipulate the array of units in the city with methods in unit's class? GreatPumpkin wrote: Nor do I understand what you mean by "a pointer to city in the unit class, which points to the parent city" Is the pointer to the citiesClass? or is it a pointer to the array of cityTypes inside the citiesClass? GreatPumpkin wrote: I really don't understand at all, can you give me an example ? Well in the ugly beast that is Turing's OOP that's not very obvious since we can't use forward declarations for types. So we get
Putting each class in its own unit doesn't get us much farther since Turing will complain about circular dependency. So what can we do? My solution (and I encourage anyone else with a nice solution to post it) is to have some kind of type that will store sufficient information about B so that A can use it. Like a parent class for B that contains everything A will need. (I don't really find this to be an elegant solution but I can't come up with something better for now) Here's an example (in multiple files) CityData.tu
Citizen.tu
City.tu
main.t (main program, call the file what you want)
Note that this is an example of how I might do it (although I split City into two pieces you might find it easier to split Citizen or do something else entirely). Another way would be using implement instead of inherit for the relation of CityData (or whatever we want to call it) and City. Basically, this is the nicest thing I can come up with for now, but I find it clunky. |
Author: | GreatPumpkin [ Sun Feb 23, 2014 2:36 pm ] |
Post subject: | Re: Advice on how to organize my class's? |
Dreadnought @ Sun Feb 23, 2014 2:20 am wrote: Well in the ugly beast that is Turing's OOP that's not very obvious since we can't use forward declarations for types. This makes me feel alot better about not understanding this. Dreadnought @ Sun Feb 23, 2014 2:20 am wrote: Basically, this is the nicest thing I can come up with for now, but I find it clunky. Your example and solution make a bit of sense to me, I'll play around with it later, but it seems somewhat straightforward. Do you think it would be poor practice to continue with a single class that encompasses an individual players (user/ai enemy/second player) cities, units, and farms? |
Author: | Tony [ Sun Feb 23, 2014 3:07 pm ] |
Post subject: | Re: Advice on how to organize my class's? |
GreatPumpkin @ Sun Feb 23, 2014 2:36 pm wrote: Do you think it would be poor practice to continue with a single class that encompasses an individual players (user/ai enemy/second player) cities, units, and farms? Yes, because that's an anti-pattern described by https://en.wikipedia.org/wiki/God_object |
Author: | GreatPumpkin [ Sun Feb 23, 2014 6:56 pm ] | ||||
Post subject: | Re: Advice on how to organize my class's? | ||||
So the majority of methods for the citizens are actually held in the city.tu class is your example ? Theres some vocabulary I haven't encountered there, and was wondering if you could give me a run down of what happens during a few parts:
self and assert
nil. I will research them when I get back home though. |
Author: | evildaddy911 [ Sun Feb 23, 2014 7:32 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
self: when you use self, you are referencing the object you are in. in this case, you are saying Set Parent City to "this city" assert: this checks to see if the following statement is true. if it is false (if upper(citizens) < citizenCount), then the program will crash. its purpose is for detecting exactly where a problem originated nil: its a null reference. it tells the program that parentCity doesnt have any data yet |
Author: | GreatPumpkin [ Sun Feb 23, 2014 10:16 pm ] | ||
Post subject: | Re: Advice on how to organize my class's? | ||
Ok I think my problems are coming from not understanding pointers. I've looked up as many tutorials and guides about them but I just can't grasp them and it's really starting to bug me. All I know is that they're a variable that holds the information of the address of another variable. I don't really understand how it ties into all of this or how it's used. Could somebody give me as simple dumbed down explanation of pointers with an example of how and where they'd be used? Like I said I have looked up tutorials and I'm still extremely lost. If you had a string that read "text" and a pointer to the string, put both the string and the pointer they should both put out as "text" ? I tried that but it can only be used with collection's, class's, and types.. So I made a little object named textContainer. Pretty much it's just a string of text that has a method to write the text into the string and to put the string.
Can somebody please dissect and explain this part for me : var myTextContainer : ^textContainer new textContainer, myTextContainer the pointer to and the new, I just don't understand those concepts, I've tried to but I can't. If the pointer just points to the address of the class, and not the class and information in it, how is it put to use? I really need somebody to help me understand this, because I'm clearly not going to get anywhere by myself. Also, if somebody could show me how I could complete my bolded example above using the textContainer I've made, it might help me? I'm feeling extremely lost and like quitting all together if I can't grasp these obviously important concepts soon. |
Author: | Raknarg [ Sun Feb 23, 2014 10:36 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
Well I can give it a go. Correct me if I'm wrong. So you have things like int, real, char, etc. Those all take a very tiny amount of information. So, variables can actually store the info. Think of these like a box that you put stuff in. You just look at the box, and there it is. However, these boxes can only hold one item at a time. A pointer is different. Imagine your box still only holds one item, but inside your box there's a card that tells you where you can find a warehouse full of other boxes that store information. These are what pointers are (things like arrays and objects). The primitive data types are a box which holds info, and the pointers are boxes that hold the info of where you can find all the other boxes (those boxes being items in your array, or data fields in a class). This is because, like I said, your box can only hold one item at a time. You can't cram in all those pieces of info together, so instead you just store it somewhere else. In this case, you're creating an item which holds direction for the computer to find all the information of that object. Lets take your example: var myTextContainer : ^textContainer new textContainer, myTextContainer First, you make a pointer to a text container. This means that myTextContainer is going to hold info for the directions of where the computer can find a copy of textContainer. It doesn't yet, but the program is saying that's what it will be. Then you initialize it. Now textContainer is actually pointing to that information. Understanding pointers is important when managing your information. You're used to doing this: var x : int := 5 var y : int := x x := 7 Normally this would mean that y copies whatever is in x's box and put it in its own. In this case, x is now 7 and y is now 5. However, you cannot do the same things with pointers, because they don't contain the object itself, they contain directions to the object. So: var x : ^SomeObject := makeNewObject() new SomeObject, x x -> someNumberInSomeObject := 5 var y : ^SomeObject := x x -> someNumberInSomeObject := 7 that data field, someNumberInSomeObject is now the same for x and y, because they're both actually using the same object, as they both have the same directions. |
Author: | Dreadnought [ Sun Feb 23, 2014 11:47 pm ] | ||
Post subject: | Re: Advice on how to organize my class's? | ||
Raknarg wrote: So you have things like int, real, char, etc. Those all take a very tiny amount of information. So, variables can actually store the info.
There isn't really a problem with variables taking up space in Turing (but it is an issue in other languages like C), for example this runs fine on my computer
Here's my "concrete" example of the difference between a value and a pointer. (Raknarg's example with boxes works too but this one feels more intuitive to me) If I tell Alice "Your number is 5." and tell Bob "Your number is 5.", then tell Alice "Double your number.", Bob's number is still 5 but Alice's is 10 Now, lets say I tell Alice "Your table is this" (as I point my finger at a table) then tell Bob "Your table is this" (pointing my finger at the same table), then I tell Alice "Paint your table red". Now Bob's table is red and Alice's table is red (since they are the same table). The idea is that in Object Oriented Programming, objects represent some kind of entity. So if I give Alice and Bob the same object, then any changes Alice makes to her object affect Bob's object (since they are the same object). So how do we translate that into a computer program? (this is where Raknarg's example with boxes is more intuitive) You create new objects by setting aside space in memory (making room in your warehouse) for the object you want (box of the right size). In Turing this is done using new. Then you have pointers which tell you where the object is in memory (cards telling you where to look in the warehouse). This means that if Alice and Bob have cards that tell them to look at the same spot in the warehouse, and Alice paints the object in her box there red, Bob's object will also be red (again, it's the same object). Raknarg wrote: var x ^SomeObject = makeNewObject() new SomeObject, x x -> someNumberInSomeObject = 5 var y ^SomeObject = x x -> someNumberInSomeObject = 7 I assume the "makeNewObject()" is not intended to be there since there is a new right after. I'd like to add a few things: nil: Basically an invalid location in memory (usually 0), think of it as a blank card (in Raknarg's warehouse of boxes example). It's good practice to set pointers to nil when they do not point to a valid object. free: So you set aside all this space in memory for objects, but when you're done with objects you should call free on a pointer to tell the computer you no longer need that space in memory (so the computer can reuse it). Otherwise the computer will continue to assume you are using it, even if you don't have a pointer pointing to that location in memory. You should always free objects, otherwise you might "leak memory". (Note that when your program finished all memory set aside for it is returned to the OS, but it's still good practice to avoid memory leaks) |
Author: | Raknarg [ Mon Feb 24, 2014 12:16 am ] |
Post subject: | RE:Advice on how to organize my class\'s? |
>I assume the "makeNewObject()" is not intended to be there since there is a new right after. Right, im mixing up with java here. Thank for the corrections. |
Author: | GreatPumpkin [ Thu Feb 27, 2014 2:21 pm ] |
Post subject: | Re: Advice on how to organize my class's? |
Thanks you guys very much for the help. I have alot better understanding of pointers, however I'm still not incredibly sure of how to use the in script, for things like lyam_kaskade's pointer tutorial in the Turing Walkthrough http://compsci.ca/v3/viewtopic.php?t=9284. I asked my teacher and another student in the class, and they both suggest witching to c++. They both seemed surprised at the thought of using pointers for anything other than a last ditch effort. I knew there'd be a time to learn a new language, and guess that time is now. I will continue to work with turing I'm sure, but for now I'm focusing my efforts on learning c++, ya'll should expect lot's of questions from me in the c++ forum. Thanks again for the help. |
Author: | Tony [ Thu Feb 27, 2014 3:22 pm ] |
Post subject: | Re: Advice on how to organize my class's? |
GreatPumpkin @ Thu Feb 27, 2014 2:21 pm wrote: I asked my teacher and another student in the class, and they both suggest witching to c++. They both seemed surprised at the thought of using pointers for anything other than a last ditch effort.
This doesn't make sense. C++ will require you to use pointers at least as much. |
Author: | Insectoid [ Thu Feb 27, 2014 5:37 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
Well, if you've got to use pointers, you might as well use a language with a decent implementation. I'm not a fan of Turing's. |
Author: | Raknarg [ Thu Feb 27, 2014 7:06 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
I don't know much about pointers, would you like to elaborate? |
Author: | Dreadnought [ Thu Feb 27, 2014 8:49 pm ] |
Post subject: | Re: Advice on how to organize my class's? |
Insectoid wrote: Well, if you've got to use pointers, you might as well use a language with a decent implementation. I'm not a fan of Turing's.
I agree that Turing's "pointers" aren't that great, but I find Turing has a decent approach to pointers and objects for beginners. I'm not sure that the implementation of pointers that C++ offers is worth all the ways in which you can shoot yourself in the foot when your starting out. |
Author: | evildaddy911 [ Thu Feb 27, 2014 9:01 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
greatpumpkin: i actually still use turing quite a lot. ive mostly switched over to java for the most part, but if its a relatively small project/doesnt require classes then ill use turing 100%. as a matter of fact, last semester i did my gr12 CS culminating in turing. its up on my github page if anyone wants to take a look |
Author: | Raknarg [ Thu Feb 27, 2014 9:18 pm ] |
Post subject: | RE:Advice on how to organize my class\'s? |
evildaddy you should look up Processing. I used to be like that with turing, but now I never use it. There's lots of limitations with turing, and this mkes you more comfortable with java: www.processing.org |
Author: | GreatPumpkin [ Mon Mar 10, 2014 9:15 pm ] | ||||||
Post subject: | Re: Advice on how to organize my class's? | ||||||
Well I am beginning to learn c++, after the march break I'll be making my number guessing game, then some other stuff before starting interfacing, I'm thinking of making a thumb drive that holds a couple 2 or 4 bytes and use it for any future robots I make in class, I've got a design for one that'll read preprogrammed instructions. Can you guys tell me for what types of things you'd use pointers in c++? Also I believe I have created a solution to my problem with the child units and parent cities:
I have a procedure called getParentCityNo, which has a single input integer. Inside the unitsClass I have an integer that holds the information of the parentCityNo. parentCityNo is set to whatever parameter is provided with getParentCityNo.
I've got a procedure called pushCityNo, what this does is when given the number of the unit that is needs to know it's parents cityNo, it runs the getParentCityNo proc mentioned in the last class on the specified unit, providing it's own cityNo as the parameter.
Slap all three codes together to try it out for yourselves, I'm sure there'll be somebody else in the future that needs to know how to make parent/child arrays of class's. |
Author: | Dreadnought [ Tue Mar 11, 2014 8:37 pm ] |
Post subject: | Re: Advice on how to organize my class's? |
GreatPumpkin wrote: Can you guys tell me for what types of things you'd use pointers in c++? Anytime you have an array, that's a pointer. Most objects are manipulated by pointers and many data structures use pointers (trees, linked lists and graphs are some of the more common ones). Also, anytime you want something to stick around when you go out of scope, you'll need a pointer (usually for objects/data structures). GreatPumpkin wrote: Also I believe I have created a solution to my problem with the child units and parent cities: Your solution works, but it has some drawbacks. Firstly, your units cannot know what a city is, all they can know is the number of city and no more. If a unit wanted to know something about its city, it would need to use some other system that would find the information needed and give it to the unit. If anything else wants to know about the city a unit belongs to, it must get the city number and use this separate system again to get to the city's data. This "separate system" ends up being a bit like the "God objects" Tony mentioned. You also have a problem if you try to expand your program, if you want to include things like houses for example. Cities need to know about houses and houses need to know about cities. But houses need to know about the units living in them and the units need to know about their houses (and possibly more than just the house number). Now units have 2 numbers to keep track of and houses have at least 1 (possibly 2). Keep adding classes and you soon realise that your classes have numbers everywhere and can't know what these numbers mean. What you've created are pointers which can only be used by other objects (and require the aforementioned "God object"). |