Move a character in arrays

Right here I'm trying to create a game style map where the integer p represents my character. My issue right here is I tried various ways to make my character move on the map but I'm out of ideas as to how. So far I tried to test where by pressing w my character should go northward from its initial position but nothing happens. I tried using "if (Map[10][20] == p){row--;}" or "if (Map[10][20] == p){row -= row;}". My directional control will be using "wasd" key.

Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

bool gameover = false;

int main(){

	int p = 8; //player character
	string move;

	while (gameover == false){
	system("cls");
	int Map[15][20];

	cout << "---------------------" << endl;	
	for (int row = 0; row < 15; row++){
		cout << "|";							
		for (int col = 0; col < 20; col++){
			if (col == 5 && row == 4){
				Map[row][col] = p;
				if (move == "w"){                 //movement
					if (Map[10][20] == p){
						p -= row;
					}
				}
				cout << Map[row][col];
			}

		}
		cout << endl;
	}

	cout << "---------------------" << endl;	//Southern wall
	cin >> move;
	
}
	
	
	
	
	

	system("pause");
	return 0;
}
Well, move doesn't have a value when it is tested in line 25 - where were you intending to input it? Similarly, most of Map doesn't have any value, but it is still tested in line 26.

Just use
while (!gameover)
rather than
while (gameover == false)

Because of the map entity in the Standard Template Library, I would be hesitant about calling your array Map.
Best to separate these into methods. You would want an initializing method to give the array values, have a redraw or refresh methods that has your loops to clear the screen then draw the current state of the array to the screen. Then have a move method that takes in your move variable.


I tried using values this time but still not sure how to properly code it right, Ill keep trying tho. Thx guys.
Topic archived. No new replies allowed.