-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkuchmanc.buildrooms.c
158 lines (126 loc) · 3.97 KB
/
kuchmanc.buildrooms.c
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
#include<stdio.h>
#include<string.h>
#include<time.h>
int main(void)
{
int i, j;
srand(time(0));
//Create a directory called kuchmanc.rooms.<PROCESS ID>
int pid = getpid();
char dirName[50];
memset(dirName, '\0', 50);
sprintf(dirName, "./kuchmanc.rooms.%d", pid);
mkdir(dirName, 0700);
//Generate 7 different room files
//Create list of 10 file names possible
char roomNames[10][10];
for (i = 0; i < 10; i++)
{
//TODO This I think needs to include sizeof
memset(roomNames[i], '\0', 10);
}
//Hardcode different room names
sprintf(roomNames[0], "BugCity");
sprintf(roomNames[1], "PeekPeak");
sprintf(roomNames[2], "TheNode");
sprintf(roomNames[3], "SpookDen");
sprintf(roomNames[4], "RatFjord");
sprintf(roomNames[5], "LostBay");
sprintf(roomNames[6], "Castle");
sprintf(roomNames[7], "LightDam");
sprintf(roomNames[8], "Glendale");
sprintf(roomNames[9], "TownTown");
//Randomly create 7 rooms (no duplicates)
int nameOpts[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int pickedNames[7];
//Increment through shrinking list of options picking names
int randPos;
for (i = 0; i < 7; i++)
{
//Generate random number between the current i and end of list
randPos = (rand() % (10 - i)) + i;
pickedNames[i] = nameOpts[randPos];
//make the picked name "removed"
nameOpts[randPos] = nameOpts[i];
}
//Creates the seven rooms
FILE *roomFiles[7];
char filePath[60];
memset(filePath, '\0', 60);
for (i = 0; i < 7; i++) {
//Creates and open files
sprintf(filePath, "./%s/%s_room", dirName, roomNames[pickedNames[i]]);
roomFiles[i] = fopen(filePath, "w+");
fprintf(roomFiles[i], "ROOM NAME: %s\n", roomNames[pickedNames[i]]);
}
//Generate all the connections until the requirements are achieved
int roomConnections[7][7] = { 0 };
int roomConCount[7] = { 0 };
for (i = 0; i < 7; i++){
roomConnections[i][i] = -1;
}
//Randomly assigns connections
int row, col;
while (!ValidNumConnections(&roomConCount[0], 7)) {
row = 0;
col = 0;
//Generate random connections until one is valid
while (roomConnections[row][col] != 0 || roomConCount[row] == 6 || roomConCount[col] == 6) {
row = rand() % 7;
col = rand() % 7;
}
//Make connection and store number of connections per room
roomConnections[row][col] = 1;
roomConnections[col][row] = 1;
roomConCount[row]++;
roomConCount[col]++;
//TODO Remove as just for testing
//for (i = 0; i < 7; i++) {
// for (j = 0; j < 7; j++) {
// printf("%d ", roomConnections[i][j]);
// }
// printf("\n");
//}
}
//Add connection text to files
int roomNum;
for (i = 0; i < 7; i++) {
roomNum = 1;
for (j = 0; j < 7; j++) {
if (roomConnections[i][j] == 1) {
fprintf(roomFiles[i], "CONNECTION %d: %s\n", roomNum, roomNames[pickedNames[j]]);
roomNum++;
}
}
}
//Determine the start and end room
int startRoom = rand() % 7;
int endRoom;
do {
endRoom = rand() % 7;
} while(endRoom == startRoom);
for (i = 0; i < 7; i++) {
if (i == startRoom) {
fprintf(roomFiles[i], "ROOM TYPE: START_ROOM\n");
} else if (i == endRoom) {
fprintf(roomFiles[i], "ROOM TYPE: END_ROOM\n");
} else {
fprintf(roomFiles[i], "ROOM TYPE: MID_ROOM\n");
}
}
//Close the rooms
for (i = 0; i < 7; i++) {
fclose(roomFiles[i]);
}
return 0;
}
int ValidNumConnections(int *conCount, int numRooms) {
int validNum = 1;
int i;
for (i = 0; i < numRooms; i++) {
if (conCount[i] < 3) {
validNum = 0;
}
}
return validNum;
}