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

Username:   Password: 
 RegisterRegister   
 Advice on how to organize my class's?
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GreatPumpkin




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




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




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




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




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




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




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

Turing:


class unitClass

    export checkActive, damage, putAllInfo, putHealth, draw, add, getParentCityNo, moveUp, moveDown, moveLeft, moveRight

    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 > 0 then
            result
                true
        elsif unitNo = 0 then
            result
                false
        end if
    end checkActive

    proc damage (D : int)
        unitHealth -= D
        if unitHealth = 0 then
            remove
        end if
    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.



Turing:

class cityClass

    import unitClass
    export checkCitiesUnits, add, checkActive, checkActiveUnit, damageUnit, putUnitsHealth, putAllUnitInfo, drawUnits, drawUnit, addUnit, moveUnitUp, moveUnitDown, moveUnitLeft, moveUnitRight




    var cityNo : int := 0

    proc add (No : int)
        cityNo := No
    end add

    fcn checkActive : boolean
        if cityNo > 0 then
            result
                true
        elsif cityNo = 0 then
            result
                false
        end if
    end checkActive





    %units interactions
    var unitsArray : array 1 .. 8 of pointer to unitClass
    for i : 1 .. 8
        new unitClass, unitsArray (i)
    end for

    proc pushCityNo (U : int)
        unitsArray (U) -> getParentCityNo (cityNo)
    end pushCityNo


    fcn checkActiveUnit (U : int) : boolean
        if unitsArray (U) -> checkActive = true then
            result true
        elsif unitsArray (U) -> checkActive = false then
            result false
        end if
    end checkActiveUnit

    var citiesUnits : string := ""
    proc checkCitiesUnits
        citiesUnits := ""
        for i : 1 .. 8
            if unitsArray (i) -> checkActive = true then
                citiesUnits := citiesUnits + "1"
            elsif unitsArray (i) -> checkActive = false then
                citiesUnits := citiesUnits + "0"
            end if

        end for
        put citiesUnits
    end checkCitiesUnits


    proc addUnit (X, Y : int)
        if checkActive = true then
            for i : 1 .. 8

                if unitsArray (i) -> checkActive = false then
                    unitsArray (i) -> add (X, Y, i)
                    pushCityNo (i)
                    exit
                end if
            end for
        end if
    end addUnit




    %%%%unit re-interactions

    proc putAllUnitInfo (U : int)
        if unitsArray (U) -> checkActive = true and checkActive = true then
            unitsArray (U) -> putAllInfo
        end if
    end putAllUnitInfo

    proc putUnitsHealth (U : int)
        if unitsArray (U) -> checkActive = true and checkActive = true then
            unitsArray (U) -> putHealth
        end if
    end putUnitsHealth

    proc damageUnit (U, D : int)
        if unitsArray (U) -> checkActive = true and checkActive = true then
            unitsArray (U) -> damage (D)
        end if
    end damageUnit

    proc drawUnits
        for i : 1 .. 8
            if unitsArray (i) -> checkActive = true and checkActive = true then
                unitsArray (i) -> draw
            end if
        end for
    end drawUnits

    proc drawUnit (U : int)
        if unitsArray (U) -> checkActive = true and checkActive = true then
            unitsArray (U) -> draw
        end if
    end drawUnit

    proc moveUnitUp (U : int)
        if unitsArray (U) -> checkActive = true and checkActive = true then
            unitsArray (U) -> moveUp
        end if
    end moveUnitUp

    proc moveUnitDown (U : int)
        if unitsArray (U) -> checkActive = true and checkActive = true then
            unitsArray (U) -> moveDown
        end if
    end moveUnitDown

    proc moveUnitLeft (U : int)
        if unitsArray (U) -> checkActive = true and checkActive = true then
            unitsArray (U) -> moveLeft
        end if
    end moveUnitLeft

    proc moveUnitRight (U : int)
        if unitsArray (U) -> checkActive = true and checkActive = true then
            unitsArray (U) -> moveRight
        end if
    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 : array 1 .. 15 of pointer to cityClass
for i : 1 .. 15
    new cityClass, playerCitiesArray (i)
end for


proc addCity
    for i : 1 .. 15
        if playerCitiesArray (i) -> checkActive = false then
            playerCitiesArray (i) -> add (i)
            exit
        end if
    end for
end addCity


addCity


playerCitiesArray (selectedCity) -> addUnit (1, 1)
playerCitiesArray (selectedCity) -> addUnit (1, 5)
playerCitiesArray (selectedCity) -> addUnit (1, 10)


loop
    cls
    playerCitiesArray (selectedCity) -> checkCitiesUnits
    playerCitiesArray (selectedCity) -> drawUnits
    playerCitiesArray (selectedCity) -> moveUnitRight (selectedUnit)
    playerCitiesArray (selectedCity) -> damageUnit (selectedUnit, 2)

    delay (1000)
end loop



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
Sponsor
sponsor
Dreadnought




PostPosted: 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").
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 2 of 2  [ 24 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: