-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_builder.cpp
338 lines (274 loc) · 8.98 KB
/
maze_builder.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
CODE PAGE 437: PIPES AND LINES!!!
*/
#include <iostream>
#include <windows.h>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <vector>
#include <fstream>
#include <string>
#include <mmsystem.h>
//define some shortcut strings to represent the box-builder codes.
#define blp 200 //bottom left pipe
#define trp 187 //top right pipe
#define brp 188 //bottom right pipe
#define tlp 201 //top left pipe
#define hp 205 //horizontal pipe
#define vp 186 //vertical pipe
#define tp 203 //T pipe
#define btp 202 //Bottom T pipe (an upside down "T")
#define ltp 204 //Left T pipe (T rotated left 90 degrees)
#define rtp 185 //Right T pipe (T rotated right 90 degrees)
#define cp 206 //cross pipe
using namespace std;
//elementof:: int, int, int[] -> bool
//checks if value i is in the array[] of length length
int elementof(int i, int length, int array[]){
for(int j = 0; j < length; j++){
if(i == array[j])
{
return 1;
}
}
return 0;
}
//rando:: int, int -> int
//returns a random integer in the interval [min,max]
int rando(int min, int max)
{
return rand() * (max - min + 1) + min;
}
//pickrand:: int, int[] -> int
//randomly choose and return an element of an array[] of length length
int pickrand(int length, int array[]){
int random = rando(0,length - 1);
int result = array[random];
return result;
}
//title:: ->
//Prints the title page!! :)
void title(){
//////////////////////////////////////////
string line;
ifstream myfile ("pipesandlinestitle.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) ){
cout << line << '\n';
}
myfile.close();
}
cout << "by Daniel Chen. April 16, 2017. \n";
getchar();
/////////////////////////////////////////////////
}
//Special pipes, defined by their ability to be connected to later pipes.
int rightpipe[7] = { tlp, blp, hp, cp, tp, btp, ltp}; //Any pipe that can be connected at the right.
int downpipe[7] = {tlp,trp,vp,cp,tp,ltp,rtp}; //Any pipe that can be connected from below.
//connectabove:: int, vector -> bool
//check if your current position can be connected from above. Returns 1 for true and 0 for false.
int connectabove(int j, vector < vector <int> > &map){
if( elementof(map[j][0], 7, downpipe) )
{
return 1;
}
else{
return 0;
}
}
//connectleft:: int, vector -> bool
//check if your current position can be connected from the left. Returns 1 for true and 0 for false.
//Note that we assert that j > 0.
int connectleft(int j, vector < vector <int> > &map){
if( elementof(map[j - 1][1], 7, rightpipe) )
{
return 1;
}
else{
return 0;
}
}
//mapout:: int, int, int, int->
//The main process function. Takes input for width and height from the user, and then generates a random map in those boundaries.
void mapout(int width, int height, int sound, int delay){
//
//
//Now that we've got the desired map dimensions, lets initialize an array. It will contain the Code Page 437 value for each character to be drawn to the screen.
//We make map a two dimensional vector (i.e. a vector of vectors), that way we can do dynamic memory allocation at runtime.
vector < vector <int> > map (width, vector<int> (2));
/*
Setup the map with values between 179 and 218, inclusive. These are the *Box-drawing Characters* from Code Page 437.
The number in the array corresponds to the box-drawing character to be drawn there.
*/
//Loop across the map. Start at the left of the current height (j), initialize map[j,i], then move over right one.
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j ++)
{
int *x = &map[j][1];//point to the current position of the map. We will decide what this is now.
//Top left corner of map?
if ((i == 0) &&(j == 0)){
*x = tlp; //make the top left corner of the map a top-left pipe, 'cause.
}
else if ((i == 0 ) && (j == width - 1)){
if (elementof(map[j -1][1], 7, rightpipe)){
*x = trp;}
else *x = 32;
}
else if ((i == height - 1) && (j == 0)){
if (elementof(map[j][0], 7, downpipe)){
*x = blp;}
else *x = 32;
}
else if ((i == height - 1 ) && (j == width - 1)){
if ( connectleft(j,map) && connectabove(j,map) ){
*x = brp;
}
else if (connectleft(j,map)){
*x = hp;
}
else if (connectabove(j,map)) {
*x = vp;
}
else {
*x = 32;
}
}
//if we are at the top of the map
else if (i == 0){
if ( connectleft(j,map)){ //if the piece to the left can be connected at the right, choose a piece to connect.
int array[3] = {trp,hp,tp};
*x = array[rand()%3];
}
else{
int array[2] = {tlp, 32}; //32 = whitespace as per the space bar.
*x = array[rand()%2];
}
}
//if we are at the leftmost of the map
else if (j == 0){
if( connectabove(j,map)) { //if it can be connected at from above
int array[3] = {vp,ltp, blp};
*x = array[rand()%3];
}
else{
int array[2] = {tlp, 32}; //32 = whitespace as per the space bar.
*x = array[rand()%2];
}
}
//if we are at the rightmost of the map
else if (j == width - 1){
if ( connectleft(j,map) && connectabove(j,map) ){ //if it can be connected to the left and above
int array[2] = {rtp,brp};
*x = array[rand()%2];
}
else if( connectleft(j,map) ) { //if it can only be connected at the left
*x = trp;
}
else if( connectabove(j,map) ) { //if it can only be connected at from above
*x = vp;
}
else{ //32 = whitespace as per the space bar.
*x = 32;
}
}
//if we are at the bottom of the map
else if (i == height - 1){
if ( connectleft(j,map) && connectabove(j,map) )
{
int array[2] = {brp,btp};
*x = array[rand() % 2];
}
else if (connectleft(j,map) ){
*x = hp;
}
else if (connectabove(j,map) ){
*x = blp;
}
else{
*x = 32;
}
}
//somewhere in the middle of all of this nonsense.
else{
if ( connectleft(j,map) && connectabove(j,map) ){ //if it can be connected to the left and above
int array[3] = {rtp,cp,brp};
*x = array[rand()%3];
}
else if( connectleft(j,map) ) { //if it can only be connected at the left
int array[3] = {hp,trp,tp};
*x = array[rand()%3];
}
else if( connectabove(j,map) ) { //if it can only be connected at from above
int array[3] = {vp,ltp,blp};
*x = array[rand()%3];
}
else{
int array[2] = {tlp, 32}; //32 = whitespace as per the space bar.
*x = array[rand()%2];
}
}
//Now that we've decided what goes here, lets print it!
cout << (char) map[j][1]; //print what we just decided for the current coordinate.
map[j][0] = map[j][1]; //push this value up into the buffer.
//If we asked for sound, bleep!!
if (sound == 1){
Beep(rand()%3000 + 200,500);
}
Sleep (delay); //delay for the amount specified by the user.
}
cout << endl; //we reached the end of our line, so move to the next one.
}
}
int main()
{
//Set the console to Code Page 437!! Standard Windows ALT + number pad combination characters...
//Note, native to windows OS (requires windows.h library which is obviously not available in Linux or Mac, unless you do some serious digging around).
SetConsoleCP(437);
SetConsoleOutputCP(437);
//////////////////////////////////////////////////////////////////
srand(time(NULL));//random time seed.
int width = 200, height = 30; //width and height variables for later use.
int delay = 0; //delay variable for speed of printing.
int sound = 0; //a boolean indicating whether we desire random sounds.
int choice = 0; //menu choice value.
//////////////////////////////////////////////////////////////////
title(); //print the title
while(0 < 1 + 2 + 3 + 4 + 5){
cout << "MENU" << endl;
cout << "1. Generate Random Map" << endl << "2. Settings" << endl;
cout << "What would you like to do? [1,2]:";
cin >> choice;
switch(choice){
case 1:
cout << "Hello, enter your width:";
cin >> width;
cout << "Now enter your height:";
cin >> height;
mapout(width,height,sound,delay); // given the map dimensions, print a random map and keep doing the doggy game...or whatever.
break;
case 2:
cout << "SETTINGS" << endl;
cout << "1. Print Speed" << endl << "2. Sound FX" << endl;
cout << "What would you like to do? [1,2]:";
cin >> choice;
switch(choice){
case 1:
cout << "Enter your new print delay in milliseconds(ms). [Enter 0 for no delay]: ";
cin >> delay;
break;
case 2:
cout << "Enter whether you would like annoying random sounds in between printing. [1 = yes, 0 = no]";
cin >> sound;
break;
default:
cout << "Wrong button!" << endl;
}
break;
default:
cout << "Are you some sort of...!!? Try again! " << endl;
}
}
return 0;
}