-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.c
72 lines (63 loc) · 1.74 KB
/
room.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
#include "defs.h"
/*
Function: initRoom
Purpose: initialize an empty room with a name. Dynamically allocate memory for the rooms , and set appropriate default values for their fields
Parameters:
in: char *name: room's name field
out: RoomType **room: The room to be initialized
Returns: void
*/
void initRoom(RoomType **room, char *name)
{
RoomType *r = malloc(sizeof(RoomType));
strcpy(r->name, name);
r->ghost = NULL;
r->evidence = NULL;
for (int i = 0; i < MAX_HUNTERS; i++)
{
r->hunters[i] = NULL;
}
for (int i = 0; i < DIRECTION_COUNT; i++)
{
r->doors[i] = NULL;
}
r->hunterCount = 0;
sem_init(&r->mutex, 0, 1); // init to 1;
*room = r;
}
/*
Function: connectRooms
Purpose: connect two rooms
Parameters:
out: RoomType *r1: modify r1's door at the dir direction
out: RoomType *r2: modify r2's door at the opposite direction of dir direction
in: DirectionType dir: it is used to get the correct door
Returns: DoorType
*/
DoorType *connectRooms(RoomType *r1, DirectionType dir, RoomType *r2)
{
DoorType *d; // pointer to the door
initDoor(&d, r1, r2);
r1->doors[dir] = d;
r2->doors[getOppositeDirection(dir)] = d;
return d;
}
/*
Function: findEnterRoom
Purpose: find the room the hunter is entering into
Parameters:
in: RoomType *curRoom: it is used to find the door
in: int dir: it is used to find the door
Returns: RoomType
*/
RoomType *findEnterRoom(RoomType *curRoom, int dir)
{
if (curRoom->doors[dir]->roomA == curRoom)
{
return curRoom->doors[dir]->roomB;
}
else
{
return curRoom->doors[dir]->roomA;
}
}