指针和结构在c?

时间:2016-04-28 04:40:38

标签: c pointers structure

我是新来的。 我正在写一个基于文本的游戏,而且这里有些不对劲。当我运行它时,命令全部打开,我想我忘了释放或重置某些东西,但我不知道在哪里或什么。 我是新生,所以这个错误对于所有人来说可能都是非常明显的。我感谢任何帮助! :)

#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!");

}

1 个答案:

答案 0 :(得分:0)

除了“命令开关”之外,您是否还有其他问题?如果没有,问题可能是:

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

您的意思是两次分配eastDoor吗?