![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#51 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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;
} |
|
|
|
|
|
#52 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
Quote:
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 |
|
|
|
|
|
|
#53 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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;
} |
|
|
|
|
|
#54 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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;
} |
|
|
|
|
|
#55 | |||
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
I'm onto my next task, and I'm stuck. This is in the Robot::step function.
Quote:
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:
Quote:
|
|||
|
|
|
|
|
#56 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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 name3. 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;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;
} |
|
|
|
|
|
#57 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
Quote:
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 - 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. |
|
|
|
|
|
|
#58 | ||
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
Quote:
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:
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;
}Last edited by aznballerlee; Dec 2nd, 2006 at 1:11 PM. |
||
|
|
|
|
|
#59 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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; |
|
|
|
|
|
#60 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
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. if ((otherRobotAt(Robot* rp)-> energy() == 0) 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; 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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |