Text based game in OOP

pick a language (OOP)

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 8884 Views - Last Post: 12 January 2009 - 07:58 PM Rate Topic: -----

#1 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

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

Text based game in OOP

Post icon  Posted 03 January 2009 - 05:28 PM

It couldn't be easier right, making a text based adventure game. That's what I'm thinking anyway so I'm playing around with this on C# as I learn my way around the VS IDE.

As I get past the initial stage and first few choices of directions for the player to head out I realize it is at least a little more complicated than I at first thought.

If I don't use some form of OOP and just go 'top down' I'll have huge ugly stacks of code in nasty never ending cascades of 'IF' statements.

I'm thinking if I come up with a way to break this out at least to separate functions I can break it up in zones and pass off to the proper function when the user hits a boundary.

I am really brand new at OOP programming and would like to hear if anyone else has tried this and how you went about implementing it. I don't want anyone to do the coding for me, I am still working on understanding the full implications of OOP and the best way to design this game.

What about a 'Zones' class with member functions for each zone in the game and the main function can just call the right member function passing along the couple of vitals stats the player has. Does that sound like I am getting warm as to the best way to do this?

I would really like to hear what other people have done or if you even know of a good link that covers the subject pretty well that would be cool too.

For a simple text based game like this I'm thinking the subject is mostly language independent since the OOP principles would hold true whether in C#, Java or VC++.

Is This A Good Question/Topic? 0
  • +

Replies To: Text based game in OOP

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Text based game in OOP

Posted 03 January 2009 - 06:02 PM

Well first I have to commend you on your thinking because it is often hard for someone new to OOP to realize that they do need to start thinking about code division and getting away from long nasty nested ifs. It is often the furthest from ones mind and something that comes with experience.

You are actually thinking about "refactoring" code and you are on the right track with this. So continue to think of separating out your code into more generic and thus flexible code functions.

Now as for the zones class, I am not too sure that will be the best idea but I can't really add alternatives or recommendations since I am clueless as to how your game design actually works.

Perhaps if you can tell us a little more of how your game works or some code that shows us what you are trying to avoid with the use of a zones class, then we can give some great advice.

Just know you are very much in the ball park with this and you just need to take it further.

:)

This post has been edited by Martyr2: 03 January 2009 - 06:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,585
  • Joined: 16-October 07

Re: Text based game in OOP

Posted 03 January 2009 - 07:15 PM

Without knowing the flow of logic, it's hard to recommend a design. However, Object Oriented Programming is not a magic bullet. It's merely an extra layer of organization. While classes can make a good program better, they can also make a bad program worse. You still have to know what you're doing.

You can program a text based came in pretty much anything. The interface is simple, the logic usually very straight forward. You can even do them web based and many have.

For any game like you're describing, you need to maintain "state." A Player class is a good way to do this in OOP. However, a player struct will work just as well for plain old C.

Do you have a some code to show? I'd like to see the long, ugly IF tree. It's rare that such things can't be solved with application of some structures.
Was This Post Helpful? 0
  • +
  • -

#4 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

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

Re: Text based game in OOP

Posted 04 January 2009 - 02:34 AM

View PostMartyr2, on 3 Jan, 2009 - 05:02 PM, said:

Just know you are very much in the ball park with this and you just need to take it further.
:)

Thank you for the post this ^ was great to hear. I'll post my code so far for you guys to get an idea of what I'm doing.

View Postbaavgai, on 3 Jan, 2009 - 06:15 PM, said:

While classes can make a good program better, they can also make a bad program worse. You still have to know what you're doing.

For any game like you're describing, you need to maintain "state." A Player class is a good way to do this in OOP. However, a player struct will work just as well for plain old C.

Thank you for the tips and advice. I was getting ready to post the code up I have so far and thinking things through when I realized there is another level of complexity that has to be addressed.

Lets say there are three primary directions you can head out on right at the beginning. The primary map location is a square but you can think of it as circular as in you can go completely around the square in either direction and end up where you started.

So along with each of the three directions you can head, each with their own complete chain of if statements containing much duplicate content you need to duplicate each one of those again so that you can reverse direction and head back where you started. Otherwise the adventure would be 'one direction'.

I'm thinking now of changing the whole thing around to an array set up. Well three arrays actually, one for each primary zone.

