-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_Map.cpp
153 lines (139 loc) · 2.3 KB
/
class_Map.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Hero_Game.h"
//
// All members of the Map class
//
// Default constructor
Map::Map()
{
arr = NULL;
}
// Destructor
Map::~Map()
{
clear();
}
// Constructor for current level
Map::Map(int l)
{
char mapName[9] = "map0.txt";
if( l > 0 && l < 4)
{
this->level = l;
mapName[3] = level + '0';
ifstream file;
file.open(mapName,ios::in);
if(file.good())
{
file >> lines >> colons;
file.ignore();
allocate(lines,colons);
int i = 0;
while(file.good())
{
file.getline(arr[i],colons+1,'\n');
i++;
}
}
else
{
this->failed();
}
file.close();
}
// Loads the saved map if the level is incorect
else
{
ifstream file;
file.open("SaveMap.txt" ,ios::in);
int l,c;
if(file.good())
{
file >> l >> c >> level;
lines = l;
colons = c;
file.ignore();
allocate(lines,colons);
int i = 0;
while(file.good())
{
file.getline(arr[i],colons+1,'\n');
i++;
}
}
else
{
this->failed();
}
file.close();
}
}
// If the map does not exist,the function stops the game
void Map::failed()
{
isRunning = false;
fail = true;
level = 0;
lines = 0;
colons = 0;
allocate(0,0);
}
// Deletes the map
void Map::clear()
{
if(arr != NULL)
{
for(int i=0; i<lines; i++)
delete[] arr[i];
delete[] arr;
arr = NULL;
}
}
// Allocates the necessary number of symbols
void Map::allocate(int l,int c)
{
arr = new char* [l];
for(int i=0; i<l ;i++)
arr[i] = new char [c+1];
}
// Prints only the map
void Map::printMap() const
{
for(int i=0;i<lines; i++)
{
cout<<arr[i];
cout<<endl;
}
}
Map& Map::operator=(const Map& m)
{
if(&m != this)
{
for(int i=0; i<lines; i++)
delete[] arr[i];
delete arr;
lines = m.lines;
colons = m.colons;
level = m.level;
allocate(lines,colons);
for(int i=0; i<lines; i++)
strcpy(this->arr[i], m.arr[i]);
}
return *this;
}
// Saves the map into a .txt file
void Map::SaveMap()const
{
ofstream file;
file.open("SaveMap.txt", ios::out, ios::trunc);
if(file.good() )
{
file<<lines<<" "<<colons<<" "<<level<<"\n";
for(int i=0; i<lines; i++)
{
file<<arr[i];
if( i < lines - 1)
file<<"\n";
}
}
file.close();
}