2016-04-28 11 views
-1

Ich bin neu hier. Ich schreibe ein textbasiertes Spiel und hier stimmt etwas nicht. Wenn ich es ausführe, werden die Befehle alle geschaltet und ich denke, dass ich vergessen habe, etwas freizugeben oder zurückzusetzen, aber ich bin mir nicht sicher, wo oder was. Ich bin ein Neuling, also ist der Fehler wahrscheinlich schrecklich für euch alle offensichtlich. Ich schätze jede Hilfe! :)Zeiger und Strukturen in c?

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

}

+0

Was bedeutet "die Befehle werden alle hochgeschaltet"? – immibis

+0

1) 'shuttle_door-> eastDoor = Brücke;' -> 'shuttle_door-> westDoor = Brücke;' 2) bei 'makeRoom'};' -> '}' 3) bei 'CAPTAIN'S QUARTERS' 'Die Gondeltür ist im Osten. -> 'Die Gondel ist im Westen .' – BLUEPIXY

Antwort

0

Sind Sie Probleme andere als zu sehen, "die Befehle immer eingeschaltet up"? Wenn nicht, könnte das Problem sein:

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

Meinen Sie eastDoor zweimal zuweisen?