Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct room{
- char name[ 32 ];
- struct room * north;
- struct room * south;
- struct room * east;
- struct room * west;
- char visited;
- int type;
- struct item * thing;
- };
- struct item{
- char name[ 32 ];
- int type;
- int points;
- };
- struct player{
- struct room * location;
- int turns;
- int points;
- };
- void describe_room( struct room * );
- void wrap_up( struct player * );
- int process_room_type( struct player * );
- int main( int argc, char * argv[] ){
- int loop = 1;
- char command = 'q';
- struct item * coffee_maker = ( struct item * )malloc( sizeof( struct item ) );
- struct item * penny_of_doom = ( struct item * )malloc( sizeof( struct item ) );
- struct room * room01 = ( struct room * )malloc( sizeof( struct room ) );
- struct room * room02 = ( struct room * )malloc( sizeof( struct room ) );
- struct room * room03 = ( struct room * )malloc( sizeof( struct room ) );
- struct room * room04 = ( struct room * )malloc( sizeof( struct room ) );
- struct room * room05 = ( struct room * )malloc( sizeof( struct room ) );
- struct room * room06 = ( struct room * )malloc( sizeof( struct room ) );
- struct room * room07 = ( struct room * )malloc( sizeof( struct room ) );
- struct room * room08 = ( struct room * )malloc( sizeof( struct room ) );
- struct room * room09 = ( struct room * )malloc( sizeof( struct room ) );
- struct player * p = ( struct player * )malloc( sizeof( struct player ) );
- p->location = room01;
- p->turns = 0;
- p->points = 0;
- strcpy( coffee_maker->name, "Coffee Maker+1" );
- coffee_maker->type = 0;
- coffee_maker->points = 5;
- strcpy( penny_of_doom->name, "dangerous looking penny" );
- penny_of_doom->type = 1;
- penny_of_doom->points = -5;
- strcpy( room01->name, "Room One" );
- room01->north = NULL;
- room01->south = room04;
- room01->east = room02;
- room01->west = NULL;
- room01->visited = 'f';
- room01->type = 0;
- room01->thing = NULL;
- strcpy( room02->name, "Room Two" );
- room02->north = NULL;
- room02->south = room05;
- room02->east = room03;
- room02->west = room01;
- room02->visited = 'f';
- room02->type = 0;
- room02->thing = NULL;
- strcpy( room03->name, "Room Three" );
- room03->north = NULL;
- room03->south = NULL;
- room03->east = NULL;
- room03->west = room02;
- room03->visited = 'f';
- room03->type = 0;
- room03->thing = coffee_maker;
- strcpy( room04->name, "Room Four" );
- room04->north = room01;
- room04->south = NULL;
- room04->east = room05;
- room04->west = NULL;
- room04->visited = 'f';
- room04->type = 0;
- room04->thing = NULL;
- strcpy( room05->name, "Room Five" );
- room05->north = room02;
- room05->south = room08;
- room05->east = room06;
- room05->west = room04;
- room05->visited = 'f';
- room05->type = 3;
- room05->thing = penny_of_doom;
- strcpy( room06->name, "Room Six" );
- room06->north = NULL;
- room06->south = NULL;
- room06->east = NULL;
- room06->west = room05;
- room06->visited = 'f';
- room06->type = 0;
- room06->thing = NULL;
- strcpy( room07->name, "Room Seven" );
- room07->north = NULL;
- room07->south = NULL;
- room07->east = NULL;
- room07->west = NULL;
- room07->visited = 'f';
- room07->type = 2;
- room07->thing = NULL;
- strcpy( room08->name, "Room Eight" );
- room08->north = room05;
- room08->south = NULL;
- room08->east = room09;
- room08->west = NULL;
- room08->visited = 'f';
- room08->type = 0;
- room08->thing = NULL;
- strcpy( room09->name, "Room Nine" );
- room09->north = NULL;
- room09->south = NULL;
- room09->east = NULL;
- room09->west = room08;
- room09->visited = 'f';
- room09->type = 1;
- room09->thing = NULL;
- while( loop ){
- loop = process_room_type( p );
- if( !loop ){
- break;
- }
- if( p->turns >= 10 ){
- break;
- }
- describe_room( p->location );
- printf( "What do you want to do? " );
- fflush( stdin );
- command = getchar();
- switch( command ){
- case 'q' :
- printf( "Thanks for playing!\n" );
- loop = 0;
- break;
- case 'd' :
- p->location->visited = 'f';
- describe_room( p->location );
- break;
- case 'n' :
- if( p->location->north ){
- p->location = p->location->north;
- }else{
- printf( "You can't go that way!\n" );
- }
- break;
- case 's' :
- if( p->location->south ){
- p->location = p->location->south;
- }else{
- printf( "You can't go that way!\n" );
- }
- break;
- case 'e' :
- if( p->location->east ){
- p->location = p->location->east;
- }else{
- printf( "You can't go that way!\n" );
- }
- break;
- case 'w' :
- if( p->location->west ){
- p->location = p->location->west;
- }else{
- printf( "You can't go that way!\n" );
- }
- break;
- case 't' :
- if( p->location->thing ){
- p->points = p->points + p->location->thing->points;
- if( p->location->thing->type == 1 ){
- printf( "You feel a little weird.\n" );
- p->location->thing = NULL;
- p->location = room07;
- }else{
- p->location->thing = NULL;
- }
- }
- break;
- default:
- printf( "Please enter a valid command (n, s, e, w, t, d, or q to quit).\n" );
- break;
- }
- p->turns++;
- }
- wrap_up( p );
- return 0;
- }
- int process_room_type( struct player * p ){
- int rv = 1; // continue
- struct room * temp = NULL;
- if( p->location->type == 1 ){
- printf( "You have won!\n" );
- p->points = p->points + 10;
- rv = 0;
- }else if( p->location->type == 2 ){
- printf( "You have died!\n" );
- p->points = p->points - 5;
- rv = 0;
- }else if( p->location->type == 3 ){
- // The spinny room
- printf( "You feel a little dizzy!\n" );
- temp = p->location->north;
- p->location->north = p->location->east;
- p->location->east = p->location->south;
- p->location->south = p->location->west;
- p->location->west = temp;
- }
- return rv;
- }
- void wrap_up( struct player * p ){
- printf( "You played for %d turns\n", p->turns );
- if( p->turns > 9 ){
- p->points = p->points - 5;
- printf( "You lost 5 points for being too slow!\n" );
- }
- printf( "You scored: %d points!\n", p->points );
- }
- void describe_room( struct room * r ){
- printf( "You are in %s\n", r->name );
- if( r->visited == 'f' ){
- if( r->north ){
- printf( "There is a passage to the north\n" );
- }
- if( r->south ){
- printf( "There is a passage to the south\n" );
- }
- if( r->east ){
- printf( "There is a passage to the east\n" );
- }
- if( r->west ){
- printf( "There is a passage to the west\n" );
- }
- r->visited = 't';
- }
- if( r->thing ){
- printf( "There is a %s here.\n", r->thing->name );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement