简体   繁体   English

c 中的指针和结构?

[英]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?你的意思是给eastDoor分配两次吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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