CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Oct 2008
    Posts
    23

    Help with Classes

    Code:
    char nodeLocation = 'A';
    
    do {     
        Node * foo = new Node(nodeLocation);
        cout << "You are currently in room: \"" << foo->getName() << "\"" << endl;
        cout << "Please enter a direction: "; cin >> nodeLocation;
        }
        
    while(nodeLocation != 'L');
    
        cout << "You have reached the end of the Maze!" << endl;
        cout << "Congratulations!" << endl;
        system("PAUSE");
    Basically my "Maze" has 12 nodes, from A-L. Each node has a set of directions it can travel in... For example, "A" can go East or South... How can I make this possible? I can't think of anyway... I want to use something like "foo->getDirections()" to say which directions are possible... I can probably figure the rest out...
    Last edited by AaronHod; November 18th, 2008 at 11:37 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help with Class'

    I'd probably set up bool functions such as CanMoveNorth() that would return true or false.

  3. #3
    Join Date
    Oct 2008
    Posts
    23

    Re: Help with Class'

    Must be a simpler way than that?

  4. #4
    Join Date
    Oct 2008
    Posts
    23

    Re: Help with Class'



    So:

    1) Start at "A"
    2) Give available directions (HOW??!!)
    3) Choose a direction
    4) Set location to new node location (eg if "East" is selected, set nodeLocation to "B"

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with Class'

    Quote Originally Posted by AaronHod
    Basically my "Maze" has 12 nodes, from A-L. Each node has a set of directions it can travel in... For example, "A" can go East or South... How can I make this possible?
    Create 4 const ints that represent north, south, east, and west. Number these ints, 1, 2, 4, and 8.

    Then create a Node class that has a private unsigned int that holds the boolean OR of these values, denoting the possible directions.
    Code:
    const int NORTH = 1;
    const int EAST = 2;
    const int WEST = 4;
    const int SOUTH = 8;
    
    class Node
    {
          unsigned int possibleDirections;
        public:      
    
              Node(unsigned d) : possibleDirections(d) { }
              unsigned int getDirections( ) const { return possibleDirections; }
    };
    
    int main()
    {
       unsigned myDirections = NORTH | SOUTH;
       Node node1( myDirections );  // Node only goes north and south
    
       unsigned curDirections = node1.getDirections( );
       // how do you "parse" the directions out of the returned int?
    }
    I leave it to you to determine how to find the possible directions given the integer that is returned to you.

    Regards,

    Paul McKenzie

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help with Class'

    Quote Originally Posted by Paul McKenzie View Post
    I leave it to you to determine how to find the possible directions given the integer that is returned to you.

    Regards,

    Paul McKenzie
    That's another way to do it, but there's still no way around an if statement determining which of the four directions is available.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with Class'

    Quote Originally Posted by GCDEF View Post
    That's another way to do it, but there's still no way around an if statement determining which of the four directions is available.
    That's true. Unless the OP is privy to something we're not aware of, there is no way to determine what directions are available without testing the return value in some way.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Oct 2008
    Posts
    23

    Re: Help with Class'

    Thanks for the help guys, I'll have a play around again a bit later and let you know how I get on.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help with Class'

    Although the above is certainly more efficient, a conceptually simpler approach might be to store the directions as a std::string containing, for instance, "NSE" or "NW". (A C-style char[5] string is also an option here since there can be at most 4 directions.)

  10. #10
    Join Date
    Oct 2008
    Posts
    23

    Re: Help with Class'

    Quote Originally Posted by Paul McKenzie View Post
    Create 4 const ints that represent north, south, east, and west. Number these ints, 1, 2, 4, and 8.

    Then create a Node class that has a private unsigned int that holds the boolean OR of these values, denoting the possible directions.
    Code:
    const int NORTH = 1;
    const int EAST = 2;
    const int WEST = 4;
    const int SOUTH = 8;
    
    class Node
    {
          unsigned int possibleDirections;
        public:      
    
              Node(unsigned d) : possibleDirections(d) { }
              unsigned int getDirections( ) const { return possibleDirections; }
    };
    
    int main()
    {
       unsigned myDirections = NORTH | SOUTH;
       Node node1( myDirections );  // Node only goes north and south
    
       unsigned curDirections = node1.getDirections( );
       // how do you "parse" the directions out of the returned int?
    }
    I leave it to you to determine how to find the possible directions given the integer that is returned to you.

    Regards,

    Paul McKenzie
    Just wondering why you used "1, 2, 4 and 8" here? Is there any significance in that?

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with Class'

    Quote Originally Posted by AaronHod View Post
    Just wondering why you used "1, 2, 4 and 8" here? Is there any significance in that?
    Yes there is. Think of bits, bit patterns, and bit masks. What are the values of 1, 2, 4, and 8 in binary?

    0001
    0010
    0100
    1000

    Does this give you a hint as to why I chose those numbers? Another hint -- why did I use a bitwise OR to combine the different directions? Yet another hint -- how do you determine if a certain bit is "on" or "off" (i.e. a certain direction is available or not)?

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Help with Class'

    Quote Originally Posted by AaronHod View Post
    Just wondering why you used "1, 2, 4 and 8" here? Is there any significance in that?
    There is quite a BIT of significance...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help with Class'

    FWIW, don't get hung up on this. You have four possibilities that may be any combination of yes or no. How you choose to represent those possibilities in your node class isn't really important. There's no getting around asking the thing teh equivalent of "can you move north" or "can you move south", so don't overthink it. You're only talking about a few lines of code and a few minutes of time.

  14. #14
    Join Date
    Oct 2008
    Posts
    23

    Re: Help with Class'

    Ok, i've given up with that method...
    There must be a much simpler way to create this using a class and pointers?
    Can somebody give me a little bit of help?
    I want something like:

    | Welcome to the Maze
    | You are currently at "A"
    | You can move EAST or SOUTH
    | User Input: S
    | You are currently at "B"
    | You can move SOUTH or WEST

    etc...

    I just don't get it.
    Last edited by AaronHod; November 18th, 2008 at 11:38 PM.

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Help with Class'

    You could always look here

    and see how it was implemented in "C" about 13 years ago [there is an older Fortran version...]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured