forked from LEE-CHIEN-AN/BBBLUE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
166 lines (144 loc) · 4.34 KB
/
Copy pathmap.cpp
File metadata and controls
166 lines (144 loc) · 4.34 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//方格的種類
enum TileType {
GRASS,
WATER,
ROCK
};
struct Vector{
int x;
int y;
};
int generateRandomNumber(int minValue, int maxValue) {
return rand() % (maxValue - minValue + 1) + minValue;
}
//這是個別的方格
class Tile {
private:
TileType type;
public:
Tile(TileType type){
this->type = type;
}
Tile(){}
TileType getTile(){
return type;
}
void setTile(TileType type){
this->type = type;
return;
}
};
//生成地圖
class TileMap {
private:
int width;
int length;
int obstacleCnt;
Tile*** TileMapObject;
public:
TileMap(int width, int length){
this->width = width;
this->length = length;
this->obstacleCnt = 0.1*(length*width);
TileMapObject = new Tile** [length];
for (int i = 0; i < length; i++){
TileMapObject[i] = new Tile* [width];
for (int j = 0; j < width; j++){
TileMapObject[i][j] = new Tile();
}
}
srand(static_cast<unsigned>(time(0)));
}
~TileMap() {
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
delete TileMapObject[i][j];
}
delete[] TileMapObject[i];
}
delete[] TileMapObject;
}
TileType getTileType(int x, int y) const {
return TileMapObject[y][x]->getTile();
}
void setTileType(int x, int y, TileType type) {
TileMapObject[y][x]->setTile(type);
}
void printTileMap() const {
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
switch (getTileType(j, i)) {
case GRASS:
cout << " 🌳 "; break;
case WATER:
cout << " 💧 "; break;
case ROCK:
cout << " 🪨 "; break;
}
}
cout << endl;
}
}
void generateWaterMass() {
int waterMassWidth = rand() % 3 + 2; // 在[2,4]之間取一個隨機數字當水池寬度
int waterMassLength = rand() % 3 + 2;
int startX = rand() % (width - waterMassWidth + 1);
int startY = rand() % (length - waterMassLength + 1);
int endX = startX + waterMassWidth - 1;
int endY = startY + waterMassLength - 1;
for (int i = startY; i <= endY; i++) {
for (int j = startX; j <= endX; j++) {
setTileType(j, i, WATER);
}
}
}
void randomRocks(int rockCnt) {
for (int i = 0; i < rockCnt; i++) {
int x = rand() % width;
int y = rand() % length;
//檢查這個方格是不是GRASS 是的話才設定為ROCK
if (getTileType(x, y) == GRASS) {
setTileType(x, y, ROCK);
} else {
i--;
}
}
}
};
int main(){
//請使用者輸入地圖的長與寬(最小地圖長寬為5x5)
int width, length;
cout << "The smallest generatable map is 5x5, please enter valid parameters!" << endl;
cout << "Enter the width of your map:" << endl;
cin >> width;
cout << "Enter the length of your map:" << endl;
cin >> length;
if (length < 5 || width < 5){
cout << "Invalid map parameters, please restart the game and try again!" << endl;
return 0;
}
//建立地圖
TileMap myTileMap(width, length);
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++) {
myTileMap.setTileType(j, i, GRASS);
}
}
//加入湖水
int lakeCnt = generateRandomNumber(static_cast<int>((length * width) * 0.02),
static_cast<int>((length * width) * 0.04));
//cout << lakeCnt << endl;
for (int i = 0; i < lakeCnt; i++)
myTileMap.generateWaterMass();
//加入岩石
int rockCnt = generateRandomNumber(static_cast<int>((length * width) * 0.05),
static_cast<int>((length * width) * 0.1));
//cout << rockCnt << endl;
myTileMap.randomRocks(rockCnt);
myTileMap.printTileMap();
return 0;
}