![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#22 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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 |
|
|
|
|
|
#23 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#24 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#25 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#26 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#27 | |||
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
Quote:
if (energySourceAt(r,c))
m_energy = FULL_ENERGY;Quote:
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:
if (m_sources[MAXSOURCES] =EnergySource(m_row, m_col)) ![]() Last edited by aznballerlee; Nov 30th, 2006 at 10:46 PM. |
|||
|
|
|
|
|
#28 | ||
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
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. The code you have at the moment just creates an EnergySource object inside the test of an if statement. Quote:
- 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. |
||
|
|
|
|
|
#29 | |||
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
Quote:
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 (int k = 0; k < MAXSOURCES; k++_
{
if ( array element has energy source )
// return true;
else
return false;
}Earlier, you said this: Quote:
Last edited by aznballerlee; Nov 30th, 2006 at 11:05 PM. |
|||
|
|
|
|
|
#30 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
![]() |
| 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 |