Development Topic: Text Game

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Martijn
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Thu Jun 04, 2009 4:47 pm
Programming Language of Choice: C++

Re: Development Topic: Text Game

Post by Martijn »

eeeeeeeeedit:

Lucas helped me on MSN, and it now works perfect :).
I just "mapped" some placed and it's really confusing atm but w/e :p.


Here is a screenshot of how it looks now:

Image

See the compass? Well it looks great in my opinion but I want it at the bottom like this:

Image


I know it's in dutch, but look at the bottom, he got the navigation stuff there. How can I do that? Or is that way to complicated? Because if I got it there I can just simply edit it, and not mess with spaces each time :p.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Development Topic: Text Game

Post by MarauderIIC »

Output a bunch of newlines. But I'm not sure how you would position the input cursor.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Martijn
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Thu Jun 04, 2009 4:47 pm
Programming Language of Choice: C++

Re: Development Topic: Text Game

Post by Martijn »

If I would unput lines until the bottom of the page, i would need to CLS each time.. which I don't want :p.
It looks like the divided his console into 2 parts, no idea how he did it tho :o
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Development Topic: Text Game

Post by Bakkon »

If you're programming in Windows, <windows.h> has some nifty functions for cursor control.

Code: Select all

void gotoxy(int x, int y)
{
	HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE);

	COORD position;
	position.X = x;
	position.Y = y;

	SetConsoleCursorPosition(output_handle, position);
}
Make sure to search MSDN for anything that looks odd to you. This should allow you to jump down a view lines, draw your compass, then jump back up to ask for a direction.
User avatar
Martijn
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Thu Jun 04, 2009 4:47 pm
Programming Language of Choice: C++

Re: Development Topic: Text Game

Post by Martijn »

@Bakkon: Meh, i'm not really getting it but thats okay got other things to worry about atm :p.


Like the walking, it works but you can move EVERYWHERE and I want to be able to say for each map exits = north, west
so you can only leave the map to the north and west! Anyone got an idea how to do that?
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Development Topic: Text Game

Post by MarauderIIC »

Martijn wrote:...I want to be able to say for each map exits = north, west
so you can only leave the map to the north and west! Anyone got an idea how to do that?
I'm not going to pull any punches here :) Normally I don't post solutions, but I don't feel creative enough to make you think.

So, yes. Make a Room class and a Direction enum.

Code: Select all

enum Direction {
    NORTH,
    EAST,
    SOUTH,
    WEST,
    NUM_DIRECTIONS
};

class Room {
/* ... */
    int index;
    Room* exits[NUM_DIRECTIONS]; //contains pointers to Rooms
    int exitNums[NUM_DIRECTIONS]; //contains index #s of Rooms
/* ... */
};
In the constructor for Room, initialize all exits to NULL and all exitNums to -1. Load all the Rooms, leave the contents of exits as NULL but store the index of each exit to in the appropriate entry in exitNums. Once all the Rooms are loaded, go through every Room and search for the index of the Room that matches this Room's exitNums entry that we were searching for, skipping any exitNums entries that are -1. Assign exits[appropriateDirection] to the Room we found that has the index that matches exitNums[appropriateDirection]. When outputting exits, loop through Directions on exits. If exits[appropriateDirection] == NULL, don't output the direction as a possible exit, and prohibit movement in that direction. If not NULL, set Player's currentRoom to exits[appropriateDirection].

Obviously, I've done this before.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Development Topic: Text Game

Post by Ginto8 »

Martijn wrote:eeeeeeeeedit:

Lucas helped me on MSN, and it now works perfect :).
I just "mapped" some placed and it's really confusing atm but w/e :p.


Here is a screenshot of how it looks now:

-big_image-

See the compass? Well it looks great in my opinion but I want it at the bottom like this:

-big_image-


I know it's in dutch, but look at the bottom, he got the navigation stuff there. How can I do that? Or is that way to complicated? Because if I got it there I can just simply edit it, and not mess with spaces each time :p.
perhaps <iomanip> is what you're looking for... if not, try <windows.h> stuff
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Martijn
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Thu Jun 04, 2009 4:47 pm
Programming Language of Choice: C++

Re: Development Topic: Text Game

Post by Martijn »

Okay. I tried to understand, I really did been googling for days but I don't get it ;/.
I understand the enum part, but the rest confuses me a little.

If someone got time, could they care to explain please?
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Development Topic: Text Game

Post by MarauderIIC »

Your rooms have index #s.
Your file has descriptions of the rooms, and each index# that each room should exit to.

Start your program.
Load all the rooms.
Replace each room's "exit index#"s with pointers to the rooms that have those index numbers.

Allow the player to play.

To output the exits, iterate through the exits array (which contains pointers). If the entry is null, don't output it as a possible exit.

When the player chooses a direction to exit, associate that direction with an index in the exits array -- each room's exits are in the same order (north, east, south, west). Use their choice as an index into the exits array. If the entry is null, output "can't go that way." If the entry is not null, replace the pointer to the player's current room with the pointer to the appropriate exit, so now the pointer to the player's current room and the pointer to the appropriate exit point to the same room.

This is an example of the move code:

Code: Select all

Room* check;
if (myPlayerInput == "north")
    check = myPlayer.currentRoom.exits[NORTH];
else if (myPlayerInput == "east")
    check = myPlayer.currentRoom.exits[EAST];\
/* repeat for other directions */

myPlayer.move(check);

Code: Select all

Player::move(Room* room) {
    if (room == NULL) {
        cout << "Can't go that way" << endl;
    } else {
        this->currentRoom = room;
        this->look();
    }
}
This is an example of the code that replaces all index #s with pointers:

Code: Select all

class Room {
    int exitIndexes[NUM_DIRECTIONS];
    Room* exits[NUM_DIRECTIONS];
};

Code: Select all

class Game {
    vector<Room*> rooms;
    void loadRooms(string filename);
    void dereferenceRooms();
};

Code: Select all

Game::dereferenceRooms() {
    vector<Room*>::iterator curRoom;
    vector<Room*>::iterator exitRoom;

    //go through all the rooms
    for (curRoom = rooms.begin(); curRoom != rooms.end(); ++curRoom) {

        //go through all this room's exit indexes, examining each one
        for (int curExitIndex = NORTH; curExit < NUM_DIRECTIONS; ++curExit) { 

            //go through all the rooms, searching for an index that matches the exit index we're examining
            for (exitRoom = rooms.begin(); exitRoom != rooms.end(); ++exitRoom) { 

                //if they match, fill in the appropriate entry of exits[] and move on
                if (exitRoom->index == curRoom->exitIndexes[curExitIndex]) {
                    curRoom->exits[curExitIndex] = *exitRoom;
                    break;
                }

            } //end exitRoom loop
        } //end curExitIndex loop
    } //end curRoom loop
} //end function
Better?
Last edited by MarauderIIC on Tue Jun 23, 2009 4:57 pm, edited 1 time in total.
Reason: put in loading code
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Development Topic: Text Game

Post by MarauderIIC »

Marking as new since I edited like 15 minutes after I posted.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply