简体   繁体   中英

Pointers and Structs in c?

I'm new here. I'm writing a text-based game and something's not right here. When I run it, the commands get all switched up and I think I'm forgetting to free or reset something, but I'm not sure where or what. I'm a freshman, so the mistake is probably horribly obvious to y'all. I appreciate any help! :)

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include <stdlib.h>


struct Room{
char *roomName;
char *roomDescription;
struct Room *northDoor, *southDoor, *westDoor, *eastDoor;
};

struct Room * makeRoom( const char *roomName, const char *roomDescription )
{
struct Room *r = malloc(sizeof(struct Room));
r->roomName = malloc(strlen(roomName) + 1);
r->roomDescription = malloc(strlen(roomDescription) + 1);

memcpy(r->roomName, roomName, strlen(roomName)+1);
memcpy(r->roomDescription, roomDescription, strlen(roomDescription)+1);
return r;


};

void printRoom( struct Room * room )
{
printf("%s\n", room->roomName);
printf("%s\n", room->roomDescription);
puts("");

}



int main()
{

printf("                         _____                          \n");
printf("            ____....-----'---'-----....____             \n");
printf("========================================================\n");
printf("             ___'---..._________...---'___             \n");
printf("            (___)       _|_|_|_      (___)            \n");
printf("              \\____.-' _.---._'-.____//              \n");
printf("                cccc'.___'---'__.'cccc                 \n");
printf("                       ccccccccc                        \n");

printf("DAMNIT, JIM\n\n");
struct Room *shuttle_door = makeRoom("WARP SHUTTLE DOOR", "You are standing in front of the Danube Class Warp Shuttle door.\nThe Captain's Quarters are to the east.\nThe warp shuttle is to the north.\nThe bridge is to the west.");
struct Room *cpt_qtr = makeRoom("CAPTAIN'S QUARTERS", "You are in the captain's quarters.\nNothing in here will help you escape.\nThe pod door is to the east.");
struct Room *escape_shuttle = makeRoom("WARP SHUTTLE", "Yay?");
struct Room *bridge = makeRoom("THE BRIDGE", "You are in the bridge. Mr. Sulu is frowning at you.\nThe pod door is to the east.");

//Connect the rooms
shuttle_door->eastDoor=cpt_qtr; shuttle_door->southDoor=NULL; shuttle_door->eastDoor=bridge; shuttle_door->northDoor=escape_shuttle;
cpt_qtr->westDoor=shuttle_door; cpt_qtr->northDoor=NULL; cpt_qtr->southDoor=NULL; cpt_qtr->eastDoor=NULL;
bridge->northDoor=NULL; bridge->southDoor=NULL; bridge->eastDoor=shuttle_door; bridge->westDoor=NULL;

//Directions...sort of
puts("The Enterprise has been compromised again; your only hope is to\nleave through the warp shuttle...");
puts("");


struct Room *currentRoom = shuttle_door;

while ( true )
{
    // Print the room description
    printRoom( currentRoom );

    // Game over if we reach the escape pod
    if ( currentRoom == escape_shuttle ) break;

    // Prompt user for command
    char command[1024];
    printf("Enter your command: ");
    scanf(" %1023s",command);
    bool command_recognized = false;

    //this is where stuff gets wonky. I think I need to free or reset something...   ¯\_(ツ)_/¯

        if(strcmp("west", command)==0)
        {
            //if you type west & the current room has a west door, you go in that door
            command_recognized = true;
            if(currentRoom->westDoor != NULL)
            {
                currentRoom = currentRoom->westDoor;
            }

            //asterisks are for readability
            else {puts("You have walked into a wall of Tribbles.");
            puts("********************");}
        }
        else if(strcmp("east", command)==0)
        {

            command_recognized=true;
            if(currentRoom->eastDoor != NULL)
            {
                currentRoom = currentRoom->eastDoor;
            }
            else {puts("You have walked into a wall of Tribbles.");
            puts("********************");}
        }

        else if(strcmp("north", command)==0)
        {
            command_recognized=true;
            if(currentRoom->northDoor != NULL)
            {
                currentRoom = currentRoom->northDoor;
            }
            else {puts("You have walked into a wall of Tribbles.");
            puts("********************");}
        }
        else if(strcmp("south", command)==0)
        {
            command_recognized=true;
            if(currentRoom->southDoor != NULL)
            {
                currentRoom = currentRoom->southDoor;
            }
            else {puts("You have walked into a wall of Tribbles.");
            puts("********************");}
        }


    else
    {
        printf("Communicator Error 502: '%s' not understood.\n", command);
        puts("****************");
    }
    puts("");

}

puts("The game is over!");

}

Are you seeing any issues other than "the commands getting switched up"? If not, the problem might be:

shuttle_door->eastDoor=cpt_qtr; shuttle_door->southDoor=NULL; shuttle_door->eastDoor=bridge; shuttle_door->northDoor=escape_shuttle;

Did you mean to assign eastDoor twice?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM