2016-04-28 26 views
-1

ここは新しいです。 私はテキストベースのゲームを書いていますが、ここには何かがありません。私がそれを実行すると、コマンドがすべてスイッチアップされ、私は何かを解放したりリセットしたりするのを忘れていると思うが、どこか何か分からない。 私は新入生ですから、間違いはおそらく皆さんにとって恐ろしいことです。私は助けていただきありがとうございます! :)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

「コマンドがすべてスイッチアップされる」とはどういう意味ですか? – immibis

+0

1) 'shuttle_door-> eastDoor = bridge;' - > 'shuttle_door-> westDoor = bridge;' 'makeRoom''}; ' - >'} '3)'キャプテンのクォーターズ ''ポッドドア東に向かいます。」 - >「ポッドのドアは西にある」 – BLUEPIXY

答えて

0

あなたは "コマンドがアップ切り替える取得" 以外のすべての問題を見ていますか?ない場合は、問題があるかもしれない:

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

あなたは二回eastDoorを割り当てることを意味していましたか?

関連する問題