Adding a bunch of asserts

This commit is contained in:
Dan Buch 2011-10-26 03:47:15 -04:00
parent e69dc72f9c
commit 874eb4f3a2
2 changed files with 37 additions and 1 deletions

23
ex19.c
View File

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

View File

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