Ok so for my text pet game im making i want there to be a small map so that the pet can move around but i honestly done even know how or where to begin with the map, im making a map generator function but like i said im not sure how to draw the map on the screen and put the pet on it and have them move around. The pet will not be player controlled so theres no need for any of that.
1 2 3 4 5 6 7 8 9
void MapGenerator()
{
char pet = '*';
int petXCoord = 0;
int petYCoord = 0;
int gameMap[10][10];
for()
}
Thats all i have so far, how should i draw the map?
Have you ever looped through a multidimensional array?
Edit: That was suppose to be more than just the question..
If not, you would start by making a for loop that will loop through your rows, but in that for statement have another that goes through the columns. So it will start at row 1 (technically 0) and then go through each column, and then move on until there are no more rows to go through.
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
int main()
{
char pet = '*';
int petXCoord = 0;
int petYCoord = 0;
char gameMap[10][10];
string space = "O";
string other = "Q";
string border = "|";
string bottomBorder = "_";
for(int i = 0; i < 5; i++)
{
cout << other;
for(int j = 0; j < 5; j++)
{
gameMap[i][j] = '0';
}
}
}
That doesnt seem to work for me, i know im doing it wrong, and to answer your question i have never looped through a multidemensional array before, i dont even use arrays, i use vectors for pretty much everything. But i need to learn them so now is better than ever.
The beauty and curse about programming is there are multiple ways to solve the same answer. What I'm noticing is that you are using cout to produce the border when you should actually be using the array. So here is how I look at multidimensional array looping:
1 2 3 4 5 6 7
for(int i = 0; i < rowsToLoop; i++) // How many rows to loop through.
{
for(int j = 0; j < columnsToLoop; j++) // How many columns to loop through.
{
// This is (usually) where most of your actions will take place.
}
}
So, let's look at your objective as of right now. To make a perimeter that is 10 spaces wide and 10 spaces tall (We get this size because of your array). We then need to then check to see if we are at the first or last row of the map. If we are, we simply need to fill in the element with one of your borders. if not, then we need to check to see if we are on the first column or the last column, if we are, put in a border, if not, put in a space.
Edit: People can get into more detail if they want, but in a general sense.. An array is like a vector except it has less dynamic properties i.e. it will have a fixed amount of elements in it. Where as a vector (as you know) you can push_back into the vector to add an element.
int main()
{
char pet = '*';
int petXCoord = 0;
int petYCoord = 0;
int rowsToLoop = 10;
int columnsToLoop = 10;
char gameMap[10][10];
char space = '0';
for(int i = 0; i < rowsToLoop; i++) // How many rows to loop through.
{
for(int j = 0; j < columnsToLoop; j++) // How many columns to loop through.
{
cout << gameMap[rowsToLoop][columnsToLoop];
}
}
}
what else do i need to do? i think i need to output the grid, im not going to use a grid, but i would like one for debugging so i know that its actually outputting correctly.
Sort of, except in gameMap[rowsToLoop][columnsToLoop] that should be gameMap[i][j]. The rowsToLoop and columnsToLoop is the amount to loop through. So what you wrote there would keep outputting whatever was in gameMap[10][10] 100 times (10 rows with 10 columns -> 10 * 10 = 100 elements).
Also, you still have to insert some type of value into the elements. So add in
gameMap[i][j] = "X";
to the for loop that goes through the columns and see what happens. You're getting there!
Edit: I forgot to explain that you choose to use gameMap[i][j] because i gives you the current row you are on and j gives you the current column you are working with.
Because of how you are using endl. Remember, anything you do in the inner for loop is done to every column.
So it starts at row 0 and is then told to go through every column in row 0 AND while it's going through all the columns, insert x into the corresponding element.
So! You actually need to use endl at the end of each row (In the first for loop after the inner for loop).
for(int i = 0; i < rowsToLoop; i++) // How many rows to loop through.
{
cout << endl;
for(int j = 0; j < columnsToLoop; j++) // How many columns to loop through.
{
gameMap[i][j] = 'x';
cout << gameMap[i][j];
}
}
that works they are all in a column but theres a gap at the top why is that? theres also another problem, when i change the amount of rows and columns to 30, the x,s dont output right and the program crashes, it looks something like this
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxx
xx
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
int main()
{
char pet = '*';
int petXCoord = 0;
int petYCoord = 0;
char gameMap[10][10];
char space = '0';
for(int i = 0; i < 20; i++) // How many rows to loop through.
{
cout << endl;
for(int j = 0; j < 20; j++) // How many columns to loop through.
{
gameMap[i][j] = 'x';
cout << gameMap[i][j];
}
}
}
Ah that's because when you put in 20 you are trying to go out of scope of the array. The array only has 100 elements, 10 rows and 10 columns. So when i or j is anything above 10 it is going to say that you are trying to modify the value inside an element that doesn't exist. i.e. gameMap[11][11].
We can solve this issue a few ways. The simplest way for you to do it without confusing you would be to make two ints:
1 2
int maxRows = 10;
int maxColumns = 10;
then you could change your initiliation of gameMap to gameMap[maxRows][maxColumns]. Then! in your for loops instead of using a number you can use maxRows and maxColumns respectively.
There is another, more efficient way, but it might be a little more confusing.
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
int main()
{
char pet = '*';
int petXCoord = 0;
int petYCoord = 0;
char gameMap[20][40];
char space = '0';
for(int i = 0; i < 20; i++) // How many rows to loop through.
{
for(int j = 0; j < 40; j++) // How many columns to loop through.
{
gameMap[i][j] = ' ';
cout << gameMap[i][j];
}
cout << endl;
}
gameMap[1][1] = pet;
}
Okay, so basically you are going to have to make a few functions. The first is a function to create the map. Let's call it createMap. The next would be a function to print the map like printMap. Then you'll have to start making functions for the various things you want your pet to do.
Well, besides the fact that you now need to make functions to interact with the pet, you start using your functions. Note, you will need to have a function that generates the map and another function to print the map. Don't have the two be the same function, it won't work.
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
void PetAI();
void MainGame();
void MapGenerator();
int main()
{
MainGame();
}
void MapGenerator()
{
char pet = '*';
int petXCoord = 0;
int petYCoord = 0;
char gameMap[20][40];
for(int i = 0; i < 20; i++) // How many rows to loop through.
{
for(int j = 0; j < 40; j++) // How many columns to loop through.
{
gameMap[i][j] = ' '; //create a space in each element
cout << gameMap[i][j];
}
cout << endl;
}
}
void MainGame()
{
MapGenerator();
}
void PetAI()
{
}