2-Dimensional array grid based simple text based rpg, noob

Hey guys,

I have been reading the forums quite a lot lately, however I have finally decided to join when i got stuck on a very simple problem :(

This is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>

using namespace std;

int main()
{
    char adventuregrid[5][5];
    int t, i;

    for( t=0; t < 5; t++) {
    for( i=0; i < 5; i++) {
    adventuregrid[t][i] = 'O' ;
    cout<< adventuregrid[t][i] << ' ' ;
    }
    cout<< endl;
    }

}



Basically, what I am trying to do is a simple text based rpg where the player can select whether he wants to go North, East, South or West.

I would like the grid pattern to look something like this:

0 0 0 0 0
0 0 0 0 0
0 X 0 0 0
0 0 0 0 0
0 0 0 0 0

Where the X marks the players current position. So far, I have been able to get the grid of zeroes to show but, I am not sure how to show the X and then update it after user input.

Thanks you, in advance.

Any help is welcome :)

Also please say if you think I am getting too ahead of myself, this seems like a simple task to set myself. Also, I am currently learning from 'C++ A beginner's guide' by Herbert Schildt. Has anyone hear of it?
Last edited on
Sorry to be rude but, *bump*

Any help would be appreciated :)
well, first of all, your main() is doing too much

have you read about functions yet? I would refactor your code to look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

static char adventuregrid[5][5];

void initGrid() {
// you fill in here
}

void printGrid() {
// you fill in here
}

main() {
  initGrid();
  adventuregrid[2][1] = 'X';     // place me at 3 rows down, 2 rows to the right
  printGrid();
}


when you get more advanced, I recommend that you put adventuregrid and any other important information in a struct or class and pass an instance of that around
You should make a variable for x and y and check if the map position matches them. Also you should use adventuregrid[i][t] instead of [t][i].
Ok, thankyou very much for replying.

I have only touched on simple functions in the second chapter of the book however, later on there is a big chapter on taking these to a more advanced level.

This has answered my question perfectly, it is a great help.

I know that I am trying to do this project a bit early but, I find it best to learn as you go along sort of thing, while referring to this forum or my book when needed. However, after I get this project finished I will read on in the book.
OK Breadman, I will give that a go.

Will be back, to tell you how your advice has gone and hopefully I wont get stuck again ;)

Thanks all so far for the advice! I am starting to get into programming more and more now
Also, sorry to be rude here, but what do I input when you type:

1
2
3

// you fill in here


Thankyou very much for your time
on Line 8, you write the code which initializes the grid - why the function is called initGrid()

on Line 12, you write the code which prints the grid

your original code indicates that you should be able to do both
Im not really getting it.

I know I am meant to bring the code but, would you able to quickly be able to write me a short program which displays a grid of circles but, one of the circles is replaced by an 'X' that is placed there by an x and y variable than is determined by the user?

I know this is a lot to ask but, this would help me hugely in understanding how arrays work and it will generally improve my understanding of the whole programming language.

Thankyou, very much so far for your help.
*bump* please
?!?!?

if you fill out Lines 8 and 12 the code above will do exactly what you are asking for!

all you have to do grab x,y from cin if you want the user to enter them.

everything is already there in this thread... ...so if you still cannot figure it out, you need to read more so you can actually understand better what you are doing.
Ok, I understand that.

But, with what am I meant to fill it out with :(

Sorry I know I am a noob.
Topic archived. No new replies allowed.