Map of console rpg

Started by
4 comments, last by mayoscuro 11 years, 5 months ago
hello,
I'm doing a small console rpg in c + +.
In this rpg just have to move a letter "p" by a matrix (two-dimensional) until you reach the letter "M".
The problem appears to generate the array. I think it's easy, the problem is that the ion Assignment I copied below (void generarmapa (game & board)) the array always takes the same value ("H") and all others are ignored.
Can someone explain why this happens?
Is there another way to assign these values???
Thank you very much ^ ^

sorry for the possible bad writing of the text, I do not speak much English, I do what I can, I will gradually improve smile.png


typedef std::vector< std::vector< int > > juego;

void generarmapa(juego &tablero){
for(int y=0;y<19;y++ ){
tablero[0][y] ='H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H';
}
for(int y=0;y<19;y++ ){
tablero[1][y] ='H','P','H','H','H','H','H',' ',' ',' ',' ',' ','O','H','H','H','H','H','H';
}
for(int y=0;y<19;y++ ){
tablero[2][y] ='H',' ','H',' ',' ',' ',' ',' ','H','H','H','H','H','H','H','H','H','H','H';
}
for(int y=0;y<19;y++ ){
tablero[3][y] ='H',' ','H',' ','H','H','H','E',' ','H','H','H',' ',' ',' ',' ','B',' ','M';
}
for(int y=0;y<19;y++ ){
tablero[4][y] ='H',' ','E',' ','H','H','H',' ','H','H','H','H',' ','H','H','H','H','H','H';
}
for(int y=0;y<19;y++ ){
tablero[5][y] ='H','H','H','H','H','H','H',' ','H','H','H','H',' ','H','H','H','H','H','H';
}
for(int y=0;y<19;y++ ){
tablero[6][y] ='H','H','H','H','H','H','H',' ','H','H','H','H',' ','H','H','H','H','H','H';
}
for(int y=0;y<19;y++ ){
tablero[7][y] ='H','H','H','H','H','H','H',' ',' ',' ','E',' ',' ','H','H','H','H','H','H';
}
for(int y=0;y<19;y++ ){
tablero[8][y] ='H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H';
}

}
juego dimensionar_vector(juego &tablero) {
int tamañovector=19;
tablero;
tablero.resize( tamañovector );

for(int i=0; i < 9; ++i) {
tablero.resize(tamañovector);

}
return tablero;
}
Advertisement
Because you are always assigning the same value. For example:

tablero[1][y] ='H','P','H','H','H','H','H',' ',' ',' ',' ',' ','O','H','H','H','H','H','H';

is equivalent to:

tablero[1][y] ='H'

For every y.
Google "comma operator". In such context comma stands as expression separator - you are assigning the first expression (always 'H') to your vector element and then executing 18 other expressions without any effect.

In C++0x you can assign to vector directly using array literal (curly braces):
tablero[1] = { 'H','P','H','H','H','H','H',' ',' ',' ',' ',' ','O','H','H','H','H','H','H' };
But if your compiler does not support C++0x you have to use some other way, for example:


static const char *c = "HPHHHHH OHHHHHH";
for (int y = 0; y < 19; y++) tablero[1][y] = c[y];


Usually it is better to store map in one-dimensional vector and calculate the index from row and column values. But in any case try to get it working at first and then experiment how to make it better.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/
You're only assigning the first value in each of your lists to tablero[...][y]. Each of those values just happens to be an 'H' so you're filling your entire set of vectors with 'H' and ignoring all the other values. You might want to look at putting all that information in a text file like:

HHHHHHHHHHHHHHHHHHH
HPHHHHH OHHHHHH
H H HHHHHHHHHHH
H H HHHE HHH B M
H E HHH HHHH HHHHHH
HHHHHHH HHHH HHHHHH
HHHHHHH HHHH HHHHHH
HHHHHHH E HHHHHH
HHHHHHHHHHHHHHHHHHH

And using file i/o to read them into a vector of chars (not ints as you were doing - though they'd work, it's wasted space). This will solve two problems:

First, you can't initialize with, or assign to, a std::vector an initializer list (which is what you sort of appear to be trying to do).
Second, you can have multiple maps without defining a new function for each map and these maps will be easier to read while you're making them.
Thanks ^ ^
Now I have another question ...
How I can detect the arrow keys without using the library <conio.h>?
Is there any way to use std :: KeyChar without pressing the enter key?

Thanks ^ ^
Now I have another question ...
How I can detect the arrow keys without using the library <conio.h>?
Is there any way to use std :: KeyChar without pressing the enter key?


What platform is this on? If you don't want to use console-ish IO then you'd need to use a native IO library like DirectInput, etc.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Is developed in windows console ¿Is that what your asking me?

This topic is closed to new replies.

Advertisement