-
November 17th, 2008, 09:57 AM
#1
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.
-
November 17th, 2008, 10:08 AM
#2
Re: Help with Class'
I'd probably set up bool functions such as CanMoveNorth() that would return true or false.
-
November 17th, 2008, 10:22 AM
#3
Re: Help with Class'
Must be a simpler way than that?
-
November 17th, 2008, 10:29 AM
#4
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"
-
November 17th, 2008, 11:17 AM
#5
Re: Help with Class'
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
-
November 17th, 2008, 11:23 AM
#6
Re: Help with Class'
Originally Posted by Paul McKenzie
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.
-
November 17th, 2008, 11:26 AM
#7
Re: Help with Class'
Originally Posted by GCDEF
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
-
November 17th, 2008, 11:40 AM
#8
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.
-
November 17th, 2008, 12:11 PM
#9
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.)
-
November 17th, 2008, 01:33 PM
#10
Re: Help with Class'
Originally Posted by Paul McKenzie
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?
-
November 17th, 2008, 01:43 PM
#11
Re: Help with Class'
Originally Posted by AaronHod
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
-
November 17th, 2008, 01:43 PM
#12
Re: Help with Class'
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
-
November 17th, 2008, 01:52 PM
#13
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.
-
November 18th, 2008, 11:36 PM
#14
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.
-
November 18th, 2008, 11:46 PM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|