ex19 audit bits

cat-town
Dan Buch 9 years ago
parent 9569ff1805
commit bc195dec65

@ -3,11 +3,13 @@
#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);
Monster *monster = self; Monster *monster = self;
printf("You attack %s!\n", monster->_(description)); printf("You attack %s!\n", monster->_(description));
@ -25,6 +27,7 @@ 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;
@ -38,6 +41,7 @@ Object MonsterProto = {
void *Room_move(void *self, Direction direction) void *Room_move(void *self, Direction direction)
{ {
assert(self != NULL);
Room *room = self; Room *room = self;
Room *next = NULL; Room *next = NULL;
@ -68,6 +72,7 @@ void *Room_move(void *self, Direction direction)
int Room_attack(void *self, int damage) int Room_attack(void *self, int damage)
{ {
assert(self != NULL);
Room *room = self; Room *room = self;
Monster *monster = room->bad_guy; Monster *monster = room->bad_guy;
@ -89,6 +94,7 @@ Object RoomProto = {
void *Map_move(void *self, Direction direction) void *Map_move(void *self, Direction direction)
{ {
assert(self != NULL);
Map *map = self; Map *map = self;
Room *location = map->location; Room *location = map->location;
Room *next = NULL; Room *next = NULL;
@ -104,6 +110,7 @@ void *Map_move(void *self, Direction direction)
int Map_attack(void *self, int damage) int Map_attack(void *self, int damage)
{ {
assert(self != NULL);
Map *map = self; Map *map = self;
Room *location = map->location; Room *location = map->location;
@ -113,6 +120,7 @@ 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
@ -149,6 +157,7 @@ Object MapProto = {
int process_input(Map *game) int process_input(Map *game)
{ {
assert(game != NULL);
printf("\n> "); printf("\n> ");
char ch = getchar(); char ch = getchar();
@ -204,6 +213,7 @@ int main(int argc, char *argv[])
// make our map to work with // make our map to work with
Map *game = NEW(Map, "The Hall of the Minotaur."); Map *game = NEW(Map, "The Hall of the Minotaur.");
assert(game != NULL);
printf("You enter the "); printf("You enter the ");
game->location->_(describe)(game->location); game->location->_(describe)(game->location);

@ -16,6 +16,7 @@ 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);
} }
@ -40,6 +41,8 @@ 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(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;
@ -50,6 +53,7 @@ void *Object_new(size_t size, Object proto, char *description)
// this seems weird, but we can make a struct of one size, // this seems weird, but we can make a struct of one size,
// then point a different pointer at it to "cast" it // then point a different pointer at it to "cast" it
Object *el = calloc(1, size); Object *el = calloc(1, size);
assert(el != NULL);
*el = proto; *el = proto;
// copy the description over // copy the description over

Loading…
Cancel
Save