-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.cpp
110 lines (87 loc) · 1.66 KB
/
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
// Jonnie Herrera
// Unfinished map sample, need object class functions implementations
#include "map-sample.h"
#include <fstream>
// constructor
map::map()
{
worldMap = getMap(); // worldMap catches the getMap value which will be a 2D array
// Original 2D array initialization
/*
for(int r = 0; r < 128; r++)
{
worldMap[r] = new grovnik[128];
}
for(int r = 0; r < 128; r++)
{
for(int c = 0; c < 128; c++)
{
worldMap[r][c].item = NULL;
worldMap[r][c].visited = 0;
}
}
*/
}
// Destructor: still need to check for memory leaks
map::~map()
{
if(worldMap)
{
for(int c = 0; c < 5; c++)
{
delete [] worldMap[c];
}
delete [] worldMap;
}
}
// function reads a text file filled with numbers, representing the items
grovnik** map::getMap()
{
ifstream in_file;
in_file.open("map.txt");
if(!in_file)
{
cout << "Failed to open file!" << endl;
return 0;
}
grovnik ** Map = new grovnik * [5];
for(int r = 0; r < 5; r++)
{
Map[r] = new grovnik[5];
}
for(int r = 0; r < 5; r++)
{
for(int c = 0; c < 5; c++)
{
in_file >> Map[r][c].item;
//Map[r][c].item = in_file.get(); // need a set type function from object class
Map[r][c].visited = 0;
}
}
return Map; // returns 2D array, need to ask if its okay
}
// displays the map
void map::displayMap()
{
if(!worldMap)
{
cout << "No map created!";
return;
}
for(int r = 0; r < 5; r++)
{
for(int c =0; c < 5; c++)
{
cout << worldMap[r][c].item;
}
cout << endl;
}
//cout << "3, 3: " << worldMap[3][3].item << endl; // was testing whether it displayed correctly
}
// test main function to see if it worked
int main()
{
map obj;
obj.displayMap();
return 0;
}