# Научный форум dxdy

Математика, Физика, Computer Science, Machine Learning, LaTeX, Механика и Техника, Химия,
Биология и Медицина, Экономика и Финансовая Математика, Гуманитарные науки

 Печатать страницу | Печатать всю тему Пред. тема | След. тема

29/12/10
33
 Privet!I want to check my code, and make it simpler.My code is supposed allow the user to navigate this matrix.The NORTH, SOUTH, EAST,WEST are the boundaries. If the user attempts to move in a direction outside these (N,S,E,W), he gets an error message.Here's a digrammatic representation: ___________________________________________________________|Bd'room 1 |Bd'room 2 |Porch || EAST (Upstairs) |SOUTH, EAST, WEST |WEST ||______________________ |_______________________|___________||Kitchen |Stairs |Ba'room ||SOUTH |NORTH, SOUTH |SOUTH ||_______________________|_______________________|___________ ||Dining | ENTRANCE |Living ||NORTH & EAST |NORTH, EAST, WEST |NORTH, WEST ||_______________________|_______________________|___________|Here's my code for an adventure game:Код:#include #include using namespace std;struct room{    string data;    room* next;    room* prev;};void entrance(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);void stairCase(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);void bedRm(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);void living(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);void masterRm(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);void porch1(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);void kitchen1(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);void bath(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);void dining(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);int main(){       room* start;    room* livingRoom;    room* stairs;    room* masterRoom;    room* bedRoom;    room* porch;    room* kitchen;    room* bathRoom;    room* diningRoom;    cout << "Welcome to the Cabin!!!" << endl;    start = new room;    start->data = "Entrance of Cabin";    cout << start->data << endl;        livingRoom = new room;    start->next = livingRoom;    livingRoom->data = "This is the living room";        diningRoom = new room;    start->next = diningRoom;    diningRoom->data = "This is the dining room";    stairs = new room;    start->next = stairs;    stairs->data = "This is the staircase";        kitchen = new room;    diningRoom->next = kitchen;    kitchen->data = "This is the kitchen";    bathRoom = new room;    livingRoom->next = bathRoom;    bathRoom->data = "This is the bathroom";    bedRoom = new room;    stairs->next = bedRoom;    bedRoom->data = "This is the bedroom (upstairs)";    masterRoom = new room;    bedRoom->next = masterRoom;    masterRoom->data = "This is the master room ";        porch = new room;    bedRoom->next = porch;    porch->data = "This is the porch (upstairs)";        entrance(start, livingRoom, stairs, masterRoom, bedRoom, porch, kitchen, bathRoom, diningRoom);}void entrance(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom){        string userInput;        cout << "You are located in the entrance \n";        cout << "You could only move the in the following directions: \n North,\t East,\t or West \n";        cin >> userInput;        for(int i = 0; i < userInput.length(); i++) {            userInput[i] = toupper(userInput[i]);        }        if (userInput == "N" || userInput == "NORTH") {            cout << stairs->data << endl;            stairCase(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }        if (userInput == "S" || userInput == "SOUTH") {            cout << "I can't move in that direction" << endl;            entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }        if (userInput == "E" || userInput == "EAST") {            cout << livingRoom->data << endl;            living(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }        if (userInput == "W" || userInput == "WEST") {            cout << diningRoom->data << endl;            dining(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }};void stairCase(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom){        string userInput;                cout << "You are in the staircase\n";        cout << "You can only move in the following directions: \n North, \t South" << endl;        cin >> userInput;        for(int i = 0; i < userInput.length(); i++) {            userInput[i] = toupper(userInput[i]);        }        if (userInput == "N" || userInput == "NORTH") {            cout << bedRoom->data << endl;            bedRm(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }        if (userInput == "S" || userInput == "SOUTH") {            cout << start->data << endl;            entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }        if (userInput == "E" || userInput == "EAST") {            cout << "I can't move in that direction" << endl;            entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }        if (userInput == "W" || userInput == "WEST") {            cout << "I can't move in that direction" << endl;            entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }};void bedRm(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom){    string userInput;    cout << "You are in the upstairs bedroom\n";    cout << "You can only move in the following directions: \n South, \t East, \t West" << endl;        cin >> userInput;        for(int i = 0; i < userInput.length(); i++) {            userInput[i] = toupper(userInput[i]);        }        if (userInput == "N" || userInput == "NORTH") {            cout << "I can't move in that direction" << endl;            entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch,kitchen, bathRoom, diningRoom);        }        if (userInput == "S" || userInput == "SOUTH") {            cout << stairs->data << endl;            stairCase(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }        if (userInput == "E" || userInput == "EAST") {            cout << porch->data << endl;            porch1(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }        if (userInput == "W" || userInput == "WEST") {            cout << masterRoom->data << endl;            masterRm(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);        }};void masterRm(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom){    string userInput;        cout << "You are in the upstairs master room\n" ;    cout << "You can only move in the following directions: \n East" << endl;        cin >> userInput;    for(int i = 0; i < userInput.length(); i++) {        userInput[i] = toupper(userInput[i]);    }    if (userInput == "N" || userInput == "NORTH") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "S" || userInput == "SOUTH") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch,kitchen, bathRoom, diningRoom);    }    if (userInput == "E" || userInput == "EAST") {        cout << bedRoom->data << endl;        bedRm(start, livingRoom, stairs,masterRoom, bedRoom,porch,kitchen, bathRoom, diningRoom);    }    if (userInput == "W" || userInput == "WEST") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch,kitchen, bathRoom, diningRoom);    }};void kitchen1(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom){    string userInput;    cout << "You are in the kitchen \n";    cout << "You can only move in the following directions: \n South" << endl;        cin >> userInput;    for(int i = 0; i < userInput.length(); i++) {        userInput[i] = toupper(userInput[i]);    }    if (userInput == "N" || userInput == "NORTH") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom); //make method for bedroom    }    if (userInput == "S" || userInput == "SOUTH") {        cout << diningRoom->data << endl;        dining(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "E" || userInput == "EAST") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "W" || userInput == "WEST") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }};void dining(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom){    string userInput;    cout << "You are in the dining room" ;    cout << "You can only move in the following directions: \n North, \t East" << endl;       cin >> userInput;    for(int i = 0; i < userInput.length(); i++) {        userInput[i] = toupper(userInput[i]);    }    if (userInput == "N" || userInput == "NORTH") {        cout << kitchen->data << endl;        kitchen1(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "S" || userInput == "SOUTH") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "E" || userInput == "EAST") {        cout << start->data << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "W" || userInput == "WEST") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }};void living(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom){    string userInput;    cout << "You are in the living room" << endl;    cout << "You can only move in the following directions: \n North, \t West" << endl;    cin >> userInput;    for(int i = 0; i < userInput.length(); i++) {        userInput[i] = toupper(userInput[i]);    }    if (userInput == "N" || userInput == "NORTH") {        cout << bathRoom->data << endl;        bath(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "S" || userInput == "SOUTH") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "E" || userInput == "EAST") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "W" || userInput == "WEST") {        cout << start->data << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }};void bath(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom){    string userInput;        cout << "You are in the bathroom" << endl;    cout << "YOu can only move in the following directions: \n South" << endl;        cin >> userInput;    for(int i = 0; i < userInput.length(); i++) {        userInput[i] = toupper(userInput[i]);    }    if (userInput == "N" || userInput == "NORTH") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "S" || userInput == "SOUTH") {        cout << livingRoom->data << endl;        living(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "E" || userInput == "EAST") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "W" || userInput == "WEST") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }};void porch1(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom){    string userInput;        cout << "You are on the porch" << endl;    cout << "You can only move in the following directions: \n West" << endl;        cin >> userInput;    for(int i = 0; i < userInput.length(); i++) {        userInput[i] = toupper(userInput[i]);    }    if (userInput == "N" || userInput == "NORTH") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "S" || userInput == "SOUTH") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "E" || userInput == "EAST") {        cout << "I can't move in that direction" << endl;        entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }    if (userInput == "W" || userInput == "WEST") {        cout << bedRoom->data << endl;        bedRm(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);    }};Spasibo!

 Заслуженный участник

