Skip to content
Snippets Groups Projects
Commit 3b46a488 authored by PAILLE Kyriann's avatar PAILLE Kyriann :8ball:
Browse files

initial commit

parent 6c4b550b
Branches
No related tags found
No related merge requests found
Makefile 0 → 100644
all: WorldOfIUT
exits.o: exits.h
cmd.o: cmd.h game.h exits.h mobile.h location.h
mobile.o: mobile.h exits.h
location.o: location.h exits.h
game.o: game.h location.h mobile.h exits.h
WorldOfIUT.o: cmd.h exits.h mobile.h game.h location.h
WorldOfIUT: WorldOfIUT.o cmd.o mobile.o location.o game.o exits.o
clean:
/bin/rm *.o WorldOfIUT
#include <stdio.h> /* for printf in Intro() */
#include "game.h" /* for Game structure and GameInit() */
#include "cmd.h" /* for processCommand() */
void Intro()
{
printf("Welcome to WorlOfIUT!\n"
"Be prepared because you are about to live extraordinary, frightful, yet exciting\n"
"adventures...\n"
"If you're new to the game, you can start by typing 'help'\n"
"Farewell, and may the spirit of the Vosges be with you!\n");
}
int main(int argc, char *argv[])
{
Intro();
Game *g=GameInit();
while(1) processCommand(g);
return 0;
}
exits.c 0 → 100644
#include "exits.h"
#include <strings.h> /* strcasecmp */
#include <stddef.h> /* NULL */
static char *dir_str[]={"north","east","south","west","up","down"};
Direction strtodir(char *name)
{
for (int i=NORTH; i<=DOWN; i++)
if (!strcasecmp(name,dir_str[i])) return i;
return WRONGDIR;
}
char *dirtostr(Direction d)
{
if (d >= NORTH && d <= DOWN) return dir_str[d];
return (char *)NULL;
}
\ No newline at end of file
exits.h 0 → 100644
#ifndef EXITS_H
#define EXITS_H
/*
directions encoded as int. Used as indices in the array of exits for each location
NORTH==0
EAST==1
SOUTH==2
WEST==3
UP==4
DOWN==5
anything other than a correct direction can be encoded as WRONGDIR==-1
The number of directions can be computed as DOWN-NORTH+1
A look over all directions can be written as
for (int i=NORTH; i<=DOWN; i++)
*/
typedef enum {WRONGDIR=-1,NORTH=0,EAST,SOUTH,WEST,UP,DOWN} Direction;
/* translate a string into a DIRECTION. Letter case does not matter */
extern Direction strtodir(char *name);
/* return the direction as a string. Return NULL if the direction is invalid */
extern char *dirtostr(Direction d);
#endif // EXITS_H
\ No newline at end of file
game.c 0 → 100644
#include "game.h"
#include "mobile.h"
#include "location.h"
#include <stdlib.h> /* malloc, free, NULL */
/* Initialize everything that makes up the game: player and locations */
Game *GameInit()
{
Game *ret=malloc(sizeof(Game));
if(ret)
{
ret->player = MobileNew("You", "A very dynamic and adventurous young person!");
MobileMove(ret->player, LocationInit(ret));
}
return ret;
}
/* correctly deallocate everythig that was dynamically allocated in GameInit */
Game *GameShutdown(Game *g)
{
if (g)
{
g->player=MobileDelete(g->player);
free(g);
}
return (Game*)NULL;
}
\ No newline at end of file
game.h 0 → 100644
#ifndef GAME_H
#define GAME_H
#include "mobile.h"
typedef struct
{
Mobile *player;
} Game;
extern Game *GameInit();
extern Game *GameShutdown(Game *g);
#endif // GAME_H
mobile.c 0 → 100644
#include "mobile.h"
#include "location.h"
#include <string.h> /* strdup */
#include <stdio.h> /* printf */
#include <stdlib.h> /* malloc, free */
Mobile *MobileNew(char *name, char *desc)
{
Mobile *ret=(Mobile*)NULL;
if (name && desc)/** && where)**/
{
ret=malloc(sizeof(Mobile));
ret->name=strdup(name);
ret->desc=strdup(desc);
}
return ret;
}
Mobile *MobileDelete(Mobile *m)
{
if (m)
{
if (m->name) free(m->name);
if (m->desc) free(m->desc);
free(m);
}
return (Mobile *)NULL;
}
void MobilePrint(Mobile *m)
{
if (m) printf("%s\n%s\n",m->name,m->desc);
}
Mobile MobileMove(Mobile *m, Location *CurrentLoc)
{
if (m && CurrentLoc) m->loc = CurrentLoc;
}
\ No newline at end of file
mobile.h 0 → 100644
#ifndef MOBILE_H
#define MOBILE_H
typedef struct
{
char *name;
char *desc;
Location *loc;
} Mobile;
extern Mobile *MobileNew(char *name, char *desc);
extern Mobile *MobileDelete(Mobile *m);
extern Mobile *MobileMove(Mobile *m, Location *CurrentLoc);
extern void MobilePrint(Mobile *m);
#endif // MOBILE_H
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment