Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2006, 7:26 PM   #21
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
Nope, you can't delete the actual m_robots and m_sources arrays because they are not allocated using new (they are allocated as part of the Valley class, so they don't need to be deleted individually). What you need to do is loop through the arrays and delete each individual entry, because those were allocated individually.
The Dark is offline   Reply With Quote
Old Nov 30th, 2006, 7:41 PM   #22
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Okay, thanks for clarifying this. I'm on the right track. Just a few more tasks I believe.

Next task I tried to do, but can't think of how, is the step function. I think knowing this would help me think of ideas for other functions:

I'm on bool Robot::step().
bool Robot::step()
{
	// If the robot has no energy left, return false
	// if (FULL_ENERGY == 0)
	//	return false;
	
       // Otherwise,
	else
	{	
		//    Randomly change direction with probability 1/3
		if (rand() % 3 == 0)    // 1/3 probability to pick a direction
			m_dir = Dir(rand() % 4);    // pick a random direction
		
	        //    Attempt to move one step in the direction we're currently facing.
		//      (E.g., to move north, decrement the row coordinate.)  If we can't
		//      move in that direction, don't move.
               if (r < 0  ||  r >= NROWS  ||  c < 0  ||  c >= NCOLS)
	    {
		cout << "***** Robot created with invalid coordinates (" << r << ","
			<< c << ")!" << endl;
		exit(1);
	    }
		switch (m_dir)
		{
		case NORTH:
			// TODO: Move one step north, if possible.
                        r--;
			break;
		case SOUTH:
                        r++;
		case WEST:
                        c++;
		case EAST:
                        c--;

The stuff I did was in blue. Can you tell me if I'm on the right track?
EDIT: Compiling said that r and c are undeclared! What must I do to use them?

Last edited by aznballerlee; Nov 30th, 2006 at 8:14 PM. Reason: indenting
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 8:11 PM   #23
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
To check energy levels you need to look at the m_energy variable.
To get the current position of the robot, you need to look at m_row and m_col.
What you have for the moves is right as far as it goes - you also need to check whether the move would put the robot out of bounds. If so, you should just return before changing the position.

Also note that, traditionally, WEST is left (c--) and EAST is right (c++).

Once you have done the move, follow the rest of the robot step instructions.
The Dark is offline   Reply With Quote
Old Nov 30th, 2006, 8:17 PM   #24
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Okay, the code works now! I'll do the rest of the step function now.

EDIT:
I continued, tried, played around, and got stuck once again.
       //    The attempt to move consumes one unit of energy.
      //    If as a result of the attempt to move, the robot is at an energy
      //       source, it's recharged to the FULL_ENERGY level.
	m_energy--;
	if (m_sources[m_nsources] = new EnergySource(r,c))
		m_energy = FULL_ENERGY;
	
	//    If at this spot there's another robot whose energy level is 0,
	//       then if we have at least SHARE_THRESHHOLD units of energy,
	//       transfer SHARE_AMOUNT units to that other robot.  (If there
	//       are two or more dead robots here, we donate to only one.)
	//    Return true, indicating the robot attempted to move.
      	else if (// another robot's energy is 0)
		if (m_energy > SHARE_THRESHHOLD)
			// that robot's energy = SHARE_AMOUNT

		return true;
}

I don't know what to stick in the parts I // out.
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 9:24 PM   #25
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Actually, I don't know why I put these lines, they're completely wrong:
if (m_sources[m_nsources] = new EnergySource(r,c))
		m_energy = FULL_ENERGY;

Using this made no difference, still error:
if (EnergySource(m_row, m_col)

I also tried the accessor for the Valley class:
bool energySourceAt(int r, int c) const
//      If there is an energy source at coordinates (r,c), return true;
//      otherwise, return false.
{
	if (EnergySource(r,c))
		return true;
	else
		return false;
}

For this, I once again got compilation errors.

Last edited by aznballerlee; Nov 30th, 2006 at 9:40 PM.
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 10:08 PM   #26
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
In the robot step method, you need to call the energySourceAt accessor for the valley object for this robot, to determine if there is an energy source at the current location.

You also need to write the energySourceAt accessor. The Valley class has an array of energy sources - you need to look through that array to see if there is one at the current coordinates.
The Dark is offline   Reply With Quote
Old Nov 30th, 2006, 10:24 PM   #27
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Quote:
In the robot step method, you need to call the energySourceAt accessor for the valley object for this robot, to determine if there is an energy source at the current location.
You are right, I was thinking about that too.
if (energySourceAt(r,c))
       m_energy = FULL_ENERGY;

Quote:
You also need to write the energySourceAt accessor
Here's my problem, it doesn't compile:
bool energySourceAt(int r, int c) const
//      If there is an energy source at coordinates (r,c), return true;
//      otherwise, return false.
{
	if (EnergySource(r,c))
		return true;
	else
		return false;
}

Quote:
The Valley class has an array of energy sources - you need to look through that array to see if there is one at the current coordinates.
if (m_sources[MAXSOURCES] =EnergySource(m_row, m_col))
EDIT: Ahh, this doesn't work.

Last edited by aznballerlee; Nov 30th, 2006 at 10:46 PM.
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 10:45 PM   #28
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by aznballerlee View Post
You are right, I was thinking about that too.
[code]
Here's my problem, it doesn't compile:
bool energySourceAt(int r, int c) const
//      If there is an energy source at coordinates (r,c), return true;
//      otherwise, return false.
{
	if (EnergySource(r,c))
		return true;
	else
		return false;
}
You need to take a step back and try to work out what you want the energySourceAt method to do, before throwing code at it.

Write down a list of steps, in English, of how the energySourceAt method could check whether there is an energy source at a particular location. Dont even worry about where the list is, just pretend you have a pile of pieces of paper with an energy location on each one, how would you go about finding whether a given location had an energy source.

The code you have at the moment just creates an EnergySource object inside the test of an if statement.

Quote:
Originally Posted by aznballerlee View Post
if (m_sources[MAXSOURCES] = energySourceAt(m_row, m_col))
For one line, responding to "look through the array" you have a lot of problems:
- I'm not even sure where you were thinking of putting that code. If it is inside the energySourceAt method, then it will be infinitely recursive. If you were going to put it in the Robot step method, then Robot doesn't have an m_sources or and energySourceAt function.
- m_sources[MAXSOURCES] is outside the boundaries of m_sources, which may cause a crash.
- You shouldn't be doing an assignment inside an if test expression
- energySourceAt returns a boolean, you can't compare that with a pointer to an energySource.
- To look through an array you need a loop.
The Dark is offline   Reply With Quote
Old Nov 30th, 2006, 10:54 PM   #29
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Quote:
Write down a list of steps, in English, of how the energySourceAt method could check whether there is an energy source at a particular location. Dont even worry about where the list is, just pretend you have a pile of pieces of paper with an energy location on each one, how would you go about finding whether a given location had an energy source.
1. Check location of robot.
2. At wherever location, check the 'piece of paper'.
3. If that 'piece of paper' has an energy source, succeed.
4. Otherwise, fail.

Quote:
For one line, responding to "look through the array" you have a lot of problems:
- I'm not even sure where you were thinking of putting that code. If it is inside the energySourceAt method, then it will be infinitely recursive. If you were going to put it in the Robot step method, then Robot doesn't have an m_sources or and energySourceAt function.
- m_sources[MAXSOURCES] is outside the boundaries of m_sources, which may cause a crash.
- You shouldn't be doing an assignment inside an if test expression
- energySourceAt returns a boolean, you can't compare that with a pointer to an energySource.
- To look through an array you need a loop.
This is inside the Valley. Anyways, still stuck on what to do. I started a loop ..
for (int k = 0; k < MAXSOURCES; k++_
{
   if ( array element has energy source  )
//  return true;
   else
     return false;
}

Earlier, you said this:
Quote:
You also need to write the energySourceAt accessor. The Valley class has an array of energy sources - you need to look through that array to see if there is one at the current coordinates.
but you're saying I can't use m_sources[m_nsources]. Confused.

Last edited by aznballerlee; Nov 30th, 2006 at 11:05 PM.
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 11:07 PM   #30
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by aznballerlee View Post
1. Check location of robot.
2. At wherever location, check the 'piece of paper'.
3. If that 'piece of paper' has an energy source, succeed.
4. Otherwise, fail.
1. OK the location is given to you in the parameters of energySourceAt, so forget about the robot for a bit.
2. You need to break this step down - how do you know which piece of paper in the pile you need to look at?
3. Yep, except don't forget you might not find a piece of paper for the location you are after - you only have a piece of paper for each energy source, not each location. So its more like "if you find a piece of paper with the right location, succeed".
4. That's right.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd compile Error. Need help please. Keiyentai Java 7 Aug 19th, 2006 1:35 AM
What is: "Oriented programming (OO)?" BrinyCode C++ 12 Nov 22nd, 2005 7:40 AM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 7:25 PM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 3:41 PM
Programmers Needed! Online Game troy_eisert C++ 2 Jan 29th, 2005 12:51 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC