Archived

This topic is now archived and is closed to further replies.

jar

i'm confused...

Recommended Posts

OK, i have a problem that i can''t seem to fix. in my program, i''d like for the user to move a face around a grid. well, here''s the code... #include <iostream> #include <windows.h> #include <conio.h> #include <cstdlib> using namespace std; const int ROWS = 10; const int COLS = 10; struct POSITION { int pos_x; int pos_y; char face; }; int main() { char grid[ROWS][COLS]; for(int i=0;i[j]='' ''; } } POSITION OBJECT; OBJECT.face = 1; OBJECT.pos_x = 4; OBJECT.pos_y = 4; bool done = false; while(!done) { grid[OBJECT.pos_x][OBJECT.pos_y]=OBJECT.face; for(int x=0;x= 9) OBJECT.pos_y=9; break; case ''s'' : ++OBJECT.pos_x; if(OBJECT.pos_x >= 9) OBJECT.pos_x=9; break; case ''w'' : --OBJECT.pos_x; if(OBJECT.pos_x <= 0) OBJECT.pos_x=0; break; case ''q'' : done = true; break; } system("cls"); } // end of while return(0); } The problem is that when the for loop draws the grid, it draws everything due to this line "grid[OBJECT.pos_x][OBJECT.pos_y]=OBJECT.face;". how can i just move the object around without actually assigning the position to the grid? thanks!

Share this post


Link to post
Share on other sites
Either you need to clear the grid every time. But my solution is to have the player as a stand-alone, like this

exchange:

  
grid[OBJECT.pos_x][OBJECT.pos_y]=OBJECT.face;

for(i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
cout << grid[i][j];
}
cout << endl;
}

with:

  
for(i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
if (i == OBJECT.pos_x &&
j == OBJECT.pos_y)
cout << OBJECT.face;
else
cout << grid[i][j];
}
cout << endl;
}

and instead of initially clearing the grid you can just:
char grid[ROWS][COLS] = {'' ''};

and (preferably) don''t name objects uppercase (OBJECT), they are more commonly used as defines, structs and classes... not the actual objects...

something like:
POSITION player;

would be suited

/G

/G

Share this post


Link to post
Share on other sites