Hey guys I need some help on this code.

I'll just post the parts of the code needed to do thing I'm stuck on.

``````class Robot
{
public:
// Constructor
Robot(Arena* ap, int r, int c);

// Accessors
int  row() const;
int  col() const;

// Mutators
void move();

private:
Arena* m_arena;
int    m_row;
int    m_col;
};

class Arena
{
public:
// Constructor/destructor
Arena(int nRows, int nCols);
~Arena();

// Accessors
int 	rows() const;
int 	cols() const;
Player* player() const;
int 	robotCount() const;
int 	nRobotsAt(int r, int c) const;
void	display(string msg) const;

// Mutators
bool   destroyRobot(int r, int c);
bool   moveRobots();

private:
int 	m_rows;
int 	m_cols;
Player* m_player;
Robot*	m_robots[MAXROBOTS];
int 	m_nRobots;
};``````

This is what I have to do:
Indicate each robot's position, if one robot is at some grid point, set the char to 'R', if there are two to eight, set it to '2' through '8', set it to 9 if its 9 or more.
This is all I have so far..

``````for (int i = 0; i < m_nRobots; i++)
{
int r = m_player[i].row();
int c = m_player[i].col();
grid[r][c] = 'R';
}``````

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Lerner

It looks like the location and the robots are disconnected, so the first algorithm that comes to mind is a simple one:

``````for each robot from robots
integer r := robot.row()
integer c := robot.col()

select grid[r][c] from
case 'R': grid[r][c] := '2'
case '2': grid[r][c] := '3'
case '3': grid[r][c] := '4'
case '4': grid[r][c] := '5'
case '5': grid[r][c] := '6'
case '6': grid[r][c] := '7'
case '7': grid[r][c] := '8'
case '8': grid[r][c] := '9'
case '9': # do nothing
default: grid[r][c] = 'R'
end select
loop``````

That should give you a start.

Could you please look over my code to see if it's correct?

``````void Arena::display(string msg) const
{
char grid[MAXROWS][MAXCOLS];
int r, c;

// Fill the grid with dots
for (r = 0; r < rows(); r++)
for (c = 0; c < cols(); c++)
grid[r][c] = '.';

// Indicate each robot's position
// TODO:  If one robot is at some grid point, set the char to 'R'.
//		  If it's 2 though 8, set it to '2' through '8'.
//		  For 9 or more, set it to '9'.
for (int i = 0; i < m_nRobots; i++)
{
if ( (m_robots[i]->row() == '.') && (m_robots[i]->col() == '.') )
{
r = m_robots[i]->row();
c = m_robots[i]->col();
}

switch ( nRobotsAt(r, c) )
{
case '1':
grid[r][c] = 'R';
case '2':
grid[r][c] = '2';
case '3':
grid[r][c] = '3';
case '4':
grid[r][c] = '4';
case '5':
grid[r][c] = '5';
case '6':
grid[r][c] = '6';
case '7':
grid[r][c] = '7';
case '8':
grid[r][c] = '8';
default:
grid[r][c] = '9';
}
}

// Indicate player's position
if (m_player != NULL)
{
// TODO:  Set the char to '@', unless there's also a robot there,
//		  in which case set it to '*'.
if ( (m_robots[i]->row() != m_player->row()) && (m_robots[i]->col() != m_player->col()) )
{
m_player->row() = 'r';
m_player->col() = 'c';
grid[r][c] = '@';
}
else
{
m_player->row() = 'r';
m_player->col() = 'c';
grid[r][c] = '*';
}
}
}``````

Here is my nRobotsAt function:

``````int Arena::nRobotsAt(int r, int c) const
{
// TODO:  return the number of robots at row r, column c
int count = 0;
for (int k = 1; k < m_nRobots; k++)
{
if ( (m_robots[k]->row() == r) && (m_robots[k]->col() == c) )
{
count ++;
}
}
return count;
}``````

Does your code compile? Does it run as you expect it to for both routine and predictable special cases, if there are any? If so then the code is probably correct. If not, then isolate the part(s) that are causing the problem and try to get it to compile, run correctly, or whatever. Then if you can't figure it out, post the least amount of code to indicate the problem and whatever compiler/linker errors you get or whatever incorrect outcome you experience.