+ 2
Making map ( matrix ) for different levels ( using classes )
I wanted to know how you can make a different map for each level with different size for example: myClass level1; level1.map[height,width] = { "###" , "###" }; ( its not the correct code but i want something like this ) I wanted to know how I can pass different matrixes to class to make level's map ( and from map i mean map of the game not map of c++ )
7 Answers
+ 2
You will not be able to get the [] operator work like that but you can do this at maximum :
#include<iostream>
using namespace std;
class Level
{
int sx,sy; //Size
public:
char** MapData;
// 2D variable pointer array to
// hold the map
Level()
{
MapData = new char*[1000];
for(int i=0;i<1000;i++)
MapData[i] = new char[1000];
}
char**& Map(int ht, int wd)
// Resizes the map to our needs,
// and allows us to assign it data
// as a normal array in main.
{
MapData = new char*[ht];
for(int i=0;i<ht;i++)
MapData[i] = new char[wd+1];
sx = ht; sy = wd;
return MapData;
}
};
int main()
{
Level l1;
char* mymap[] = {"####",
"####",
"####",
"####"};
// mymap is a temp object to hold data.
l1.Map(4,4) = mymap;
// Initializes Map with Size and Data.
for(int i=0;i<4;i++)
{
cout<<l1.MapData[i]<<endl;
}
}
+ 4
MapData is a pointer to a pointer to char, which in English means a 2D array/matrix that we need.
Normal arrays, require their size completely specified at compile time, so if you want a feature to declare variable sized maps in main, you need to use pointers, which can get resized by reallocation.
Now, the function Map() accepts a size coordinate and returns a reference to the 2d array created with that size. This is done, so that the user can assign the array data using the function itself. Basically, the 'type&' syntax allows the function's returned object to be used as an lvalue / value that has a fixed address in memory, and thus, we are able to do:
Map(4,4) = data;
+ 3
Thanks for the answer but it's a bit confusing for me (pointers)
I would be happy if you explain the code especially this part :
MapData = new char*[1000]; , char**& Map(int ht, int wd)
+ 3
Hey, sorry for asking about simple things a lot, but
I want to move my character on the map
I tried what I did on 2d arrays without pointers and I Tested everything I knew but I got nothing.
this is my code see what's the problem: https://code.sololearn.com/c9HSS9AKSozg/#cpp
( don't gives me compile error but when I press arrow keys it crashes)
+ 3
in your example you should say :
Move(0, 1);
yes it should do this but it gives me the error:
Unhandled exception at 0x00C3451B in main.cpp.exe: 0xC0000005: Access violation writing location 0x00C3DA5D.
x and y are set in lines 65 & 66, and I tried what you said either
but it still doesn't work
+ 2
Can you explain the Move function?
For the map :
####
#@ #
# #
####
Move(1,0) should do this:
####
# @#
# #
####
Right?
And yes, initially, your x and y have no values, and so they fail in retrieving an element.
Add this to the constructor:
y = 0; x = 0; // If character starts from 0,0.
+ 1
I don't understand. The position to access the data is valid, the array has memory defined for that position, and even I can print MapData[y][x]; But I don't understand why we are unable to assign it data.
I tried the program in CppDroid, but the same error occurs.