Create movement within a vector

Hi I am new to C++ and was wondering how to create movement of the "@" symbol within the vector. What I wanted the current code (shown below) to do is to insert the 3 symbols into the vector but judging on what I can see it seems to be simply adding it to the line and therefore extending the size of the vector. If there is a way of rectifying this that would also be much appreciated. Thanks for any help in advance.

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;

int main()
{
int i, j;
int RandomT = rand() % 120;
int RandomAT = rand() % 120;
int RandomX = rand() % 120;

vector <vector<char>> map(6, vector<char>(20, '.'));

for (i = 0; i < 6; i++) {
for (j = 0; j < 20; j++)
{
cout << map[i][j];
if (((i * 20) + j) == RandomT) {
map[i][j] = 'T';
cout << map[i][j];
}
cout << map[i][j];
if (((i * 20) + j) == RandomAT) {
map[i][j] = '@';
cout << map[i][j];
}
cout << map[i][j];
if (((i * 20) + j) == RandomX) {
map[i][j] = 'X';
cout << map[i][j];
}

cout << endl;

}
}
return 0;
}
What is your assignment you are attempting to solve?
It was just an additional in class task to try and create a board game with vectors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for (i = 0; i < 6; i++) {
for (j = 0; j < 20; j++)
{
cout << map[i][j];
if (((i * 20) + j) == RandomT) {
map[i][j] = 'T';
cout << map[i][j];
}
cout << map[i][j];
if (((i * 20) + j) == RandomAT) {
map[i][j] = '@';
cout << map[i][j];
}
cout << map[i][j];
if (((i * 20) + j) == RandomX) {
map[i][j] = 'X';
cout << map[i][j];
}

cout << endl;

}
}


Should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for (i = 0; i < 6; i++) {
for (j = 0; j < 20; j++)
{
if (((i * 20) + j) == RandomT) {
map[i][j] = 'T';
cout << map[i][j];
}
else
if (((i * 20) + j) == RandomAT) {
map[i][j] = '@';
cout << map[i][j];
}
else
if (((i * 20) + j) == RandomX) {
map[i][j] = 'X';
cout << map[i][j];
}
else cout << map[i][j];
}

cout << endl; // Note : This line has been moved
}
Last edited on
Ah you life saver, thanks so much. I knew it was something small that was tripping me up
Do you know the best way of going about putting the movement into the vectors?
Do you know the best way of going about putting the movement into the vectors?

What do you mean?
How you would approach adding movement within the vector. I was trying to get:

while(true)
if

else

to work but was not sure if it was the best way to go about or in fact if it would work at all?
What pieces do you want them to move? Should they move randomly after a certain interval?
The '@' symbol is supposed to represent the player and it should move based on what key the user enters.

s – move down
w – move up
d – move right
a – move left
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
using namespace std;

int main()
{
int i, j;
int RandomT = rand() % 120;
int RandomAT = rand() % 120;
int RandomX = rand() % 120;

vector <vector<char>> map(6, vector<char>(20, '.'));

int pX, pY;
int pX_2, pY_2;

system("cls");
for (i = 0; i < 6; i++) {
for (j = 0; j < 20; j++)
{
if (((i * 20) + j) == RandomT) {
map[i][j] = 'T';
cout << map[i][j];
}
else
if (((i * 20) + j) == RandomAT) {
map[i][j] = '@';
pX = j, pY = i;
cout << map[i][j];
}
else
if (((i * 20) + j) == RandomX) {
map[i][j] = 'X';
cout << map[i][j];
}
else cout << map[i][j];
}

cout << endl; // Note : This line has been moved
}


while(1)
{
    char choice; cout << "Your direction : "; cin >> choice;
    switch(choice)
    {
        case 's' : pX_2 = pX; pY_2 = pY + 1; break;
        case 'w' : pX_2 = pX; pY_2 = pY - 1; break;
        case 'd' : pX_2 = pX + 1; pY_2 = pY; break;
        case 'a' : pX_2 = pX - 1; pY_2 = pY; break;
        default : pX_2 = pX; pY_2 = pY; break;
    }
    if(pX_2 >= 0 && pX_2 <= 19 && pY_2 >= 0 && pY_2 <= 5 && map[pY_2][pX_2] == '.')
    {
        map[pY_2][pX_2] = '@';
        map[pY][pX] = '.';
        pX = pX_2; pY = pY_2;
    }

system("cls");
for (i = 0; i < 6; i++) {
for (j = 0; j < 20; j++)
{
    cout << map[i][j];
}

cout << endl; // Note : This line has been moved
}


}
return 0;
} 

Edit : Modified the code a bit so that the code can work well on Cpp.sh.
http://cpp.sh/3gc55
Last edited on
Yeah my visual studio is playing up a bit at the moment so I adjusted the code to make it work for C++ shell. Thanks so much.
Topic archived. No new replies allowed.