Sign in to follow this  

should i do it this way?(C++)

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just wondering, (I have thought and thought and I cannot seem to figure out how to do this properly)I am trying to make a simple txt game and I was thinking maybe I should set a variable for every area and then when that variable = true then i set the old room to false and I just let the player carry on from there. A few problems, 1) I should use pointers for this? 2) Is this the way to do it, or am I just a moron? I just want to start out with like a game with 1 wepon 1 thing of armor like 3 rooms, a couple of enemys, and that would set me good for now. But I cannot even achieve such a small feat as this. I need help! Thanks, death

Share this post


Link to post
Share on other sites
im still fairly new at this too but here is how it somes to mind for me and i may be wrong. i would create an array of room structs or classes. then have a variable in the class or struct that is a boolean value or a global var that holds the index number of the current roon in the array.

like this
ROOM rooms[5]; // array of 5 rooms 0 = foyer 1 = kitchen 2 = blah blah
int current_room = 0; // set the current room to 0 element or the foyer ..

this may not be the way to do it but its a suggestion.

Good luck

Share this post


Link to post
Share on other sites
I think you could use an array to have a map.


//this gives you nine rooms
bool rooms[3][3];
int playerposx;
int playerposy;

//check to make sure that the current players position is set to true.
if (rooms[playerposx][playerposy] = false)
rooms[playerposx][playerposy] = true;


//you could also set the previous player position to false
//when you set the current one to true just have variables
//for the previous positions as well.



Share this post


Link to post
Share on other sites
I agree with the array/map ideas.
By the way, are you familiar with concepts like classes, functions, arrays,
not to mention the game-loop?

EDIT:
if (rooms[playerposx][playerposy] = false)
if (rooms[playerposx][playerposy] == false)

These lines has very different effect ^^

Share this post


Link to post
Share on other sites
here is something i just did .. again im havent done anything like this but its a start ...

in this map rooms are joined by '|' or '-' .
for example you get to room 4 from room 0 or room 5 but not from 7 or 3.

/*
[6]-[7]-[8]
| |
[3] [4]-[5]
| | |
[1]-[0]-[2]

*/

struct ROOM
{
char name[10];
char description[30];
int ID;
int north; // holds the element or id # of the room to the north
int south; // holds the element or id # of the room to the south
int west; // west
int east; // east
};


something like this maybe? you can add functionality for items and enemies to be inside.

*doh my map didnt come out the way i had it so it may not make much sense now lol

Share this post


Link to post
Share on other sites
Quote:
Original post by pulpfist
I agree with the array/map ideas.
By the way, are you familiar with concepts like classes, functions, arrays,
not to mention the game-loop?

EDIT:
if (rooms[playerposx][playerposy] = false)
if (rooms[playerposx][playerposy] == false)

These lines has very different effect ^^


classes no, functions some, arrays no, game loop yes :)

I understand the difference, the first one sets whatever said to be true, while the second one checks to see if what said b4 is true.

i.e.

alive = true;

if (alive == true) {
cout<<"You are alive\n";
} else{
cout<<"you are dead.\n";
}

just an example, i know i didnt delcare the variable or anything but thats the jist of it. (Just trying to show you I understand :) )

Share this post


Link to post
Share on other sites
Quote:
Original post by rherm23
here is something i just did .. again im havent done anything like this but its a start ...

in this map rooms are joined by '|' or '-' .
for example you get to room 4 from room 0 or room 5 but not from 7 or 3.

/*
[6]-[7]-[8]
| |
[3] [4]-[5]
| | |
[1]-[0]-[2]

*/

struct ROOM
{
char name[10];
char description[30];
int ID;
int north; // holds the element or id # of the room to the north
int south; // holds the element or id # of the room to the south
int west; // west
int east; // east
};


something like this maybe? you can add functionality for items and enemies to be inside.

*doh my map didnt come out the way i had it so it may not make much sense now lol



ok but how do i link the fact that i go north to get from a to b etc..?

ex how does the program know that I may get to 3 from 1 but not 5 to 6?

Share this post


Link to post
Share on other sites
I dont want to "dictate" your game design, but as a small example to get started:



ROOM rooms[5][5];
int player_row=2, player_col=2;

