• ### What is your GameDev Story?

Public Group

#### Archived

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

# moving characters in an array

This topic is 5401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've tried a few different ways to move a character in an array, but now I'm not sure it's possible. For example: X X X X X P O X X O O X X X X X how can I make character P move to the right, replacing the O, and let there be an O where P was? so like: X X X X X O P X X O O X X X X X i've tried incrementing the elements of the array, but it says i need an l-value, and incrementing like: map[1][1] += 2 but it increments the letter to R. is moving the character only possible with position.x, position.y methods, or i guess in other words coordinate methods? not sure, I'm still learning C++ so.... [edited by - TheOne1 on April 10, 2004 5:01:34 PM]

##### Share on other sites
Did you try swapping the elements instead? Like:
std::swap(map[1][1], map[1][2]);

##### Share on other sites
oh, I never knew about that function. What header file do I have to include?

thanks.

##### Share on other sites
It should be in <algorithm>.

##### Share on other sites
Swapping is probably the best method, as stated above by SiCrane. If you really want to use addition on characters, though, you could try finding the difference between the two target characters and either adding/subtracting a value from the two to be switched, like this:

int difference = 0;
char A = map[x1][y1], B = map[x2][y2];
difference = (A > B) ? (A-B) : (B-A);

if(difference != 0){
if(A < B){
map[x1][y1] += difference;
map[x2][y2] -= difference;
}else{
map[x1][y1] -= difference;
map[x2][y2] += difference;
}
}

As compared to a single swap, which would be on the order of three lines of code:

char temp = map[x1][y1];
map[x1][y1] = map[x2][y2];
map[x2][y2] = temp;

EDIT: fixed code to be more clear

[edited by - MajorShredd on April 10, 2004 5:28:40 PM]

##### Share on other sites
( for swap:: )
hmmm, it does work, but the O where the P was disappears. What I'm trying to do, but haven't implemented yet, was to ask the user where he wanted to go ( n, s, w, e ) and for example if south was chosen, then the character would be passed to some function that decrements or increments the character P down or up or whatever direction the user chooses.

[edited by - TheOne1 on April 10, 2004 5:22:30 PM]

[edited by - TheOne1 on April 10, 2004 5:22:54 PM]

##### Share on other sites
Im new to C++ my self 2 weeks now hehe. But what I have been doing is I have my double array like int map[10[10]

when I build my map i use 1 for wall 0 for sapce. I run it threw a nested forloop and print it to the console.

then i get the input from the player. I check for colision and if everything is ago, I go to old location and print a space and go to new location and print out the char. its really easy and works well. I got a nice lil library from my school that makes it really easy to go to console locations. If ya want to take a look at it send me an ICQ 50220171. all i got to do is con.Goto( y, x, ) and im there its very nice.

##### Share on other sites
Consider if it''s possible, instead of having one of your Rooms "contain" the Player, to just have the Player keep track of which Room it is in.

Re getting swap to work properly (i.e. leave O behind where P used to be - if I''m understanding you, you end up with nothing there? Or is there actually a problem?)- what is the type of the things you are storing in map[][]? Does it have a copy constructor?

##### Share on other sites
quote:
Original post by BSMonkey
Im new to C++ my self 2 weeks now hehe. But what I have been doing is I have my double array like int map[10[10]

when I build my map i use 1 for wall 0 for sapce. I run it threw a nested forloop and print it to the console.

then i get the input from the player. I check for colision and if everything is ago, I go to old location and print a space and go to new location and print out the char. its really easy and works well. I got a nice lil library from my school that makes it really easy to go to console locations. If ya want to take a look at it send me an ICQ 50220171. all i got to do is con.Goto( y, x, ) and im there its very nice.

for the parameters for con.Goto, what goes into y and x? The array subscript numbers? I don''t have icq, but if you could send that library to me at linkedlist@comcast.net I would appreciate it.

##### Share on other sites
Well when you set up your console window, you can set it for say 100 rows and 100 collums. then when you use con.Goto( y, x ) y is what row you want to go to and x is what collum. it then moves your currser there and you can print out your character or a space or anything else ya want.

and if ya want to be lazzy and just clear and reprint everytime in your game loop witch works well but not as great you will need to use
con.CursorOn( false );
that makes it so you cant see the cursor so you dont have flashing lines going across your screen.

the library has alot of other stuff in it too. It can do just about everything cin and cout can do. the only thing it cant do is it doesnt work with string class not 100% sure but I dont think it does.

Its multiple files tho so I cant email it to you, you got anything else? MSN AIM YAHOO?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 11
• 15
• 11
• 12
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015839
×