Posted: 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 Walkthroughhttp://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.
Sponsor Sponsor
Tony
Posted: 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.
Posted: 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.
Raknarg
Posted: 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?
Dreadnought
Posted: 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.
evildaddy911
Posted: 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
Raknarg
Posted: 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:
Posted: 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:
var unitNo, parentCityNo :int:=0 var unitHealth :int:=10 var x, y :int:=0
proc putAllInfo
put x, ', ', y
put unitNo, " from ", parentCityNo
put unitHealth
end putAllInfo
proc add (X, Y, No :int)
x := X
y := Y
unitNo := No
end add
proc remove
x :=0
y :=0
unitNo :=0
parentCityNo :=0 end remove
proc getParentCityNo (No :int)
parentCityNo := No
end getParentCityNo
fcn checkActive :boolean if unitNo > 0then result true elsif unitNo =0then result false endif end checkActive
proc damage (D :int)
unitHealth -= D
if unitHealth =0then
remove
endif end damage
proc putHealth
put unitHealth
end putHealth
proc draw
Draw.FillBox(x *10, y *10, x *10 + 10, y *10 + 10, black) end draw
proc moveUp
y +=1 end moveUp
proc moveRight
x +=1 end moveRight
proc moveDown
y -=1 end moveDown
proc moveLeft
x -=1 end moveLeft
end unitClass
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.
fcn checkActive :boolean if cityNo > 0then result true elsif cityNo =0then result false endif end checkActive
%units interactions var unitsArray :array1.. 8ofpointerto unitClass
for i :1.. 8 new unitClass, unitsArray (i) endfor
proc pushCityNo (U :int)
unitsArray (U) -> getParentCityNo (cityNo) end pushCityNo
fcn checkActiveUnit (U :int):boolean if unitsArray (U) -> checkActive =truethen resulttrue elsif unitsArray (U) -> checkActive =falsethen resultfalse endif end checkActiveUnit
var citiesUnits :string:="" proc checkCitiesUnits
citiesUnits :="" for i :1.. 8 if unitsArray (i) -> checkActive =truethen
citiesUnits := citiesUnits + "1" elsif unitsArray (i) -> checkActive =falsethen
citiesUnits := citiesUnits + "0" endif
endfor put citiesUnits
end checkCitiesUnits
proc addUnit (X, Y :int) if checkActive =truethen for i :1.. 8
if unitsArray (i) -> checkActive =falsethen
unitsArray (i) -> add (X, Y, i)
pushCityNo (i) exit endif endfor endif end addUnit
%%%%unit re-interactions
proc putAllUnitInfo (U :int) if unitsArray (U) -> checkActive =trueand checkActive =truethen
unitsArray (U) -> putAllInfo
endif end putAllUnitInfo
proc putUnitsHealth (U :int) if unitsArray (U) -> checkActive =trueand checkActive =truethen
unitsArray (U) -> putHealth
endif end putUnitsHealth
proc damageUnit (U, D :int) if unitsArray (U) -> checkActive =trueand checkActive =truethen
unitsArray (U) -> damage (D) endif end damageUnit
proc drawUnits
for i :1.. 8 if unitsArray (i) -> checkActive =trueand checkActive =truethen
unitsArray (i) -> draw
endif endfor end drawUnits
proc drawUnit (U :int) if unitsArray (U) -> checkActive =trueand checkActive =truethen
unitsArray (U) -> draw
endif end drawUnit
proc moveUnitUp (U :int) if unitsArray (U) -> checkActive =trueand checkActive =truethen
unitsArray (U) -> moveUp
endif end moveUnitUp
proc moveUnitDown (U :int) if unitsArray (U) -> checkActive =trueand checkActive =truethen
unitsArray (U) -> moveDown
endif end moveUnitDown
proc moveUnitLeft (U :int) if unitsArray (U) -> checkActive =trueand checkActive =truethen
unitsArray (U) -> moveLeft
endif end moveUnitLeft
proc moveUnitRight (U :int) if unitsArray (U) -> checkActive =trueand checkActive =truethen
unitsArray (U) -> moveRight
endif end moveUnitRight
end cityClass
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.
Turing:
var selectedUnit :=1 var selectedCity :=1
%%%interactions for cities var playerCitiesArray :array1.. 15ofpointerto cityClass
for i :1.. 15 new cityClass, playerCitiesArray (i) endfor
proc addCity
for i :1.. 15 if playerCitiesArray (i) -> checkActive =falsethen
playerCitiesArray (i) -> add (i) exit endif endfor end addCity
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.
Sponsor Sponsor
Dreadnought
Posted: 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").