Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Dec 1st, 2006, 3:21 PM   #51
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
In other words, I tried and this is what I got. Correct?

Robot* otherRobotAt(Robot* rp) const
//      If there is at least one robot (other than the one rp points to)
//      at the same (r,c) coordinates as the one rp points to, return a
//      pointer to the one of those other robots with the least amount
//      of energy remaining (if there's a tie, any one of the tied robots
//      will do); otherwise, return NULL.
//
{
	for (int i = 0; i< m_nrobots; i++)
	{
		if ((m_robots[i] -> row()==r)
			&& (m_robots[i] -> col()==c)
			return this; 
	}
	return NULL;
}
aznballerlee is offline   Reply With Quote
Old Dec 1st, 2006, 4:52 PM   #52
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by aznballerlee View Post
In other words, I tried and this is what I got. Correct?

Robot* otherRobotAt(Robot* rp) const
//      If there is at least one robot (other than the one rp points to)
//      at the same (r,c) coordinates as the one rp points to, return a
//      pointer to the one of those other robots with the least amount
//      of energy remaining (if there's a tie, any one of the tied robots
//      will do); otherwise, return NULL.
//
{
	for (int i = 0; i< m_nrobots; i++)
	{
		if ((m_robots[i] -> row()==r)
			&& (m_robots[i] -> col()==c)
			return this; 
	}
	return NULL;
}
If you look at the technicolour code above, here are things to look at:
This function is a member of Valley, so you can't return this, because this is a valley object, not a robot object
You need to check that you aren't matching the robot in the array with the robot that was passed in
You should get the coordinates to check for from the Robot passed in
You can't just return the first robot you find on that position, you need to find the one with the least energy
The Dark is offline   Reply With Quote
Old Dec 1st, 2006, 6:22 PM   #53
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Possibly wrong, but I tried.

Robot* otherRobotAt(Robot* rp) const
//      If there is at least one robot (other than the one rp points to)
//      at the same (r,c) coordinates as the one rp points to, return a
//      pointer to the one of those other robots with the least amount
//      of energy remaining (if there's a tie, any one of the tied robots
//      will do); otherwise, return NULL.
//
{
	for (int i = 0; i< m_nrobots; i++)   // check each robot
	{
		if ((m_robots[i] -> row()==m_row)      // if robot matches coordinate
			&& (m_robots[i] -> col()==m_col && (m_robot[i]!= rp))  // and robot not the one we're looking at
			int n = m_source[i];       
			for (int j = 0; j < i; j++)        // find robot of minimum energy
			{   
				if (m_source[j] < n) 
				       n = m_source[j];
			}	
			return m_robots[i];                 //returns robot of least energy
				
	}
	return NULL;
}
aznballerlee is offline   Reply With Quote
Old Dec 1st, 2006, 10:12 PM   #54
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
The Dark:

After 2 hours, I fixed my errors:

Robot* Valley::otherRobotAt(Robot* rp) const
{
Robot* robotToReturn = NULL;
int leastEnergy = FULL_ENERGY;
for (int i=0; i < m_nrobots; i++)
{
	if ((m_robots[i] -> row() == rp -> row() ) 
		&& (m_robots[i] ->  col() == rp -> col()) 
		&& (m_robots[i] != rp) 
		&& (robotToReturn -> energy() < leastEnergy))
	{
		robotToReturn = m_robots[i];
		leastEnergy = robotToReturn -> energy();
	}
}
	return robotToReturn;


}
aznballerlee is offline   Reply With Quote
Old Dec 1st, 2006, 11:02 PM   #55
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
I'm onto my next task, and I'm stuck. This is in the Robot::step function.
Quote:
// 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.
     for (int h=0; h < m_nrobots; h++)
    {
        if ((m_robots[h] -> row() == this -> row() )
            && (m_robots[h] ->  col() == this -> col())
            && (m_robots[h] != this)
            && ((m_robots[h] -> energy() == 0)))
            return true;
        {
            m_energy -= SHARE_AMOUNT;
            m_robots[h] -> energy() = SHARE_AMOUNT;
        }
    }

I have two errors which you can help me figure how to fix.

Quote:
1. left of '->energy' must point to class/struct/union/generic type
Currrently, the m_robots[h] are of type int. I don't know how to turn them into classes.

Quote:
2. m_nrobots and m_robots are undeclared identifiers.
I looked at the classes, and that's right. Those are part of the Valley class. There aren't anything variables (not that i can find) in Robot that can tell me the robot size though!
aznballerlee is offline   Reply With Quote
Old Dec 1st, 2006, 11:37 PM   #56
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Not meant to overwhelm you, but I worked on the remaining functions, and I'll pose my problems here. (I think most of them revolve aroudn the same idea, so we can work on them simultaneously)

1. Robot:step (remaining part)
This problem is listed in my previous post(s).

2. Valley::display() (Part 1)
        
// TODO:  indicate each robot's position
// for each robot in the valley,
//   set the appropriate element of the grid to the first character
//          of the robot's name.
    for (int b = 0; b < m_nrobots; b++)
    {
        grid[r][c] = 'name()';   // TODO: Fix name() so only first char of name
I have trouble setting name to only first char. Also, not sure if my steps are right.

3. Valley::display() (Part 2)
        
        // TODO:  Write robot energy info
        // for each robot in the valley,
        //   write the robot's name and remaining energy level.

        cout << name() << endl;
        cout << energy() << endl;
Also, not sure I did this part right as well.

4. Valley::step()
Robot:step () made the single robot move. Valley::step() makes ALL the robots move one step.

bool Valley::step()
{
    //      TODO: implement step
    //      Have each robot in the valley step.  If any of them attempted to move,
    //      return true.  If none of them did, they're all dead, so return false.


for (int p = 0; p < m_nrobots; p++)
{
    if (m_robots[p].step)
        return true;
}
 return false;
}
Once again, the main issue is that the one in Problem 1. The left side is not a pointer to class or something . Blah.
aznballerlee is offline   Reply With Quote
Old Dec 2nd, 2006, 4:47 AM   #57
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by aznballerlee View Post
The Dark:

After 2 hours, I fixed my errors:

Robot* Valley::otherRobotAt(Robot* rp) const
{
Robot* robotToReturn = NULL;
int leastEnergy = FULL_ENERGY;
for (int i=0; i < m_nrobots; i++)
{
	if ((m_robots[i] -> row() == rp -> row() ) 
		&& (m_robots[i] ->  col() == rp -> col()) 
		&& (m_robots[i] != rp) 
		&& (robotToReturn -> energy() < leastEnergy))
	{
		robotToReturn = m_robots[i];
		leastEnergy = robotToReturn -> energy();
	}
}
	return robotToReturn;


}
There is a problem with the red line - robotToReturn hasn't been set at this point (at least the first time rounf the loop - the other times it is set to a robot you don't want to test again).

1. You don't need that loop at all - you just wrote a function to find other robots at the given location, you should be using it.
2.
        grid[r][c] = 'name()';   // TODO: Fix name() so only first char of name
- Where do r anc c come from?
- name() should not be in single quotes
- name() should be called on the robot you are currently looking at
- You can access the first character of a std:string the same way you would access the first element of an array.

3. The english requirement says "for each robot..." thats a pretty big clue that your are going to need a for loop. See #2 for other changes needed.

4. You have already written this kind of code in your otherRobotAt function, where you used "->" and "()" correctly. I'm not sure why you have reverted to using "." and trying to call functions without the "()".
You also need to try to call step for all of the robots, at the moment you stop on the first successful one. You need to just record if one of them is successful and move on to the next one.
The Dark is offline   Reply With Quote
Old Dec 2nd, 2006, 12:42 PM   #58
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Quote:

There is a problem with the red line - robotToReturn hasn't been set at this point (at least the first time rounf the loop - the other times it is set to a robot you don't want to test again).
I don't understand. Are you saying that we're testing the same robotToReturn each time? Does this mean I need to test the i'th element of the robotToReturn?

1. How's this?
	//    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.
	
		if ((otherRobotAt(Robot* rp)-> energy() == 0)
			&& (m_energy > SHARE_THRESHHOLD))
		{
			m_energy -= SHARE_AMOUNT;
			otherRobotAt(Robot* rp)-> energy() = SHARE_AMOUNT;
                        return true;
		}

2.
grid[r][c] = 'name()'; // TODO: Fix name() so only first char of name
Quote:
- Where do r anc c come from?
- name() should not be in single quotes
- name() should be called on the robot you are currently looking at
- You can access the first character of a std:string the same way you would access the first element of an array.
r and c are the rows and columns, they come from the Robot and energySoucrce .. they were declared in the beginning of this Valleyisplay function. This shows what I changed, along with what was before it.
void Valley::display() const
{
	char grid[NROWS][NCOLS];
	int r, c;

	// fill the grid with dots
	for (r = 0; r < NROWS; r++)
	{
		for (c = 0; c < NCOLS; c++)
		{
			grid[r][c] = '.';
		// TODO:  mark each energy source with a star
		// for each energy source in the valley,
		//   set the appropriate element of the grid to '*'
			if (energySourceAt(r,c))
				grid[r][c] = '*';
		}
	}	
	
		// TODO:  indicate each robot's position
		// for each robot in the valley,
		//   set the appropriate element of the grid to the first character
		//          of the robot's name.
	for (int b = 0; b < m_nrobots; b++)
	{
		grid[r][c] = vp -> name()[0];   // TODO: Fix it so only first char of name
	
	}

3. You are right. Is this on the right track?
      
  // TODO:  Write robot energy info
  // for each robot in the valley,
  //   write the robot's name and remaining energy level.

for (int c = 0; c < m_nrobots; c++)
{
cout << m_robot [c] -> name() << endl;
cout << m_robot [c] -> energy() << endl;
}

4. I made a change in this, but I understand you, it still ends prematurely.
I can't seem to think of a way to prevent this ..
bool Valley::step()
{
    //      TODO: implement step
    //      Have each robot in the valley step.  If any of them attempted to move,
    //      return true.  If none of them did, they're all dead, so return false.


for (int p = 0; p < m_nrobots; p++)
{
    if (m_robots[p] -> step())
        return true;
}
 return false;
}

Last edited by aznballerlee; Dec 2nd, 2006 at 1:11 PM.
aznballerlee is offline   Reply With Quote
Old Dec 2nd, 2006, 1:33 PM   #59
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Oops, number 2 should be changed to this:

grid[r][c] = m_robots[b] -> name()[0];

and a typo on number 3
cout << m_robots[c] -> name() << endl;
cout << m_robots[c] -> energy() << endl;
aznballerlee is offline   Reply With Quote
Old Dec 2nd, 2006, 4:33 PM   #60
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
I don't understand. Are you saying that we're testing the same robotToReturn each time? Does this mean I need to test the i'th element of the robotToReturn?
Trace through it in your head, or on paper. Check what value robotToReturn would have the first time round the loop. And yes, that is what you need to do.

1.
if ((otherRobotAt(Robot* rp)-> energy() == 0)
This won't compile - what is the bit in red for? Are you trying to do this without access to a compiler? If so, you are making it very hard for yourself.
It is also checking for the energy returned from the robot that otherRobotAt returns before seeing if there is a robot returned at all!
m_energy -= SHARE_AMOUNT;
otherRobotAt(Robot* rp)-> energy() = SHARE_AMOUNT;
m_energy is a member of Robot, but I think this code is in a Valley method, so you can't access m_energy directly.
You can't assign a value to an integer return value, you are probably going to have to add another Robot mutator. Maybe something like shareEnergy(Robot *with).
2. r and c may be defined in your function, but when that code is executed they don't have valid values. Even if they did you are putting the first letter of each robot on the same location over and over . The robot has a location, use it!
3. This looks right
4. You need to have a variable that indicates whether any robot made a step. Change the variable appropriately in the loop then return an appropriate value at the end.
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 12:42 AM.

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