How do move a character

Hey
I am working on a assigment and i am stuck and need help.
i need to make 5x5 grid and than add chracter in it.I need to move it using u,d,l,r.

p * * * * User enter d --> * * * * *
* * * * * p * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *

i made this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int s = 5;
	char player = 'p';
	char choice;
	for (int i = 1; i <= s; i = i + 1){
		
		for (int j = 1; j <= s; j = j + 1){
			if (i == 1&&j==1){
				cout <<" "<<player;
			}
			else{

				cout << " *";
			} 
		}
		
		cout << endl;
		
	}


i am not able to move chracter.if i add a condtion in loop than it will not make whole grid and keep asking for choice after every row.
Can anyone point me in right direction?
Last edited on
Show your full relevant code if you want an exact solution to your issue.

1
2
			if (i == 1&&j==1){
				cout <<" "<<player;

Looks like you're hardcoding the player's position to be (1, 1). Keep track of the player's position in variables called "player_row" and "player_col" or something like that, and check
if (i == player_row && j == player_col) {... }
Last edited on
ascii art games benefit mightily from the *nonstandard but available on most* function "gotoxy" which sets the cursor position to where you want to be. You need 2 calls and 2 writes, one to replace the current player with a filler character and one to change the filler character to the player marker.

this is cleaner than trying to clear the screen and re-draw the text every single key press.

getch() is another great nonstandard function for this type of work, it can read the user's input without writing it on the screen.

I don't like suggesting nonstandard extensions but these are commonly available (caps/spelling vary a little) and very, very useful for the task at hand.
i havent studied getch function and i dont even understand by gotoxy is.
they are, as I said, a couple of nonstandard but common language extensions. I said what they do also:
gotoxy moves the cursor position so you can overwrite text on the screen with it, and getch lets you read 1 character but does not print it on the screen or require the enter key to be pressed.

if you want to play with them, you now know all you need to know about them --- you are armed with information and can use that to reach out with google for details and work it into your code from some examples that are sure to pop up if you are so inclined. And if not, that is fine too, its just a casual suggestion.


You have added wrong code such as for (inti=i; i<=s, i= I+1), this code you give not a proper result. Here you can add for(int i= i, i<= j; i= i+s)
For (int i=1; i<=s; i= i+1, i=i+s)
Having use this you can short out the problem.



int s = 5;
char player = 'p';
char choice;
for (int i = 1; i <= s; i = i + 1){

for (int j = 1; j <= s; j = j + 1){
if (i == 1&&j==1){
cout <<" "<<player;
}
else{

cout << " *";
}
}

cout << endl;

}

For more visit:- http://www.traininginlucknow.in/best-c-language-training-in-Lucknow.html
Topic archived. No new replies allowed.