Adding a bunch of asserts

cat-town
Dan Buch 13 years ago
parent e69dc72f9c
commit 874eb4f3a2

@ -3,11 +3,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <assert.h>
#include "ex19.h" #include "ex19.h"
int Monster_attack(void *self, int damage) int Monster_attack(void *self, int damage)
{ {
assert(self != NULL);
assert(damage > -1);
Monster *monster = self; Monster *monster = self;
printf("You attack %s!\n", monster->_(description)); printf("You attack %s!\n", monster->_(description));
@ -25,6 +29,8 @@ int Monster_attack(void *self, int damage)
int Monster_init(void *self) int Monster_init(void *self)
{ {
assert(self != NULL);
Monster *monster = self; Monster *monster = self;
monster->hit_points = 10; monster->hit_points = 10;
return 1; return 1;
@ -39,6 +45,9 @@ Object MonsterProto = {
void *Room_move(void *self, Direction direction) void *Room_move(void *self, Direction direction)
{ {
assert(self != NULL);
assert(direction);
Room *room = self; Room *room = self;
Room *next = NULL; Room *next = NULL;
@ -69,6 +78,9 @@ void *Room_move(void *self, Direction direction)
int Room_attack(void *self, int damage) int Room_attack(void *self, int damage)
{ {
assert(self != NULL);
assert(damage > -1);
Room *room = self; Room *room = self;
Monster *monster = room->bad_guy; Monster *monster = room->bad_guy;
@ -90,6 +102,9 @@ Object RoomProto = {
void *Map_move(void *self, Direction direction) void *Map_move(void *self, Direction direction)
{ {
assert(self != NULL);
assert(direction);
Map *map = self; Map *map = self;
Room *location = map->location; Room *location = map->location;
Room *next = NULL; Room *next = NULL;
@ -106,6 +121,9 @@ void *Map_move(void *self, Direction direction)
int Map_attack(void *self, int damage) int Map_attack(void *self, int damage)
{ {
assert(self != NULL);
assert(damage > -1);
Map *map = self; Map *map = self;
Room *location = map->location; Room *location = map->location;
@ -115,6 +133,8 @@ int Map_attack(void *self, int damage)
int Map_init(void *self) int Map_init(void *self)
{ {
assert(self != NULL);
Map *map = self; Map *map = self;
// make some rooms for a small map // make some rooms for a small map
@ -156,12 +176,13 @@ int process_input(Map *game)
printf("\n> "); printf("\n> ");
char ch = getchar(); char ch = getchar();
assert(ch != EOF);
getchar(); // eat ENTER getchar(); // eat ENTER
int damage = rand() % 4; int damage = rand() % 4;
switch(ch) { switch(ch) {
case -1: case EOF:
printf("Giving up? You suck.\n"); printf("Giving up? You suck.\n");
return 0; return 0;
break; break;

@ -7,6 +7,8 @@
void Object_destroy(void *self) void Object_destroy(void *self)
{ {
assert(self != NULL);
Object *obj = self; Object *obj = self;
if(obj) { if(obj) {
@ -18,6 +20,8 @@ void Object_destroy(void *self)
void Object_describe(void *self) void Object_describe(void *self)
{ {
assert(self != NULL);
Object *obj = self; Object *obj = self;
printf("%s\n", obj->description); printf("%s\n", obj->description);
} }
@ -25,6 +29,8 @@ void Object_describe(void *self)
int Object_init(void *self) int Object_init(void *self)
{ {
assert(self != NULL);
// do nothing really // do nothing really
return 1; return 1;
} }
@ -32,6 +38,9 @@ int Object_init(void *self)
void *Object_move(void *self, Direction direction) void *Object_move(void *self, Direction direction)
{ {
assert(self != NULL);
assert(direction);
printf("You can't go that direction.\n"); printf("You can't go that direction.\n");
return NULL; return NULL;
} }
@ -39,6 +48,9 @@ void *Object_move(void *self, Direction direction)
int Object_attack(void *self, int damage) int Object_attack(void *self, int damage)
{ {
assert(self != NULL);
assert(damage);
printf("You can't attack that.\n"); printf("You can't attack that.\n");
return 0; return 0;
} }
@ -46,7 +58,9 @@ int Object_attack(void *self, int damage)
void *Object_new(size_t size, Object proto, char *description) void *Object_new(size_t size, Object proto, char *description)
{ {
assert(size);
assert(description != NULL); assert(description != NULL);
// setup the default functions in case they aren't set // setup the default functions in case they aren't set
if(!proto.init) proto.init = Object_init; if(!proto.init) proto.init = Object_init;
if(!proto.describe) proto.describe = Object_describe; if(!proto.describe) proto.describe = Object_describe;
@ -62,6 +76,7 @@ void *Object_new(size_t size, Object proto, char *description)
// copy the description over // copy the description over
el->description = strdup(description); el->description = strdup(description);
assert(el->description != NULL);
// initialize it with whatever init we were given // initialize it with whatever init we were given
if(!el->init(el)) { if(!el->init(el)) {

Loading…
Cancel
Save