-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilding.c
executable file
·183 lines (161 loc) · 5.28 KB
/
building.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
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
#include "defs.h"
/*
Function: populateBuilding
Purpose: Populates a building with rooms and doors
Parameters:
in-out: BuildingType* building: The building to populate
Returns: void
*/
void populateBuilding(BuildingType *building)
{
/*
* Requires that the building has been initialized with initBuilding()
* Requires that initRoom and connectRooms have been implemented
* You are allowed to modify this function, but the room connections
* must remain the same. The overall map MUST be the same, with the same names
* and the same connections. Failure to do so will result in major
* deductions.
*/
// Room names array
char *room_names[] = {
"Van", "Hallway N", "Hallway S", "Master Bedroom", "Boy's Bedroom", "Bathroom",
"Kitchen", "Living Room", "Garage", "Utility Room"};
int num_rooms = sizeof(room_names) / sizeof(room_names[0]);
// Initialize rooms: It should be strcpy/strlcpy/strncpy the name
for (int i = 0; i < num_rooms; i++)
{
initRoom(&(building->rooms[i]), room_names[i]);
}
// Connect rooms. The first room, in the direction, you will find the second room.
// connectRooms() returns a pointer to the door that connects the two rooms.
building->doors[0] = connectRooms(building->rooms[0], SOUTH, building->rooms[1]);
building->doors[1] = connectRooms(building->rooms[1], EAST, building->rooms[4]);
building->doors[2] = connectRooms(building->rooms[1], WEST, building->rooms[3]);
building->doors[3] = connectRooms(building->rooms[1], SOUTH, building->rooms[2]);
building->doors[4] = connectRooms(building->rooms[2], EAST, building->rooms[5]);
building->doors[5] = connectRooms(building->rooms[2], SOUTH, building->rooms[6]);
building->doors[6] = connectRooms(building->rooms[6], SOUTH, building->rooms[7]);
building->doors[7] = connectRooms(building->rooms[6], WEST, building->rooms[8]);
building->doors[8] = connectRooms(building->rooms[8], NORTH, building->rooms[9]);
building->roomCount = num_rooms;
building->doorCount = 9;
}
/*
Function: initBuilding
Purpose: initialize the building; Dynamically allocate memory for the building and set the default values for its fields
Parameters:
out: BuildingType **building: The building to be initialized
Returns: void
*/
void initBuilding(BuildingType **building)
{
BuildingType *b = malloc(sizeof(BuildingType)); // return pointer's address
// set 3 count values to 0
b->doorCount = 0;
b->roomCount = 0;
b->hunterCount = 0;
// set 3 pointers
b->journal = NULL;
b->ghost = NULL;
// array of pointers
// init to NULL
for (int i = 0; i < MAX_HUNTERS; i++)
{
b->hunters[i] = NULL;
}
for (int i = 0; i < MAX_ROOMS; i++)
{
b->rooms[i] = NULL;
}
for (int i = 0; i < MAX_DOORS; i++)
{
b->doors[i] = NULL;
}
*building = b;
}
/*
Function: cleanupBuilding
Purpose: cleanup all allocated memory at the end of the program
Parameters:
in/out: BuildingType *b: it is used to get access to all data and to be cleared after this function is completed
Returns: void
*/
void cleanupBuilding(BuildingType *b)
{
// clean all evidence
EvidenceNodeType *currNode;
EvidenceNodeType *nextNode;
currNode = b->journal->head;
while (currNode != NULL)
{
nextNode = currNode->next;
free(currNode->evidence);
currNode = nextNode;
}
currNode = b->journal->head;
while (currNode != NULL)
{
nextNode = currNode->next;
free(currNode);
currNode = nextNode;
}
free(b->journal);
// clean hunters
for (int i = 0; i < b->hunterCount; i++)
{
free(b->hunters[i]);
}
// clean rooms
for (int i = 0; i < b->roomCount; i++)
{
free(b->rooms[i]->evidence);
free(b->rooms[i]);
}
// clean doors
for (int i = 0; i < b->doorCount; i++)
{
free(b->doors[i]);
}
// clean building
free(b);
}
/*
Function: printResults
Purpose: display required results to the terminal after all threads finish running
Parameters:
in: BuildingType *b: it is used to get access to specific data in order to print results to the screen
Returns: void
*/
void printResults(BuildingType *b)
{
printf("\n\nResults: \n");
for (int i = 0; i < b->hunterCount; i++)
{
if (b->hunters[i]->boredom == MAX_BOREDOM)
{
printf("%s was too bored and ran away!\n", b->hunters[i]->name);
}
else if (b->hunters[i]->fear == MAX_FEAR)
{
printf("%s was too afraid and ran away!\n", b->hunters[i]->name);
}
else
{
printf("%s escaped the ghost!\n", b->hunters[i]->name);
}
}
printf("\n");
printEvidence(b->journal);
char ghostType[MAX_STR];
ghostToString(b->journal->guess, ghostType);
if (b->journal->banished == TRUE)
{
printf("The hunters guessed correctly! The ghost was a %s.\n", ghostType);
printf("The hunters banished the ghost!\n");
}
else
{
printf("The hunters did not have a guess! They didn't know it was a %s.\n", ghostType);
printf("The hunters did not banish the ghost!\n");
}
}