Hi,
I want to create a little command game, where you can move an ASCII sign
around the board.
You can navigate it with the wasd keys.
If you hit a special sign, for example o or S or something else
actions are called.
i use the switch case fct to check which key was pressed.
is there an option to reduce the code down there?
If I had more symbols that call different functions the code down there would inflate very fast and becomes messy.
Some other simple ideas, maybe not as good as those from tipaye :+)
Have each case: call it's own function. This will tidy things up considerably.
Have a std::map<char, std::function>, this is a table of function pointers which is better than a switch if there are lots of cases.
There is also ncurses , a library for doing stuff in the console : colours, moving the cursor etcetera.
A State Machine is one of the Design patterns, consider looking into the others as well. Some of the Frameworks such as Qt have a State Machine object which you can adapt and use. Qt has a lot of really good stuff, check it out.
As I seem to be saying "ad nausem", don't have usingnamespace std; Google that as well ;+)
I think a case statement is fine. What you need to reduce the code is a function move(int x, int y, int deltax int deltay);
Then much (sometimes all) of the code in the cases can collapse to a call to move().
I did not know that map was reserved in the std namespace.
map in may case should be a class where the current "game_map" is stored.
an ASCII field that looks something like these
1 2 3 4 5 6 7 8
#############
# S o #
#
# oo #
# o#
# o #
#############
and the player_x and player_y gives the coordinates where the player currently is.
If he hits the open place on the right he would enter the next map, etc.
like in the old snes role play games.