I'm drawing a map on graph paper and marking each 'movement' location so I know exactly how many there will be. Then I can make an array with that many elements and assign each grid square it's description. If you move ahead, you go to grid spot 2 lets say, and array element 2. From there if you move backward one location you are back to grid location one and array element one's description is 'painted' for you again. With possible changes in character state it might not seem too hokey or repeated.

As a perverse thought, one of the simplest way to do this game might be with 'goto' statements!

Even better I think would be the array method where any movement choice just corresponds with an array element and I'll only need one of every element along with your character state no matter which way you came to be in that particular location.

I can use the struct like Baavgai suggested to keep track of the user state elements pretty easy with this set up I think.

Here is a really crude map of the game area. The apartment complex is a square that can be walked around completely arriving back where you started. There are also two ways to get to the final destination of the store or back to the apartment. Rather than having to duplicate every instance and encounter found in each area depending on which 'If Else' chain was followed this circular map layout was the original reason I thought a zones class might be handy and cut down on duplication. Now I'm thinking the array is the way to go though.

The formatting on here doesn't like to let me draw a text map so imagine the big square as dead space that you can go around completely in either direction ending up where you started at X, the apartment door.

BTW, I live in SO Cal pretty much in the ghetto so this is a tribute to the various ghetto flora and fauna I have experienced on my way from my apartment to the corner liquor store about a block away. :)

Map:
X-store..............................W
--------------------- S--|--N
|...........................| E
|...........................|
||........................... |
||..........................|
|...........................|dumpster
|... (Apartment)X---|-----------car
|...........................|
|...........................|
---------------------
[back parking lot]

Here is my old code just for reference sake, this is being changed to an array set up right now don't forget, this is only a partial of version 1.0. Version 1.1 is on the way :) Go ahead and laugh when you read this, just remember this is my down home ghetto tribute game.
namespace Adventure1
{
    class Program
    {                //workin on player struct per baavgai's suggestion, not sure on implementation yet but
        struct player//working on it!
        {
            string [] healthState;//good bad ugly, hopefully not dead
            String [] inventory;
            int timeOfDay;//straight integer for whole hour tracking only
            int moneyLeft;//don't blow it, gramma will scalp u bald
            int lightState;//0 for dark 1 for light
        }

        static void Main(string[] args)
        {
            String cont = "y";//main function iteration test 'continue' yes or no
            int darkCount = 0; //number of turns spent in the dark, ever been eaten by a grue?
            const int inventoryItems = 3;//max number of items that can be in inventory at one time

            player Dude1;//create new instance of type player

            while (cont == "y")
            {
                Console.Clear();
                //Dude1.moneyLeft = 10;  //not working yet, researching structs in C#
                //Dude1.inventory[0] = "empty";//not working yet, researching structs in C#


                String playerName;
                String travel; //used to read each move in game
                String direction1 = "n";
                String direction2 = "s";
                String direction3 = "e";
                String direction4 = "w";
                string direction5 = "car";


                Console.Write("Hello ghetto dweller, please enter yo name: ");
                playerName = Console.ReadLine();

                Console.WriteLine("Yo {0} you must get through the ghetto to dee liqqur sto to pick up a ", playerName);
                Console.WriteLine("bottle of \"medsins\" for yo Gramma.");
                Console.WriteLine();

                Console.WriteLine("When you'z leave yo crib you step out dee doh and are standing on the ");
                Console.WriteLine("sidewalk facing norf.  From here you have a choice of going norf to the ");
                Console.WriteLine("parking lots, souf back to yo crib, or east and west on the sidewalk.");

                Console.WriteLine();
                Console.Write("Which way do you want to go {0}, enter direction (n,s,e,w): ", playerName);
                travel = Console.ReadLine();

                Console.WriteLine();
                if (travel.Equals(direction1))
                {
                    Console.WriteLine("You went {0}, you are standing behind yo hooptie, what a sweet ride!", direction1);
                    Console.WriteLine();
                    Console.WriteLine("From here you can go souf, or enter your car.  Which way you wants to go {0}?", playerName);
                    Console.Write("Enter choice (car,s): ");
                    travel = Console.ReadLine();
                    //if car enter auto-zone :)/> and continue with hooptie encounter
                    //south goes back to main apt start zone
                    //primary direction thread 1
                }
                else
                    if (travel.Equals(direction2))//add && inventory==empty to this path
                        Console.WriteLine("Why you's bugging out back to yo crib {0}?  Get yo ass to dee sto!", playerName);
                    else
                        if (travel.Equals(direction4))
                        {
                            Console.WriteLine("You went {0}, you's standing sides the trash dumpster.  You here a bum jumping up ", direction4);
                            Console.WriteLine("and down inside.");
                            Console.WriteLine();

                            Console.WriteLine("From here you can go norf in to dee dumpster enclosure, east back to yo warm ");
                            Console.WriteLine("crib or west towards the main street.  What way you's goin {0}?", playerName);
                            Console.Write("Enter choice (n,w,e): ");
                            travel = Console.ReadLine();
                            //if north continue with dumpster enclosure zone and bum encounter
                            //east goes back to main apt start zone
                            //primary direction thread 2
                        }
                        else
                            if (travel.Equals(direction3))
                            {
                                Console.WriteLine("You went {0}, you are at the end of the sidewalks at the NE corner of the ", direction3);
                                Console.WriteLine("complex looking at the back lot of the apartment building.");
                                //primary direction thread 3
                            }

                Console.WriteLine();
                Console.WriteLine("Congratulations, {0}, this is the end of your adventure.  Thank you for playing.", playerName);

                Console.Write("Would you likes to try again?  Enter y or n: ");
                cont = Console.ReadLine();
            }
        }
    }
}

