Using array for x and y coordinates?
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Using array for x and y coordinates?

1. Junior Member
Join Date
Sep 2010
Posts
9

## Using array for x and y coordinates?

Hey guys, I have recently been trying to redo a game I made a while back to make the code more efficient and readable, so that people looking at the source actually know what their looking at. So, I'm redoing a few parts that I did in very inefficient, bulky ways, and replacing those methods with newer compact ones.

Now, thats been working just great, until I tried to replace the way I used to do x and y coordinates with an array. The way the game used to work was that when the user typed in a command like "north" or "west", the program would add 1 to the y and subtract 1 from the x, respectively. Now, to reduce possible bugs with using the x and y directly, I assigned a number to each spot, called z, with a very long, complicated mess of code that looked like this:
Code:
```int main ()
{
//blah blah blah
if ( x == 1 && y == 5 )
z = 1;
if ( x == 2 && y == 5 )
z = 2;
if ( x == 3 && y == 5 )
z = 3;
//blah blah blah
}```
This ended up making the grid look like this

5|1 2 3 4 5
4|6 7 8 9 10
3|11 12 13 14 15
2|16 17 18 19 20
1|21 22 23 24 25
0 1 2 3 4 5

Now because of how unbelievably horribly long this was, I ended up just replacing all that code with an equation:
Code:
`z = ( ( 5 - y ) * 5 ) + x;`
This works just fine, however, I do want to just get rid of the x, y, and z all together and replace it with a 2-dimensional array. Since I don't have much experience with arrays, however, I need a bit of help.

What I want the array to do is have each of the 25 squares numbered using a slightly modified version of the above equation (to compensate for the fact that it goes from 0-4 instead of 1-5). But then, I also want to have some kind of way to be able to store the location of the player inside of one of the squares in the array and then have the program be able to compare the location of the player in an if statement, in something like this:

Code:
```If ( array[x][y] == near_the_tv && strcmp (input, "watch tv" ) == 0 )
cout<<"You watch TV. There is nothing good to watch.\n\n";```
Any ideas? Its not really mandatory (since the way I have works, I guess) but it would help my understanding of arrays and make everything look nicer.

2. Member +
Join Date
Aug 2008
Posts
902

## Re: Using array for x and y coordinates?

Well, you posted this in Visual C++ Programming, so I assume you are learning C++, not C, so you should consider redesigning your RPG with C++ in mind.

For instance, instead of positions being a loose x variable and y variable, have a position object.

example:
Code:
```class position
{
public:
position(int x = 0, int y = 0)
: pos_x(x), pos_y(y)
{}

inline int& x() { return pos_x; }
inline const int& x() const { return pos_x; }
inline int& y() { return pos_y; }
inline const int& y() const { return pos_y; }

bool operator==(const position& rhs) const
{
return (pos_x == rhs.pos_x && pos_y == rhs.pos_y);
}
bool operator!=(const position& rhs) const
{
return !(*this == rhs);
}
bool operator<(const position& rhs) const
{
return (pos_x * pos_x + pos_y * pos_y) <
(rhs.pos_x * rhs.pos_x + rhs.pos_y * rhs.pos_y);
//impliment less than so this class works with std::map
}

private:
int pos_x, pos_y;
};```
Instead of using arrays, use std::vector instead. You could determine where you are with something like:

Code:
```std::map<position, std::string> game_map;
game_map[position(0, 0)] = "Commons";

...

position player_location(0, 0);
//where are we?
cout << "You are at " << game_map[player_location] << ".\n";
//prints: "You are at Commons."```
This isn't perfect as is, but you hopefully get the idea. You could also create a "player" class to hold things like current position, name, race, etc.

This eliminates all that unsightly if-then-else code you have and makes changing your map a trivial thing, instead of requiring major mucking about (pun intended) in your programs logic.

3. Junior Member
Join Date
Sep 2010
Posts
9

## Re: Using array for x and y coordinates?

Thanks for the relatively speedy reply! I'm pretty new to programming so forgive my lack of skills :P.

Do you have any links to reading material on objects and std::map I could read? I havent really used classes at all and I havent heard of std::map before...

4. Member +
Join Date
Aug 2008
Posts
902

## Re: Using array for x and y coordinates?

This site is probably the best resource.

http://www.cplusplus.com/reference/stl/map/
http://www.cplusplus.com/reference/stl/vector/

It is pretty technical, since it's merely a reference, but there are some examples of it's use as well.

Writing your own classes may be a little difficult at first, but using classes like map and vector is easy, so you might as well start getting your feet wet with them now.
Last edited by Chris_F; December 5th, 2010 at 11:10 PM. Reason: typo

5. Junior Member
Join Date
Sep 2010
Posts
9

## Re: Using array for x and y coordinates?

Thank you so much! I'll post a new thread if I find any more problems, but that was the main one, so thanks!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•