Structure of my game

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Structure of my game

Post by Yumtard » June 8th, 2017, 9:21 pm

Hey guys,

So today I made a game. It's a simple text based adventure game.
I got it to work the way it's supposed to work but feel unsure how I should structure the code.
Feels like there should be more function and maybe more classes and that maybe some stuff should go somewhere else...

As of now basically the whole game is played in a boring function called PlayGame() which gets a line from the user and then finds the correct response.

here's a list of possible commands:
north
south
west
east
status
kill monster
look inventory
buy charm
buy talisman

Please let me know if you think this looks fine the way it is or if you have some tips/hints for how I can better structure a game like this.

Note: I don't need tips for making the game more exciting or better game play, the game is going to be shit :D just wanna know if this is a shit way of doing it or not basically

Note 2: I do not want you to hold my dick and completely rewrite it for me. Just hints or critizing the potentially shitty logic

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Structure of my game

Post by Yumtard » June 8th, 2017, 9:50 pm

might be a good idea to add the link

https://github.com/Yumtard/TextAdventure

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Structure of my game

Post by Yumtard » June 8th, 2017, 11:31 pm

Working on improvements... :D

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Structure of my game

Post by albinopapa » June 9th, 2017, 7:38 am

Maybe now is a good time to learn about inheritance. For instance, you locations could be setup as different children of a parent Location class.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Structure of my game

Post by Yumtard » June 9th, 2017, 2:25 pm

^ went a bit different way than that, but good point gonna try something with inheritance next!

Changed the whole code completely but can't push to github for some reason

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Structure of my game

Post by Yumtard » June 9th, 2017, 2:31 pm

https://github.com/Yumtard/TextAdventur ... /Antagning

here's the new code
branch "neat"

fter a confusing day of many fails, this feels a bit better

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Structure of my game

Post by Yumtard » June 9th, 2017, 5:19 pm

Meh u guys can ignore OP. Im almost finished, just a couple things left and prob wont change it anymore so dont waste your time :)

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Structure of my game

Post by chili » June 10th, 2017, 6:47 am

Most of my advice would have required some features of the language that would take you too long to have learned anyways, and probably would have been overkill for this scale of game.

First of all, if...else chains scale terribly as the number of possible inputs increases, both in terms of performance and code maintainability. I would use a map/unordered map.

Second, I would organize locations as a graph of location objects connected by pointers. I would create a Verb/Action class and an Object class. Verbs would have a set grammar, like "take <obj>" or "use <obj1> on <obj2>". Objects would have a container of allowed actions and their effects. Locations and player inventory can contain objects. When a player enters a command, the parse will try to match the sentence to a command and check to see if the named objects exist in range and if they support the command, and if so it will execute the effect functor stored for that action/object/location combination.

Of course there are more details to hash out but that would be the general approach I would try.
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Structure of my game

Post by Yumtard » June 10th, 2017, 10:14 am

^ Awesome chili, never heard of these concepts.
Do you/do you plan to cover any of this in tutorials?
Will try to look into it

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Structure of my game

Post by chili » June 10th, 2017, 11:33 am

I'll definitely be covering things like map containers. Also of course inheritance and dynamic binding (which would be how you would want to implement the above). And also the concept of functors/lambdas will be covered. I doubt I'll cover the specific case of making a text adventure, and probably not any other kind of grammar parser either (I'm making a parser for the assembler in the CPU Design series, but I'm not going to cover the making of the assembler itself, just going to be using it as a tool during the CPU design).
Chili

Post Reply