This post has been edited by badjava: 04 January 2009 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,585
  • Joined: 16-October 07

Re: Text based game in OOP

Posted 04 January 2009 - 03:03 AM

View Postbadjava, on 4 Jan, 2009 - 03:34 AM, said:

here is my code so far.


:huh: Where is here?

( Note: I do this with emails all the time. And in the attached... oops.)
Was This Post Helpful? 0
  • +
  • -

#6 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

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

Re: Text based game in OOP

Posted 04 January 2009 - 01:00 PM

View Postbaavgai, on 4 Jan, 2009 - 02:03 AM, said:

View Postbadjava, on 4 Jan, 2009 - 03:34 AM, said:

here is my code so far.


:huh: Where is here?

( Note: I do this with emails all the time. And in the attached... oops.)


It's above your post sry for weirdness, apparently it kept the time stamp from when I started the post yesterday before grocery shopping and all that got me side tracked.
Was This Post Helpful? 0
  • +
  • -

#7 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Text based game in OOP

Posted 04 January 2009 - 04:06 PM

1) Don't worry about things like time of day, health, the player's name, or inventory. Just concentrate on getting movement down.

2) Don't explicitly code your whole game. The way you're doing it now has nothing to do with the programming paradigm you're using. You need to separate your data from your code.

For example, you could define your "data" in a text file like this:
@area
id=1
name=Apartment
description=A run down apartment.
exit=A door to the South=2
exit=A window to the East=3

@area
id=3
name=Balcony
description=A balcony...
exit=A window to the West=1

@area
id=2
name=Hallway
description=A long hallway.
exit=A door to the North=1



You would need to create the appropriate data structures, for example (going with the above) you would probably want to create some kind of "area" structure.

Then you would need to write code to load the text files into your structures at runtime.
Was This Post Helpful? 0
  • +
  • -

#8 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

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

Re: Text based game in OOP

Posted 04 January 2009 - 04:23 PM

View PostTom9729, on 4 Jan, 2009 - 03:06 PM, said:

1) Don't worry about things like time of day, health, the player's name, or inventory. Just concentrate on getting movement down.

View PostTom9729, on 4 Jan, 2009 - 03:06 PM, said:

2) Don't explicitly code your whole game. The way you're doing it now has nothing to do with the programming paradigm you're using. You need to separate your data from your code.

Well I part way get this. I can see separating the data from the code, and work on movement etc, I don't know exactly what you mean by what I'm doing now has nothing to do with the paradigm. Could you explain what you mean? I have a feeling it will help me get focused in the right way here.
Was This Post Helpful? 0
  • +
  • -

#9 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Text based game in OOP

Posted 04 January 2009 - 04:32 PM

View Postbadjava, on 4 Jan, 2009 - 06:23 PM, said:

Well I part way get this. I can see separating the data from the code, and work on movement etc, I don't know exactly what you mean by what I'm doing now has nothing to do with the paradigm. Could you explain what you mean? I have a feeling it will help me get focused in the right way here.

I meant that I don't think the problem you have now is going to be solved just by making your program OO (object oriented).

Making it OO certainly wouldn't hurt. Especially with games, it makes it easier to think about your program, ex. having a "player" object, a "item" object.

Even if you do make it OO, writing the game the way you'd write a Choose Your Own Adventure book is just going to lead to a massively long headache inducing amount of code.

You need to reduce the amount of code duplication that you're doing. Even if you don't want to parse all of the game data from text files, having a bunch of area objects each with their own description (and maybe something like a printDescription(..)) method associated with them would greatly clean up your code.

This post has been edited by Tom9729: 04 January 2009 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Text based game in OOP

Posted 04 January 2009 - 04:34 PM

He's saying that you should set up the "infrastructure" before worrying about game content. Example:

ifstream storyData("story.txt");
//do some file I/O
//load it into a structure/class of some nature
myPlayer->setInfo(textFileHealthData); //example

//I'm dynamic and part of the game engine per se :)



Instead of:

//somewhere in the code
cout << "You enter a cave."; //hard coded

//I'm not flexible and I'll have to be changed each iteration of the design process :(



edit: Top down OOP and regular top down are are still going to be messy, error prone, and tedious to develop. Point being that making if else structures OOP doesn't make them something else other then a massive cascading if else structure.

edited for typos

This post has been edited by KYA: 04 January 2009 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

#11 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

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

Re: Text based game in OOP

Posted 04 January 2009 - 04:53 PM

View PostKYA, on 4 Jan, 2009 - 03:34 PM, said:

edit: Top down OOP and regular top down are are still going to be messy, error prone, and tedious to develop. Point being that making if else structures OOP doesn't make them something else other then a massive cascading if else structure.

edited for typos


OIC, ok thank you both of u guys, I get it now. This is a great learning experience. Who woulda thought it would stem from a simple text based game?!

(going back to paper to do more planning & layout work)

This post has been edited by badjava: 04 January 2009 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

#12 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Text based game in OOP

Posted 05 January 2009 - 06:08 AM

I'm (read: very slowly) putting a text adventure engine together with OOP in C++, feel free to check it out. It's in the "Share your Projects" forum. :)

Tom is right though, focus on getting your map set up, and get movement working. It's actually quite a simple task. :)
Was This Post Helpful? 0
  • +
  • -

#13 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

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

Re: Text based game in OOP

Posted 05 January 2009 - 10:27 AM

View Postgabehabe, on 5 Jan, 2009 - 05:08 AM, said:

I'm (read: very slowly) putting a text adventure engine together with OOP in C++, feel free to check it out. It's in the "Share your Projects" forum. :)

Tom is right though, focus on getting your map set up, and get movement working. It's actually quite a simple task. :)

I think I hit on the way to do it last night. I'm curious to see your project to see if I'm anywhere close to the way you are doing it.
Was This Post Helpful? 0
  • +
  • -

#14 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

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

Re: Text based game in OOP

Posted 05 January 2009 - 11:15 AM

View Postgabehabe, on 5 Jan, 2009 - 05:08 AM, said:

I'm (read: very slowly) putting a text adventure engine together with OOP in C++, feel free to check it out. It's in the "Share your Projects" forum. :)

Tom is right though, focus on getting your map set up, and get movement working. It's actually quite a simple task. :)


I need more to go on, I looked and couldn't find anything. Searched on your name but too many posts to go through them all this morning :)

Oh wait is it that cider post i saw? I just read 'engine'...

This post has been edited by badjava: 05 January 2009 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#15 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Text based game in OOP

Posted 05 January 2009 - 06:45 PM

You know, Guy, you don't really get on Yahoo nearly half-as-often as I had assumed. I would've been glad to help you with this project as I'd said, but apparently you've discarded my offer.

Hope you get it completed.
Just to echo what somebody had said earlier (in different words): Don't worry about game-specifics.
You must learn how to read instruments before you can drive around in space.
(You must be able to move around in a "virtual world" before you can add an inventory, life, etc)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3