What are some design patterns that appear in your code often?

Hello, I wanted to ask this question because I think it's interesting to hear about other people's experiences. Have you always had one or two design patterns that kept appearing around in your codebase?

Thanks =)
Yeah, well, since I'm a super newbie and sometimes come across some kind of complicated decision making, I always end up having too many if-else statements. If I'm making some kind of text-based adventure game, I would have no idea on how to not repeatedly re-use so many conditionals.
@syeare, perhaps once you're comfortable with functions you'd be able to avoid a lot of conditionals by simply calling a function instead.
I do understand how to use main as a driver for secondary functions, but I'm not sure how exactly I would implement it.
For example, let's say the program is a text adventure like I mentioned earlier. You're in a cave and there's an entrance deeper inside or a hole that you can climb up into that will lead you back in the surface. You can choose either up or down.
Now that you're either up or down, you can choose left or right. How would the function know what to do for each and every one of the countless situations?
syeare, here are some links to past discussions which may be helpful to you.
http://www.cplusplus.com/forum/beginner/250616/
http://cplusplus.com/forum/beginner/263396/
http://www.cplusplus.com/forum/general/119658/
http://www.cplusplus.com/forum/beginner/6864/#msg31667

https://cplussplussatplay.blogspot.com/2012/11/text-adventure-games-c-part-1.html

About text-based game difficulty:
http://www.cplusplus.com/forum/beginner/49189/
http://www.cplusplus.com/articles/G13hAqkS/

dhayden wrote:
The best advice I can give is that this should be data driven. In other words, your code reads a data file that describes the game and then plays it. Put another way, you have to write a "text based adventure game" engine.

If you code the game directly, your program will be a giant mess of text.


Disch wrote:
[A text-based adventure game is] not easy to make. In fact they're one of the harder games you can make.

Parsing text input and making sense of it alone is difficult. Add onto that the heavy events and complexities involved in text based adventure games an you have a project that a beginner probably isn't equipped to handle.

Believe it or not, simple realtime action games with animation and graphics (like a simple galaga or space invaders clone) are tons easier to make.

Beginners want to try out the text based games because they think it will be easier because they think graphics are difficult. But they're mistaken!


A user named Duthomhas (or was it Disch? :P) I believe also wrote a very good summary of how to structure a text-based adventure game. I can't find the link, but basically he reiterates the idea of it being data-driven. A lot of logic should be moved into files that the program itself can parse for information.

Basically you have a set of state which you carry with you for entire game (e.g. which doors have been unlocked, which people you have talked to, current health, current room, etc.). A given room has a set of options associated with it. When you select an option, you then load the next room and loop back to the options, which are now the options for the new room. Focus on just allowing the player to move around the world (east, west, north, south, etc), and then focus on more in-depth logic within each room.

As others have said, some beginners tend to think of text-based adventures as being easy games because there isn't much of a graphical component to it, but it can become very complex; it may be easier to pull off the band-aid and make a simpler, graphical game if you want to give yourself an easier challenge (see Disch's quote). And probably a lot funner (although well-made text-based adventures can be really fun, too).
you can make a 'map' (not a c++ <map>, a game board) in some sort of data structure. If the user goes left, you move left in the structure, and interact with the data stored at that location. The data in the data structure comes from a file, sure. The data may hold the state of that 'room' ... etc. Then all you really need to know is where you are in the structure. You can create zork like games with that approach all day.


as for things I use a lot..
I am a big fan of vector & lookup tables. Programmers often solve things that should just be a fetch, taking a performance hit and a debugging / complexity hit.

I also avoid race conditions in my threading. I much prefer many threads going full bore as fast as they can, each one doing something unrelated to the others, to having a bunch of stop and wait mutex junk that removes the gains of threading. Its not always possible to do that, of course.

Last edited on
@syeare,
one way to avoid if-else is a look-up table. The code snippet below is from a rock-paper-scissor game I made long long time ago for a friend.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
enum Choice {PAPER, SCISSOR, ROCK};

enum Result {PLAYER_WON, COMPUTER_WON, DRAW};

struct Rule
{
  Choice player;
  Choice comp;
  Result result;
};

const int NUM_RULES = 9;

const Rule Rules[] = 
{
  { PAPER, SCISSOR, COMPUTER_WON},
  { PAPER, ROCK, PLAYER_WON},
  { PAPER, PAPER, DRAW},
  { ROCK, PAPER, COMPUTER_WON},
  { ROCK, SCISSOR, PLAYER_WON},
  { ROCK, ROCK, DRAW},
  { SCISSOR, ROCK, COMPUTER_WON},
  { SCISSOR, PAPER, PLAYER_WON},
  { SCISSOR, SCISSOR, DRAW}
};

Result getResult(Choice player, Choice comp)
{
  for (int i = 0; i < NUM_RULES; ++i)
  {
    if (Rules[i].comp == comp && Rules[i].player == player)
      return Rules[i].result;
  }
}
Topic archived. No new replies allowed.