Skip to content

Commit

Permalink
Splitted code into multiple files. Added room.c
Browse files Browse the repository at this point in the history
Makefile: remove $(OBJS) instead of *.o
  • Loading branch information
oestrich committed Oct 25, 2010
1 parent 8f9641c commit bbd9d42
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 55 deletions.
54 changes: 0 additions & 54 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#include <stdlib.h>
#include "room.h"

struct room * create_area(char *);
void free_area(struct room **);
void link_areas(struct room *, char, struct room *);

int main(){
struct room * start;
struct room * hallway;
Expand Down Expand Up @@ -36,53 +32,3 @@ int main(){
return 0;
}

// Clean up memory
void free_area(struct room ** b){
free(*b);

*b = NULL;
}

// Allocate memory for a struct room
struct room * create_area(char * name){
struct room * temp = malloc(sizeof(struct room));

if(temp == NULL){
printf("Error allocating memory\n");
exit(0);
}

temp->name = name;

return temp;
}

/**
* Take two areas and link them
*
* The second argument is how b is located to a
* a, 'w', b goes to
* a->west = b
* b->east = a
*/
void link_areas(struct room * a, char l, struct room * b){
switch(l){
case 'n':
a->north = b;
b->south = a;
break;
case 'e':
a->east = b;
b->west = a;
break;
case 'w':
a->west = b;
b->east = a;
break;
case 's':
a->south = b;
b->north = a;
break;
}
}

2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ $(EXEC): $(OBJS)
$(CC) -o $(EXEC) $(OBJS)

clean:
rm -f *.o
rm -f $(OBJS)
rm -f $(EXEC)
54 changes: 54 additions & 0 deletions room.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include "room.h"

// Clean up memory
void free_area(struct room ** b){
free(*b);

*b = NULL;
}

// Allocate memory for a struct room
struct room * create_area(char * name){
struct room * temp = malloc(sizeof(struct room));

if(temp == NULL){
printf("Error allocating memory\n");
exit(0);
}

temp->name = name;

return temp;
}

/**
* Take two areas and link them
*
* The second argument is how b is located to a
* a, 'w', b goes to
* a->west = b
* b->east = a
*/
void link_areas(struct room * a, char l, struct room * b){
switch(l){
case 'n':
a->north = b;
b->south = a;
break;
case 'e':
a->east = b;
b->west = a;
break;
case 'w':
a->west = b;
b->east = a;
break;
case 's':
a->south = b;
b->north = a;
break;
}
}

7 changes: 7 additions & 0 deletions room.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef ROOM_H
#define ROOM_H

struct room{
char * name;
Expand All @@ -8,3 +10,8 @@ struct room{
struct room * south;
};

struct room * create_area(char *);
void free_area(struct room **);
void link_areas(struct room *, char, struct room *);
#endif

0 comments on commit bbd9d42

Please sign in to comment.