int main()
{
while(running)
{
if(keydown['s']
if(player_row < 4)
player_row++;

...

}
return 0;
}





Something like this will let you take one step at the time in four different directions.

This is only pseudo code, maybe its better if you show what you got so far...

Share this post


Link to post
Share on other sites
this is for linking rooms

you have a room struct i gave earlier .. and a map.

we will initialize room 0 in our array of rooms for an example...

int current_room = 0;

ROOM house[9]; // 9 rooms in our house
house[0].name = "entry";
house[0].description = "a small entry";
house[0].north = 4;
house[0].south = -1; // no room to the south
house[0].east = 2;
house[0].west = 1;
.... do this for all your rooms in the array so house[1] and so on

then to check if the next room is open to the player...
lets say that the button for north was hit and the player wants to go north

if(house[current_room].north != -1)
{
current_room = house[current_room].north;
}
else
{
cout << "There is no door that way." << endl;
}

hope that cleared up some of my mess heh. like i said i just started this stuff when i read your post and someone else might have a better solution.

[Edited by - rherm23 on December 19, 2005 2:52:13 PM]

Share this post


Link to post
Share on other sites
Quote:
ok but how do i link the fact that i go north to get from a to b etc..?

ex how does the program know that I may get to 3 from 1 but not 5 to 6?



You could do this with a class, where you could store a true/false value for north, south, east and west.

Say you had an object named room1, and directional attributes were boolean values.
room1.north = true;
room1.south = false;
room1.east = false;
room1.west = false;

You could then define each room individually based on your map.

Using classes, you could hold a large amount of data for each room in a similar manner. However, you will also need functions to interpret the data. Having a value that says you can't go south is only valid if it keeps you from going south. =)

Share this post


Link to post
Share on other sites
i hope this helps .... and also how do i put my code into blocks so it doesnt take up soo much space.

this is very crude and i just now did it so it may be very ugly lol and crude.

i tested it and it works as far as moving from room to room using wasd keys

w = north
a = west
s = south
d = east

textgame.h


#ifndef _TEXT_GAME_H
#define _TEXT_GAME_H

#include <cstring>


/*
[6]-[7]-[8]
| |
[3] [4]-[5]
| | |
[1]-[0]-[2]

*/


struct ROOM
{
std :: string name;
std :: string description;
int north; // holds the element or id # of the room to the north
int south; // holds the element or id # of the room to the south
int west; // west
int east; // east
};

struct PLAYER
{
int position; // should always be the same as current_room .. may even replace it
int health;
int alive;
};

ROOM house[9];
PLAYER person;

void init_game();
void run_game();
void end_game();
void getInput();
void wrongway();
void showRoom();


#endif






//////////////////////////
main.cpp



#include <iostream>
#include <stdlib.>

#include "textgame.h"

using namespace std;

int main(int argc, char *argv[])
{
init_game();
run_game();
end_game();
system("PAUSE");
return 0;
}

void init_game()
{
// init person
person.health = 100;
person.position = 0;
person.alive = 1;

// init house
house[0].name = "Entrance";
house[0].description = "entryway";
house[0].north = 4;
house[0].east = 2;
house[0].west = 1;
house[0].south = -1;

house[1].name = "Den";
house[1].description = "den";
house[1].west = -1;
house[1].south = -1;
house[1].east = 0;
house[1].north = 3;

house[2].name = "Foyer";
house[2].description = "foyer";
house[2].east = -1;
house[2].west = 0;
house[2].north = 5;
house[2].south = -1;

house[3].name = "Hallway";
house[3].description = "hallway";
house[3].east = -1;
house[3].west = -1;
house[3].south = 1;
house[3].north = 6;

house[4].name = "RecRoom";
house[4].description = "RecRoom";
house[4].south = 0;
house[4].north = -1;
house[4].west = -1;
house[4].east = 5;

house[5].name = "Hall";
house[5].description = "hall";
house[5].east = -1;
house[5].west = 4;
house[5].north = 8;
house[5].south = 2;

house[6].name = "Kitchen";
house[6].description = "kitchen";
house[6].east = 7;
house[6].north = -1;
house[6].south = 3;
house[6].west = -1;

house[7].name = "Bathroom";
house[7].description = "A small bathroom";
house[7].east = 8;
house[7].north = -1;
house[7].south = -1;
house[7].west = 6;

house[8].name = "Bedroom";
house[8].description = "bedroom";
house[8].east = -1;
house[8].north = -1;
house[8].south = 5;
house[8].west = 7;

};

////////////////////////////////////////////

void run_game()
{
while(person.alive)
{
system("CLS");
showRoom();
getInput();
}
}

///////////////////////////////////////////

void end_game()
{
cout &lt;&lt; "Game Over!!!" &lt;&lt; endl;
}

///////////////////////////////////////////

void getInput()
{
char choice;

cout &lt;&lt; " choose your direction ..." &lt;&lt; endl;
cin &gt;&gt; choice;

switch(choice)
{
case 'w':
case 'W':
if(house[person.position].north != -1)
{
person.position = house[person.position].north;
}
else
{
wrongway();
}
break;

case 's':
case 'S':
if(house[person.position].south != -1)
{
person.position = house[person.position].south;
}
else
{
wrongway();
}
break;

case 'd':
case 'D':
if(house[person.position].east != -1)
{
person.position = house[person.position].east;
}
else
{
wrongway();
}
break;

case 'a':
case 'A':
if(house[person.position].west != -1)
{
person.position = house[person.position].west;
}
else
{
wrongway();
}
break;

default:
cout &lt;&lt; "Invalid Entry" &lt;&lt; endl;
break;
}

}

///////////////////////

void wrongway()
{
cout &lt;&lt; "There is no path in that direction." &lt;&lt;endl;
}

///////////////////////

void showRoom()
{
cout &lt;&lt; house[person.position].description &lt;&lt; endl;
}






like i said i hope that seeing it can help ya a bit more than us trying to explain it.

[Edited by - rherm23 on December 19, 2005 3:55:18 PM]

Share this post


Link to post
Share on other sites

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this