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 Valley

isplay 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;
}