help to reduce code with switch case c++

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.

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
36
37
38
39
40
41
42
43
 move = _getch();
switch(move){
	case 'a':
		if (map[player_y][player_x-1] == 'o'){
			fight();		
			map[player_y][player_x] == ' ';	//delete preview position
			player_x--;
			break;			
		}
		
		if (map[player_y][player_x-1] == ' '){
			map[player_y][player_x]   == ' ';
			player_x--;		// player_x = player_x-1
			break;
		}
		break;
		
		
	case 'w':
		if (map[player_y-1][player_x] == 'o'){
		fight();
		map[player_y][player_x] == ' ';	
		player_y--;
		break;
		}
		
		if (map[player_y-1][player_x] == 'S'){
			cout << "Enter Shop? (y/n): ";
			cshop = _getch();
			if (cshop == 'y'){
				shop_enter(); 
				break;
				}	
			else break;
			}		
		
		if (map[player_y-1][player_x] == ' '){
		fight();
		map[player_y][player_x] == ' ';	
		player_y--;
		break;
		}
		break;
Last edited on
looks like you could benefit from the same method used to object-orient traditional state machines:

www.faisoncomputing.com/publications/articles/oostatemachines.pdf

careful using "map", that symbol is defined in namespace std.
okay, thx
but it looks very more complex than i thought in the first place.

what does std::map do ?
You can type std::map in google cant you?

I'll do you the honor - http://bfy.tw/XuM
Last edited on
Hi, Welcome to Cplusplus

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 using namespace std; Google that as well ;+)

Will need your Google & Wiki foo to help you :+)

Good Luck !!
Here is an example based on TheIdeasMan's suggestion:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <map>
#include <functional>

typedef std::map<char, std::function<void(char)>> CommandMap;

void MoveLeft(char c);
void MoveRight(char c);
void MoveUp(char c);
void MoveDown(char c);

int main()
{
  CommandMap Commands;

  Commands['a'] = MoveLeft;
  Commands['d'] = MoveRight;
  Commands['s'] = MoveDown;
  Commands['w'] = MoveUp;


  int cmd;
  do
  {
    std::cout << "\nEnter command (q to quit) ";
    cmd = _getch();
    if (cmd == 'q')
      break;
    if (Commands.find(cmd) != Commands.end())
    {
      // found the command
      Commands[cmd](cmd);
    }
    else
    {
      // handle unknown command
      std::cout << "\a** Unknown command **\n";
    }
  } while(cmd != 'q');

  system("pause");
  return 0;
}

void MoveLeft(char c)
{
  std::cout << "MoveLeft: " << "c = " << c << "\n";
}
void MoveRight(char c)
{
  std::cout << "MoveRight: " << "c = " << c << "\n";
}
void MoveUp(char c)
{
  std::cout << "MoveUp: " << "c = " << c << "\n";
}
void MoveDown(char c)
{
  std::cout << "MoveDown: " << "c = " << c << "\n";
}
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().
Thx for all the replies.

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.

Similiar to the "Dungeon Crawl" at
http://www.cplusplus.com/articles/N6vU7k9E/
but more complex with levling up and such things.


I'll definitly take a look at ncurses.
Last edited on
Topic archived. No new replies allowed.