text game movement system

I'm stumped why this won't change locations correctly

Page 1 of 1

2 Replies - 1319 Views - Last Post: 13 January 2009 - 06:24 PM Rate Topic: -----

#1 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

text game movement system

Post icon  Posted 12 January 2009 - 07:42 PM

I can use the debug stage of this program and hard code in any location I want and it works right. If I try to have the state change by user input it is wrong every time and every way I have tried to make this work so far, it just never moves to the correct room.

I've removed all of the extra's I had in and exit conditions and funny messages and it is down to the bare bones code, still no joy.

Could any of you take a look at this and see what is going on? If there is a { or something missing it was missed when pasting the code in here. This runs and compiles on my system with no errors other than never being able to move to the correct room.
[edit]I just realized I am referencing the 'new room' incorrectly with the users 'movement input' but I am stumped still on how to transpose the input movement to the corresponding room ID/array element without kludging something hardcoded again. It's time for a break before my brain melts but I would love to see what you guys have to suggest for a possible fix.[/edit]
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;
#define max_locations 5
#define max_directions 4

struct t_rooms {
   int id_number;
   char *roomname;
};

struct t_locationinfo
{
   int location_id;
   char *description;
   t_rooms room_array[max_directions];
};

t_locationinfo main_locations[max_locations]=
{
    {1, "At your ghetto crib, you see a sidewalk out front.", {
        {2,"sidewalk"},
        {0,""},
        {0,""},
        {0,""}},
    },
    {2, "Sidewalk, standing in front of yo crib. Places to go are back to yo crib, your car, smelly dumpster or the backlot.", {
        {1,"apartment"},
        {3,"dumpster"},
        {4,"car"},
        {5,"backlot"}}
    },
    {3, "At the smelly dumpster, you hear a bum jumping up and down inside. The sidwalk is to the south.",{
        {2,"sidewalk"},
        {0,""},
        {0,""},
        {0,""}},
    },
    {4, "Standing beside yo hooptie, what a nasty ride.  You can go back to the sidewalk from here.",{
        {2,"sidewalk"},
        {0,""},
        {0,""},
        {0,""}},
    },
    {5, "The backlot, here is the vehicle graveyard at your apartments.  You can go back to the sidewalk in front of yo crib.",{
        {2,"sidewalk"},
        {0,""},
        {0,""},
        {0,""}},
    },                   
};

void paint_location(int location_index);
void paint_exits(int location_index);
void dot_progress();
int movement_input(int location_index, int movement_id);


int main(int argc, char *argv[])
{
     char user_input = 'y';
     
    //hard coded debug stage removed for clarity
   // cout << endl<< endl << ("Exiting debug stage. ");
    
    int location_index  = 0;
    int movement_id = 0;
    system("cls");
    
    while(location_index <= max_locations)
    {
        movement_id = 0;
        paint_location(location_index);
        paint_exits(location_index);

        location_index= movement_input(location_index, movement_id);
    }

    //exit condition inactive for debugging
    cout << endl << ("\nThank you for playing our game!  So long, and thanks for all the fish.");
    cout << endl << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

int movement_input(int location_index, int movement_id)
{
    int temp=location_index;
    int valid_exits = 0;
    
    cout << endl << ("\nEnter the room number you would like to move to.");
    cout << ("\nMove to room: ");
    cin >> movement_id;
    
    if(main_locations[location_index].room_array[movement_id].id_number > valid_exits)
    {
        cout << endl << ("\nThis is room number: ") << main_locations[location_index].room_array[movement_id].id_number;
        cout << endl << ("\nThis is room name: ") << main_locations[location_index].room_array[movement_id].roomname;        
        cout << endl << endl;
        system("pause");
        return main_locations[location_index].room_array[movement_id].id_number;
    }
    else 
    {
        cout << endl << ("\nThat is not a valid exit ID.");
        return temp;
    }
}   
   

void paint_location(int location_index)
{
     cout << endl << main_locations[location_index].description;
}

void paint_exits(int location_index)
{
     if(main_locations[location_index].room_array[0].id_number > 0)
     {
         cout << endl << ("\nAvailable exits: ") << endl;
                  
         for(int direction_id=0;direction_id<4;direction_id++)
         {
             if(main_locations[location_index].room_array[direction_id].id_number > 0)
             {
                 cout << endl;
                 cout << main_locations[location_index].room_array[direction_id].roomname << (", exit number: ");                    
                 cout << main_locations[location_index].room_array[direction_id].id_number;                    
             }                 
         }
     }       
     else
     {
         cout << endl << ("I'm sorry but you are dead, you can't move if you dead");
     }
return;
            
}
void dot_progress()
{
     for(int i = 0;i < 5;i++)
     {
         for (int x = 0; x <= 10; x++) {
         Sleep(300);
         printf(".");
         }

         cout << ("\b");
         cout << (" ");
            
         for (int x = 0; x <= 10; x++) 
         {
             Sleep(100);
             int temp = 2;
             while(temp>0)
             {
                 cout << ("\b");
                 temp--;
             } 
             cout << (" ");
         }
         cout << ("\b");
               
     }
return;
}

This post has been edited by badjava: 12 January 2009 - 08:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: text game movement system

#2 matthew180   User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: text game movement system

Posted 12 January 2009 - 08:20 PM

Quickly looking at the code I see a few things:

1. The end of your data array has an extra comma, which I would think should have caused a compiler syntax error.

2. Your room location id's start at 1, but you are using them as array indexes. Doing this is fine, but your id's then need to be numbered as their corresponding array index (which also makes them irrelevant).

3. You start off with location_index = 0 which is okay, and happens to work out. But if I try to go to the sidewalk, your function movement_input() is going to return 2, which you then use as an index which actually puts me at the smelly dumpster.

To separate the array indexes from the actual room id's, you need to "search" for the id, not simply use the id as an index. A function to take and id and return the array index could be used:

/**
 * Finds a room id and returns its index in the main_locations array.
 *
 * @parm[in] location_id  The location id to get an index for.
 *
 * @return The index of the specified room, or -1 if not found.
 */
int
get_location_index(int location_id)
{
	int i;

	for ( i = 0; i < max_locations; i++ )
	{
		if ( main_locations[i].location_id == location_id )
			break;
	}

	// In a complete game you should do some error checking.
	if ( i >= max_locations )
	{
		 // The location id was not found, this must be a data base error or
		 // bug in the code.
		 printf("Ahhh!  You die unexpectedly...  Sorry.\n");
		 i = -1;
	}

	return i;
}
// get_location_index()



Matthew
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: text game movement system

Posted 13 January 2009 - 06:24 PM

Code compiled. However, there seems a basic misunderstanding in this code. That id number does really have anything to do with array index. It's actually one off.

Also, your life would be easier if you just tracked the number of directions. In fact, if you gave your main record an exit name you'd just have to establish one link between two locations...

Anyway, I did a mockup of how I might do this.

#include <cstdlib>
#include <iostream>

using namespace std;

#define max_locations 5
#define max_exit 4

typedef struct t_locationinfo {
	string description;
	int exitCount;
	struct t_connects {
		string name;
		struct t_locationinfo *leadsTo;
	} exits[max_exit];
} LocInfoType;

LocInfoType main_locations[max_locations]= {
	{"At your ghetto crib, you see a sidewalk out front.", 0, {} },
	{"Sidewalk, standing in front of yo crib. Places to go are back to yo crib, your car, smelly dumpster or the backlot.", 0, {} },
	{"At the smelly dumpster, you hear a bum jumping up and down inside. The sidwalk is to the south.", 0, {} },
	{"Standing beside yo hooptie, what a nasty ride.  You can go back to the sidewalk from here.", 0, {} },
	{"The backlot, here is the vehicle graveyard at your apartments.  You can go back to the sidewalk in front of yo crib.", 0, {} }
};

void addExit(LocInfoType &locFrom, string exitName, LocInfoType &leadsTo) {
	locFrom.exits[locFrom.exitCount].name = exitName;
	locFrom.exits[locFrom.exitCount].leadsTo = &leadsTo;
	locFrom.exitCount++;
}

void initConnects() {
	addExit(main_locations[0], "sidewalk", main_locations[1]);
	addExit(main_locations[2], "sidewalk", main_locations[1]);
	addExit(main_locations[3], "sidewalk", main_locations[1]);
	addExit(main_locations[4], "sidewalk", main_locations[1]);

	addExit(main_locations[1], "apartment", main_locations[0]);
	addExit(main_locations[1], "dumpster", main_locations[2]);
	addExit(main_locations[1], "car", main_locations[3]);
	addExit(main_locations[1], "backlot", main_locations[4]);
}

void paint(LocInfoType &loc) {
	cout << endl << loc.description;
	if (loc.exitCount>0) {
		cout << endl << ("\nAvailable exits: ") << endl;          
		for(int i=0; i<loc.exitCount; i++) {
			cout << endl;
			cout << loc.exits[i].name << ", exit number: " << (i + 1);
		}                 
	} else {
		cout << endl << ("I'm sorry but you are dead, you can't move if you dead");
	}
}


int main(int argc, char *argv[]) {
	LocInfoType *currentLocation;
	initConnects();
	currentLocation = &main_locations[0];
	while(currentLocation!=NULL) {
		paint(*currentLocation);
		cout << endl << endl;
		cout << "Enter the room number you would like to move to." << endl;
		cout << "Move to room: ";
		int movement_id;
		cin >> movement_id;
		movement_id--;
		
		if (movement_id==-1) {
			cout << endl << "You admit defeat." << endl;
			currentLocation = NULL;
		} else if (movement_id<0 || movement_id>=currentLocation->exitCount) {
			cout << endl << "That is not a valid exit ID." << endl;
		} else {
			currentLocation = currentLocation->exits[movement_id].leadsTo;
		}
	}
	return EXIT_SUCCESS;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1