View Single Post
Old Dec 3rd, 2006, 10:36 AM   #73
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
1. I fixed up the switch statement, I think this would be more logical.
        //    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.
        switch (m_dir)
        {
        case NORTH:
            // TODO: Move one step north, if possible.

            if (m_row-- < 0  ||  m_row-- >= NROWS)
            {
                break;      // do nothing don't move
            }
            m_row--;       // otherwise, decrement row coordinate
            break;
            
// TODO: Implement the other movements
        case SOUTH:
            if (m_row++ < 0  ||  m_row++ >= NROWS)
            {
                break;
            }
            m_row++;
            break;

        case WEST:
            if (m_col-- < 0  ||  m_col-- >= NCOLS)
            {
                break;
            }
            m_col--;
            break;

        case EAST:
            if (m_col++ < 0  ||  m_col++ >= NCOLS)
            {
                break;
            }
            m_col++
                break;
        
        default:
            cout << " ***** Direction must be valid" << endl;
        }        
    }

2. I changed my destructor for valley too. I realizede that I hadn't deleted each element inside the array I allocated. So I fixed that.

//      TODO:  Implement the destructor
//      Delete any dynamically allocated objects held by the Valley.
Valley::~Valley()
{
    for (int f = 0; f < m_nrobots; f++)
        delete [] m_robots[f];

    for (int g = 0; g < m_nsources; g++)
        delete [] m_sources[g];
}

3. Oops, forgot that. How's this now.
	Robot* otherRobot = m_valley->otherRobotAt(this);

	if (otherRobot != NULL)
	{
		if ((otherRobot -> m_energy == 0) && (m_energy > SHARE_THRESHHOLD))
		{
			m_energy -= SHARE_AMOUNT;
			otherRobot-> m_energy = SHARE_AMOUNT;
			return true;
		}
	}
	return true;
}
aznballerlee is offline   Reply With Quote