game C++

May someone help me with the levels. I don't know how to make and turn different levels
#include<iostream>
#include<windows.h>
using namespace std;
int ok = 1;
bool state;



int main(){
char Map[10][10]={
"#########",
"#@ #",
"# #",
"# #",
"# sd #",
"# < #",
"# #",
"# #",
"#########"
};

int x = 1, y =1;
while(state != true){



system("cls");
for(int k = 0; k < 10; k++){
cout<<Map[k]<<endl;

}
system("pause>nul");
if(GetAsyncKeyState(VK_DOWN)){
int y1 = y + 1;
if(Map[y1][x] == ' '){
Map[y][x] = ' ';
y++;
Map[y][x] = '@';
}
}

if(GetAsyncKeyState(VK_UP)){
int y1 = y - 1;
if(Map[y1][x] == ' '){
Map[y][x] = ' ';
y--;
Map[y][x] = '@';
}
}

if(GetAsyncKeyState(VK_RIGHT)){
int x1 = x + 1;
if(Map[y][x1] == ' '){
Map[y][x] = ' ';
x++;
Map[y][x] = '@';
}
}

if(GetAsyncKeyState(VK_LEFT)){
int x1 = x - 1;
if(Map[y][x1] == ' '){
Map[y][x] = ' ';
x--;
Map[y][x] = '@';
}
}
if(GetAsyncKeyState(VK_LEFT)){
int x1 = x - 1;
if(Map[y][x1] == '<'){
ok++;
Map[y][x] = ' ';
x--;
Map[y][x] = '@';
}
}

}
system("pause");
return 0;
}
Please edit your post and use code tags, to make it readable - http://www.cplusplus.com/articles/jEywvCM9/
what are you trying to accomplish isnt trivial, thats a lot of generalization to count. i've developed a class for you, which creates a 'map' object, which you can use to create a vector (see <vector> library) of those 'map' objects

this class constructor takes arguments the length of row and colums and the location of the text file which you want to extract your map

it has a () overloaded operator that means you can use the map type as a 2d array but instead of [] brackets you use () like this myMap(1,1) = 'T' .

it also has a print function which prints the map on screen
myMap.print();

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<windows.h>
#include<fstream>
using namespace std;

class MapType
{
public:
	MapType(int row, int col, const char *txt);
        MapType(const MapType &m);
	~MapType();
	void print();
	char &operator()(int a, int b);
private:
	char **ptr;
	int mRow, mCol;
        //private data, can't be accesed outside the class

};

MapType::MapType(int row, int col, const char *txt)
{
        //assigns some private data
	mRow = row;
	mCol = col;
        
        //creates a input file type object
	ifstream in(txt);
        
        //allocates a 2D array dinamically
	ptr = new char*[row];
	for (int i = 0; i < row; i++)
		ptr[i] = new char[col];

        //reads from file
       //remember that this method ignores white spaces ' '
       //so you better f ill the map free spaces with something then modify print() function
       //or replace them with spaces
	for (int i = 0; i < mRow; i++)
	for (int j = 0; j < mCol; j++)
		in >> ptr[i][j];

	in.close();
}

//copy constructor, this is applied when you copy a map onto another
MapType::MapType(const MapType &m)
{
	mRow = m.mRow;
	mCol = m.mCol;

	ptr = new char*[mRow];
	for (int i = 0; i < mRow; i++)
		ptr[i] = new char[mCol];

	for (int i = 0; i < mRow; i++)
	for (int j = 0; j < mCol; j++)
		ptr[i][j] = m.ptr[i][j];
}

//overloaded operator (example cout << myMap(10,12))
char & MapType::operator()(int a, int b)
{
	return ptr[a][b];
}

//prints map
void MapType::print()
{
	for (int i = 0; i < mRow; i++)
	{
		for (int j = 0; j < mCol; j++)
			cout << ptr[i][j];
		cout << endl;
	}
}

//destructor called when program ends
//it deletes the map from memory
MapType::~MapType()
{
	for (int i = 0; i < mRow; i++)
		delete[] ptr[i];
	delete[] ptr;
}

int main(){
	
	MapType myMap(10, 10, "map1.txt"); //creates a 10x10 map read from map1.txt
	vector<MapType> maps; //creates a vector of maps
	maps.push_back(myMap); //pushes the map in the back of the vector

	maps[0].print(); //prints first map
	maps[0](2, 2) = '@'; //accesses the 2x2 element of the first map
	maps[0].print();

	return 0;
}
Topic archived. No new replies allowed.