06/10/08
6313
 Последний раз редактировалось Xaositect 09.11.2013, 16:50, всего редактировалось 3 раз(а). Well, if you have 9 functions that all do almost the same, you should try to make one function that can replace them.All your functions for rooms have the following structure:Код:void ...(...){    string userInput;    // output a message telling the player where he is    // output a message telling the player where he can go    cin >> userInput;    for(int i = 0; i < userInput.length(); i++) {        userInput[i] = toupper(userInput[i]);    }    if (userInput == "N" || userInput == "NORTH") {        //go north if we can    }    if (userInput == "S" || userInput == "SOUTH") {        //go south if we can    }    if (userInput == "E" || userInput == "EAST") {        //go east if we can    }    if (userInput == "W" || userInput == "WEST") {        //go west if we can    }};So what do we need to make a function that does this uniformly? In this function we need to know:1. what is the current room and how to describe it to the player2. which paths are available3. to which rooms are these paths taking the playerMaybe we should add this data to the room structure:Код:struct room {    string data;    room* next;    room* prev;    string description;    bool north;    room* northRoom;    bool south;    room* southRoom;    bool east;    room* eastRoom;    bool west;    room* westRoom;}Actually, do we really need the prev and next pointers? prev isn't used anywhere, and next used only in the initialization code, we set it and never use it again. Also, if you need a list, you really should use list or vector from standard library.Код:struct room {    string data;    string description;    bool north;    room* northRoom;    bool south;    room* southRoom;    bool east;    room* eastRoom;    bool west;    room* westRoom;}And again we have 4 pieces of code which the same structure and the same purpose. Let's make it an array.Код:struct room {    string data;    string description;    bool openPaths[4];    room* neighbors[4];}Actually we do not need this bool array. We can use NULL pointers to say that the specific path is closedКод:struct room {    string data;    string description;    room* neighbors[4];}Let's try to write the room processing function.Код:void room(room *room){    string userInput;    cout << room->description << endl;    cout << "You can move in the following directions:" << endl;    if (room->neighbors[0] != NULL)       cout << "North \t";    if (room->neighbors[1] != NULL)       cout << "South \t";    if (room->neighbors[2] != NULL)       cout << "East \t";    if (room->neighbors[3] != NULL)       cout << "West \t";    cin >> userInput;    for(int i = 0; i < userInput.length(); i++) {        userInput[i] = toupper(userInput[i]);    }    if (userInput == "N" || userInput == "NORTH") {        if (room->neighbors[0] != NULL) {            cout << room->neighbors[0]->data << endl;            location(room->neighbors[0]);        } else {            cout << "I can't move in that direction" << endl;            location(room);        }    }    if (userInput == "S" || userInput == "SOUTH") {        if (room->neighbors[1] != NULL) {            cout << room->neighbors[1]->data << endl;            location(room->neighbors[1]);        } else {            cout << "I can't move in that direction" << endl;            location(room);        }    }    if (userInput == "E" || userInput == "EAST") {        if (room->neighbors[2] != NULL) {            cout << room->neighbors[2]->data << endl;            location(room->neighbors[2]);        } else {            cout << "I can't move in that direction" << endl;            location(room);        }    }    if (userInput == "W" || userInput == "WEST") {        if (room->neighbors[3] != NULL) {            cout << room->neighbors[3]->data << endl;            location(room->neighbors[3]);        } else {            cout << "I can't move in that direction" << endl;            location(room);        }    }};Umm.It looks worse than before. Again we have 4 blocks that do exactly the same. Also, there are these indices which are too easy to misplace... What does 2 means again, east or west? Let's make a type for cardinal directions and a function to convert user input to this type.Код:enum direction {    NORTH = 0,    SOUTH = 1,    EAST = 2,    WEST = 3,    END_DIRECTION = 4,}direction input() {    cin >> userInput;    for(int i = 0; i < userInput.length(); i++) {        userInput[i] = toupper(userInput[i]);    }    if (userInput == "N" || userInput == "NORTH") {        return NORTH;    }    if (userInput == "S" || userInput == "SOUTH") {        return SOUTH;    }    if (userInput == "E" || userInput == "EAST") {        return EAST;    }    if (userInput == "W" || userInput == "WEST") {        return WEST;    }}Now we can make our main function much nicer:Код:void room(room *room){    string userInput;    cout << room->description << endl;    cout << "You can move in the following directions:" << endl;    if (room->neighbors[NORTH] != NULL)       cout << "North \t";    if (room->neighbors[SOUTH] != NULL)       cout << "South \t";    if (room->neighbors[EAST] != NULL)       cout << "East \t";    if (room->neighbors[WEST] != NULL)       cout << "West \t";    direction dir = input();    if (room->neighbors[dir] != NULL) {        cout << room->neighbors[dir]->data << endl;        location(room->neighbors[dir]);    } else {        cout << "I can't move in that direction" << endl;        location(room);    }};I think you can continue on yourself in this direction. For example, the 4 blocks which output possible directions to move can be unified in one loop. Also notice that data field in room structure is used only before location function, so maybe we should move it there.I also didn't say anything about initialization code: now you need to set up room->neighbors pointers. Actually, the best thing to do is to realize that now that we have function that can process arbitrary room maze, the information about rooms and paths is data: it can be stored not in the program, but in the text file, so we can change the layout of our adventure without changing and recompiling the program.-- Чт ноя 07, 2013 20:12:26 --Also, think what happens when the player enters "UP" or something, and what to do about it.

14/01/11
2369
 Room structures can be initialized as constants. Also, we can include a function pointer to the room structure in order to do something special if it is needed. Here is some code for illustration: код: [ скачать ] [ спрятать ]код: [ скачать ] [ показать ]Используется синтаксис C++ #include #include using namespace std; char* directions[]={"North", "South", "East", "West"}; struct room {     char* description;         room*  exits[4];//north,south,east,west         void* somedata;         void (*somefunction)(); }; extern room entrance, livingroom, diningRoom, staircase, kitchen; room entrance = { "Entrance of Cabin\n\ You are located at the entrance\n", {&staircase,NULL,&livingroom,&diningRoom}, NULL, NULL, }; room livingroom = { "This is the living room\n\ You are in the living room\n", {NULL,NULL,NULL,&entrance}, NULL, NULL, }; room diningRoom = {         "This is the dining room\n\ You are in the dining room\n", {NULL,NULL,&entrance,NULL}, NULL, NULL, }; room staircase = {         "This is the staircase\n\ You are in the staircase\n", {NULL,&entrance,NULL,&kitchen}, NULL, NULL, }; int numofapples=1; void kitchenfunc(void) {     if (numofapples>0)     {         cout<<"There is an apple on the table. Take it? (Y/N)";         string userInput;         cin >> userInput;             for(int i = 0; i < userInput.length(); i++)             userInput[i] = toupper(userInput[i]);          if (userInput == "Y" )          {                numofapples--;                cout<<"You take the apple\n";          }          if (userInput == "N" )          cout<<"You decide not to take the apple\n";                 } } room kitchen = {         "This is the kitchen\n\ You are in the kitchen\n", {NULL,NULL,&staircase,NULL}, &numofapples, kitchenfunc, }; int main(int argc, char *argv[]) {         string userInput;                 cout << "Welcome to the Cabin!!!" << endl;         room* CurrentRoom=&entrance;         while (true)         {                 cout<description;                             if (CurrentRoom->somefunction!=NULL)             CurrentRoom->somefunction();                 cout<<"You could only move in the following directions:\n";                 int exnum=0;                 for (int i=0;i<4;i++)                         if (CurrentRoom->exits[i]!=NULL)                         {                                 if (exnum)                                         cout<<", ";                                 cout<> userInput;             for(int i = 0; i < userInput.length(); i++)             userInput[i] = toupper(userInput[i]);                 room* TryRoom=CurrentRoom;                 if (userInput == "QUIT" || userInput == "EXIT")                         break;                 if (userInput == "N" || userInput == "NORTH")                         TryRoom=CurrentRoom->exits[0];                 if (userInput == "S" || userInput == "SOUTH")                         TryRoom=CurrentRoom->exits[1];                 if (userInput == "E" || userInput == "EAST")                         TryRoom=CurrentRoom->exits[2];                 if (userInput == "W" || userInput == "WEST")                         TryRoom=CurrentRoom->exits[3];                 if (TryRoom==NULL)                         cout<<"I can't move in that direction\n";                 else                         CurrentRoom=TryRoom;         }         return 0; }

29/12/10
33
 Последний раз редактировалось CrypticMath 11.11.2013, 15:18, всего редактировалось 1 раз. Thank you, Xaositect!-- Пн ноя 11, 2013 15:18:12 --Thank you, Sender!

 Показать сообщения за: Все сообщения1 день7 дней2 недели1 месяц3 месяца6 месяцев1 год Поле сортировки АвторВремя размещенияЗаголовок по возрастаниюпо убыванию
 Страница 1 из 1 [ Сообщений: 4 ]

Модераторы: Karan, maxal, Toucan, PAV, Супермодераторы

#### Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей

 Вы не можете начинать темыВы не можете отвечать на сообщенияВы не можете редактировать свои сообщенияВы не можете удалять свои сообщенияВы не можете добавлять вложения

 Найти: