Making a grid

Im trying to make a grid using Multidimensional arrays but im not sure how to set it up so that the character will move on the grid. How would that work can someone show me?

Here is my code:

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
#include <iostream>
#include <string>
#include <windows.h>
#define VK_W 0x57
#define VK_A 0x41
#define VK_S 0x53
#define VK_D 0x44

int up = 0;
int down = 0;
int left = 0;
int right = 0;

using namespace std;


int main()
{
    int grid[5][5] = {{1,2,3,4,5}, {1,2,3,4,5}};
    char character = '*';

    up = GetAsyncKeyState(VK_W);
        if(up)
        {
            cout << "Test" << endl;
        }

        cin.get();
}


The key presses dont work for some reason.
Bump
To make an analogy, I think what is getting confusing is combining the chess board with legal moves. Your grid is a 2d array, the position of the character is a 1d array of size 2.

But I think I understand what you want better now that I've looked at your code a little.

First, your grid isn't initialized correctly, I also made it a char array (maybe you want to label your grid later, so you can draw walls instead of numbers).
1
2
3
4
5
char grid[5][5] = {{'1','2','3','4','5'},
                   {'1','2','3','4','5'},
                   {'1','2','3','4','5'},
                   {'1','2','3','4','5'},
                   {'1','2','3','4','5'}};


Now you make your character's position, and to place him in the middle of your grid:

int position[2] = {2, 2};

To print out your game: (note that character is a char)

1
2
3
4
5
6
7
8
9
10
for (int i =0; i < 5; i ++){
	for (int j =0; j < 5; j++){
		if (i == position[0] && j == position[1])
			cout << character;
		else
			cout << grid[i][j];
		cout << " ";
	}
	cout << endl;
}


To move your character, you will adjust the values in the position array. I don't know anything about getting key strokes, but this looks okay:
1
2
3
4
5
// Before really moving, make sure that character can move in that direction.  Else you'll go out of bounds.
if(up) position[0] -= 1;
else if (right) position[1] +=1;
else if (down) position[0] += 1;
else if (left) position[1] -= 1;


I think that would be enough to at least move the character around. What is good here is that you never touch the grid array, so it's safe.

Again, that is about as far as I can go, I don't know a good way to update the console screen other than "cls" and redraw.

I guess I'll post what I made, a good break from homework :)

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
#include <iostream>
using namespace std;

int main(){
	char grid[5][5] = {{'1','2','3','4','5'},
	{'1','2','3','4','5'},
	{'1','2','3','4','5'},
	{'1','2','3','4','5'},
	{'1','2','3','4','5'}};
	char character = '*';

	int position[2] = {2,2};

	// Draw the grid once
	for (int i =0; i < 5; i ++){
		for (int j =0; j < 5; j++){
			if (i == position[0] && j == position[1])
				cout << character;
			else
				cout << grid[i][j];
			cout << " ";
		}
		cout << endl;
	}

	// Wait for input
	cout << "\n\nHit enter."; cin.get();

	// Move the character
	position[1] = 1;

	// Clear screen and redraw
	system("cls");
	for (int i =0; i < 5; i ++){
		for (int j =0; j < 5; j++){
			if (i == position[0] && j == position[1])
				cout << character;
			else
				cout << grid[i][j];
			cout << " ";
		}
		cout << endl;
	}


	cout << "\n\nDone."; cin.get();

	return 0;
}


Edit: I guess I didn't answer your question at all!

The only thing I think about this is to use a char:

1
2
3
4
5
char move;

cin.get(move); cin.ignore();
if (move == 'w') position[0] -= 1;
// else if other buttons 
Last edited on
Topic archived. No new replies allowed.