简体   繁体   中英

reversing coordinates in an array variable C++

I am not exactly sure how to phrase this question, sorry for the unhelpful title.

I have a large array (5 columns, 50 rows) that I am using to draw out a level environment in ascii text (each entry in the array is a single character and they are all printed out to make an image) ie:

  char worldarr[5][9] = 
{
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, //the length of these entries are actually
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, //50 but I shortened them for the post
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
}

where the output is:

XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX

Now I am trying to set up a coordinate system, where (0,0) is the bottom left hand corner, the goal is to be able to put in a coordinate and have that location replaced with an '@' my original assumption was that I could use this:

int x = 0, y = 0;
worldarr[y][x] = '@'; 

but this outputs:

@XXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX

not:

XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
@XXXXXXXX

how could I make a new system to force (0,0) to be at the bottom, or better yet, the exact center of the array (if possible).

sorry again for the terrible conciseness, I am not sure what I am working with.

Instead of trying to completely remap your array, you could instead wrap the array in a class.

Something like:

class World {
private:
    static const size_t COLS = 5;
    static const size_t ROWS = 9;
    char worldarr[COLS][ROWS] = 
    {
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, //the length of these entries are actually
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, //50 but I shortened them for the post
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
    };
public:
    char* operator[](int indx) {
        return worldarr[COLS - (indx + 1)];
    }
};

Then you could use it like:

World w;
w[0][0] = '@';

See a live example here: https://ideone.com/f6xgBb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM