Writing text-based game in C++ and need tips!

Started by
1 comment, last by shadowisadog 12 years, 5 months ago
First of all, I'd like to say that this is my first post in the forums, so hi! :lol:

A brief introduction about my background: I am in the second year of a Physics curriculum in Greece. I work and have worked extensively with computers, and some say it's my talent. For the most part of this work, I was self-taught (I was always tinkering with the machine and software and learning through trial and error) until I had the honor to have a great and passionate Informatics teacher in High School, who introduced me to the great world of programming. Since then, I have worked with Pascal, C++ a tidbit of Python, Octave and while it's not a programming language, HTML. Unfortunately, since I tend to easily get bored when not seeing results and procrastinate, I can't say that I know any language to its 100%. My favorite until now is C++, so I got a book about it, but it isn't of much help (It's gaming oriented, but I find it hard to follow).

Anyway, since I'm a hardcore gamer, I always wanted to try my hand at writing one. After getting a hard slap when I learnt that 3D game making isn't piece of pie, I decided to go old school and turn to text based games, which to me seem a thrilling way to convey an interactive story. I have a fictional story brewing up in my mind, but it's the technical part that bugs me.

I'm using the Dev-CPP compiler to write my game. I want it to play like this: A dialog describes the setting. After each room/area is described with text, you are asked 'What will you do?'. There are four possible actions in my mind: SEARCH, TAKE, EXAMINE, USE.

SEARCH lists all the objects/usables in the room, like levers, buttons etc.
TAKE <itemID> puts the ItemID item in you inventory.
EXAMINE <itemID> give a description of the object, possibly hinting what you can do with it.
USE <useID> opens/activates the useID usable (lever, door, light etc.)

As for the programming part, While I can go at it writing tons of 'couts << ' and simply make it more like a question game, I'd like to write some efficient code. For example, instead of writing code for each different instance of the story, I'd like to use functions and OOP. Say, TakeItem() function reads the parameter, and if it is valid, adds it to your inventory. I also think about adding a combat system (similar to Final Fantasy and Pokemon), which would rely on member variables like PlayerHealth etc. Problem is, while I have read some OOP and function syntax, I have never wrote code like that so it seems kind of hard! So I should get more specific about my questions.

a) As a draft, I thought about writing a 'big' Player class, which would include the member variables like Health, InventoryCap, Level etc. and the methods of the actions listed above, maybe some other additions too. Is that a good way to 'group' everything the player does?
b) One obstacle I stumble upon when analyzing the game is the item system. First things first, if I get the idea, I should write an Item class which would include general characteristics of all items, and for every new item, make a new instance (object?) of the class with a new name. But this way, I'm troubled about how to pass the names of the objects as parameters to the Player methods. Is it as simple as writing function(object.ObjectName) ?
c) I also am concerned about the Inventory. While I could make a fixed array with an x number of slots, and release them as I use items, I'd prefer to make one that changes size. I'm kind of sure that this makes use of pointers, and while they are a total pain, I want to practice their use.
d) One thing that is about the items in each room. As I said, I don't want everytime that the search action is called to simply print the names. One idea I had is this: in every Item object, include a variable that holds the area number, so when you get in a new area, all the items with this ID are included. Thing is I don't know how to implement a search algorithm for this one! Is there any better idea or should I stick with that one?

That's every question I could come up with. Remember, I'm not a pro with C++ so if I'm missing anything feel free to point it out! Thank you in advance!
Advertisement
There are a couple of principle in OOP that could definitely help you.

a) Writing a big Player class is probably a good idea. It wouldn't hurt as a starting point. My suggestion would be to start with a Player class and only add the stuff that you absolutely need. So don't go adding health unless you need it.

b) If every item in the game is a new object then they could all either inherit from an Item class or implement an Item interface. This way you can write something like use(object). In fact you can use the C++ type system to manage this. For example you can have Lever which implements Item and Door which implements Item. Then you can write use(Door) and use(Lever).
This is Polymorphism and OOP is great for it. I think you could use this in a few places. This also uses Function overloading.

c) What you want there is the C++ Standard Library. Either the vector or the list container or maybe a map. As for pointers, you'll probably end up using them for all sorts of stuff. And I'm pretty sure you'll need them if you use STL.

d) There are many ways you can do this. The way I'd probably go is to give every room it's own inventory. That way you don't have to worry about doing a search dealio.

One other thing. You say you get bored when not seeing results, well there will be plenty of that when start writing this. To avoid this, get your basic stuff going as soon as possible. Get that dialog being printed. Get the What will you do message printed. Move as quickly as you can. And only write what you absolutely need.

First of all, I'd like to say that this is my first post in the forums, so hi! :lol:

A brief introduction about my background: I am in the second year of a Physics curriculum in Greece. I work and have worked extensively with computers, and some say it's my talent. For the most part of this work, I was self-taught (I was always tinkering with the machine and software and learning through trial and error) until I had the honor to have a great and passionate Informatics teacher in High School, who introduced me to the great world of programming. Since then, I have worked with Pascal, C++ a tidbit of Python, Octave and while it's not a programming language, HTML. Unfortunately, since I tend to easily get bored when not seeing results and procrastinate, I can't say that I know any language to its 100%. My favorite until now is C++, so I got a book about it, but it isn't of much help (It's gaming oriented, but I find it hard to follow).

Anyway, since I'm a hardcore gamer, I always wanted to try my hand at writing one. After getting a hard slap when I learnt that 3D game making isn't piece of pie, I decided to go old school and turn to text based games, which to me seem a thrilling way to convey an interactive story. I have a fictional story brewing up in my mind, but it's the technical part that bugs me.

I'm using the Dev-CPP compiler to write my game. I want it to play like this: A dialog describes the setting. After each room/area is described with text, you are asked 'What will you do?'. There are four possible actions in my mind: SEARCH, TAKE, EXAMINE, USE.

SEARCH lists all the objects/usables in the room, like levers, buttons etc.
TAKE <itemID> puts the ItemID item in you inventory.
EXAMINE <itemID> give a description of the object, possibly hinting what you can do with it.
USE <useID> opens/activates the useID usable (lever, door, light etc.)

As for the programming part, While I can go at it writing tons of 'couts << ' and simply make it more like a question game, I'd like to write some efficient code. For example, instead of writing code for each different instance of the story, I'd like to use functions and OOP. Say, TakeItem() function reads the parameter, and if it is valid, adds it to your inventory. I also think about adding a combat system (similar to Final Fantasy and Pokemon), which would rely on member variables like PlayerHealth etc. Problem is, while I have read some OOP and function syntax, I have never wrote code like that so it seems kind of hard! So I should get more specific about my questions.

a) As a draft, I thought about writing a 'big' Player class, which would include the member variables like Health, InventoryCap, Level etc. and the methods of the actions listed above, maybe some other additions too. Is that a good way to 'group' everything the player does?
b) One obstacle I stumble upon when analyzing the game is the item system. First things first, if I get the idea, I should write an Item class which would include general characteristics of all items, and for every new item, make a new instance (object?) of the class with a new name. But this way, I'm troubled about how to pass the names of the objects as parameters to the Player methods. Is it as simple as writing function(object.ObjectName) ?
c) I also am concerned about the Inventory. While I could make a fixed array with an x number of slots, and release them as I use items, I'd prefer to make one that changes size. I'm kind of sure that this makes use of pointers, and while they are a total pain, I want to practice their use.
d) One thing that is about the items in each room. As I said, I don't want everytime that the search action is called to simply print the names. One idea I had is this: in every Item object, include a variable that holds the area number, so when you get in a new area, all the items with this ID are included. Thing is I don't know how to implement a search algorithm for this one! Is there any better idea or should I stick with that one?

That's every question I could come up with. Remember, I'm not a pro with C++ so if I'm missing anything feel free to point it out! Thank you in advance!


Do not use DevCPP. It is outdated and buggy .Use a modern environment such as Visual Studio (the express versions are free) or Code Blocks.

a) It will be alright to start and I wouldn't worry about it at this stage. You can always refactor later.
b) You can use a string parameter. http://www.cplusplus.com/reference/string/string/
c) You can use a vector (a dynamic array). http://www.cplusplus.com/reference/stl/vector/. Really I would use a map over an array (you can access the values by using the item name as a key).
d) could you not have a room class that holds the items (using a map or whatever) rather than the other way around?

This topic is closed to new replies.

Advertisement