How do I get around big "If else's?

So the code works! (Big deal), I'm still not happy with the big "If else statement and desperately need an alternative, expecially since the grids still small and eventually I want a 10 * 10 * 10 multi-dimensenal array in it's place.
system Pause, I'm not worried about, I've got alternatives.
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int array[10] = {1,2,3,4,5,6,7,8,9, 10};
int direction;
char n, e, s, w;
int location;

//cout << array[6] << " " << array[7] << " " << array[8] << " " << array[9] << endl;
//cout << array[3] << " " << array[4] << " " << array[5] << endl;
//cout << array[0] << " " << array[1] << " " << array[2] << endl;

location = array[0];
cout << "You awake to find yourself locked in a grid of rooms" << endl;
cout << "\n" << "Directions of travel are 2 for N, 6 for E, 8 for South, 4 for West," << "\n" << endl;
cout << "starting location = room " <<  location << "\n" << endl;
cout << "Enter your direction to travel?" << endl;

while (location !=10)
{
cin >> direction;

if ( (location == array[0]) && (direction == 2) )       //north to
    location = array[3];                                //room 4
else if ( (location == array[0]) && (direction == 6) )  //east to
    location = array[1];                                //room 2
    
else if ( (location == array[1]) && (direction == 2) )  //north to
    location = array[4];                                //room 5
else if ( (location == array[1]) && (direction == 6) )  //east to
    location = array[2];                                //room 3
else if ( (location == array[1]) && (direction == 4) )  //west to
    location = array[0];                                //room 1

else if ( (location == array[2]) && (direction == 2) )  //north to
    location = array[5];                                //room 6
else if ( (location == array[2]) && (direction == 4) )  //west to
    location = array[1];                                //room 3

else if ( (location == array[3]) && (direction == 2) )  //north to
    location = array[6];                                //room 7
else if ( (location == array[3]) && (direction == 6) )  //east to
    location = array[4];                                //room 5
else if ( (location == array[3]) && (direction == 8) )  //west to
    location = array[0];                                //room 1
    
else if ( (location == array[4]) && (direction == 2) )  //north to
    location = array[7];                                //room 8
else if ( (location == array[4]) && (direction == 6) )  //east to
    location = array[5];                                //room 6
else if ( (location == array[4]) && (direction == 8) )  //west to
    location = array[1];                                //room 2
else if ( (location == array[4]) && (direction == 4) )  //west to
    location = array[3];                                //room 4

else if ( (location == array[5]) && (direction == 2) )  //north to
    location = array[8];                                //room 9
else if ( (location == array[5]) && (direction == 4) )  //east to
    location = array[4];                                //room 5
else if ( (location == array[5]) && (direction == 8) )  //west to
    location = array[2];                                //room 3

else if ( (location == array[6]) && (direction == 6) )  //east to
    location = array[7];                                //room 9
else if ( (location == array[6]) && (direction == 8) )  //south to
    location = array[3];                                //room 4

else if ( (location == array[7]) && (direction == 6) )  //east to
    location = array[8];                                //room 9
else if ( (location == array[7]) && (direction == 8) )  //south to
    location = array[4];                                //room 5
else if ( (location == array[7]) && (direction == 4) )  //west to
    location = array[6];                                //room 7

else if ( (location == array[8]) && (direction == 6) )  //east to
    location = array[9];                                //room 10
else if ( (location == array[8]) && (direction == 8) )  //south to
    location = array[5];                                //room 6
else if ( (location == array[8]) && (direction == 4) )  //west to
    location = array[7];                                //room 8 
     
cout << "Your at location " << location << endl;

if (location == array[9])  //north
cout << "You've found the exit" << endl;

}
system ("PAUSE");
return 0;
}

So, I need advice on what to use instead of, and possibly how someone that knows what there doing, lol, would code it.

Regards

Leppie
Here is the code... it even checks for incorrect moves...

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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
   int direction;
   int location, old_location;

   int map[10][10] =
      {
     //  0,  1,  2,  3,  4,  5,  6,  7,  8,  9
       {11, 11,  3, 11, 11, 11,  1, 11, 11, 11},  // 0
       {11, 11,  4, 11,  0, 11,  2, 11, 11, 11},  // 1
       {11, 11,  5, 11,  1, 11, 11, 11, 11, 11},  // 2
       {11, 11,  6, 11, 11, 11,  4, 11,  1, 11},  // 3
       {11, 11,  7, 11,  3, 11,  5, 11,  1, 11},  // 4
       {11, 11,  8, 11,  4, 11, 11, 11,  2, 11},  // 5
       {11, 11, 11, 11, 11, 11,  7, 11,  3, 11},  // 6
       {11, 11, 11, 11,  6, 11,  8, 11,  4, 11},  // 7
       {11, 11, 11, 11,  7, 11,  9, 11,  5, 11},  // 8
       {11, 11, 11, 11, 11, 11, 11, 11, 11, 11}   // 9
      };

   location = 1;
   old_location = location;
   cout << "You awake to find yourself locked in a grid of rooms" << endl;
   cout << "\n" << "Directions of travel are 2 for N, 6 for E, 8 for South, 4 for West," << "\n" << endl;
   cout << "starting location = room " <<  location << "\n" << endl;
   cout << "Enter your direction to travel?" << endl;

   while (location !=9)
   {
      cin >> direction;

      location = map[location][direction];

      if(location == 11)
      {
         cout<<"Incorrect move...!!!... Enter direction again."<<endl;
         location = old_location;
      }
      else
      {
         cout << "Your at location " << location << endl;
         old_location = location;

         if (location == 9)  //north
         cout << "You've found the exit" << endl;

      }
   }
   return 0;
}


Use a linux terminal... dont pause the system... :)

EDIT : Removed unused variables n, e, s , w
Last edited on
Typically a dungeon map will contain flags indicating which ways are walls, doors, etc. Hence, you know which way you can travel by examining the appropriate bit of the current cell.
Thanks for the answers but it's not it, I should have explained myself further (sorry). Ultimately the whole dungeon map is going to be free roam, look at it as more of a maze (has anyone watched cube)? Your dumped in the grid and have to work your way around, movement will be by forward, back left and right, up and down, starting room, starting direction will be random, there's no grid display, certain rooms are trapped, certain doors locked, and you have to find your way to the outer door, where ever that is? N,S,E & W, wont come into it as your have no idea what direction your facing. Free roam is important as you will need to be able to back-track, all rooms will be the same, apart from colour variations indication level of random traps involved. So I need to be able to wander every direction. Any ideas?
If your maze changes as people roam, then there is no need for a map.

Otherwise, you will have to prepare (or automatically generate) a dungeon map that the user can roam, as we have indicated to you.
Thanks again mgupta and Duoas, one final question though, I ended up preparing a dungeon map, like mgupta's as a 2D array but my question is, as I ultimately want to take this 3D (cubed) 10 * 10 *10 is it possible to prepare a 3d array map?
Yes of course it is....
Declaring a multidimensional array as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int map [10] [10] [10] =
    {
     //  0,  1,  2,  3,  4,  5,  6,  7,  8,  9
       {11, 11,  3, 11, 11, 11,  1, 11, 11, 11},  // 0
       {11, 11,  4, 11,  0, 11,  2, 11, 11, 11},  // 1
       {11, 11,  5, 11,  1, 11, 11, 11, 11, 11},  // 2
       {11, 11,  6, 11, 11, 11,  4, 11,  1, 11},  // 3
       {11, 11,  7, 11,  3, 11,  5, 11,  1, 11},  // 4
       {11, 11,  8, 11,  4, 11, 11, 11,  2, 11},  // 5
       {11, 11, 11, 11, 11, 11,  7, 11,  3, 11},  // 6
       {11, 11, 11, 11,  6, 11,  8, 11,  4, 11},  // 7
       {11, 11, 11, 11,  7, 11,  9, 11,  5, 11},  // 8
       {11, 11, 11, 11, 11, 11, 11, 11, 11, 11}   // 9
      };


But then how do you prepare the third indices for it? Do I just extend this 2D map keeping the third dimension as an abstraction or do I need to create each upper level of the third dimension as an individual map. Hope that makes sense?
You mean like this?

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
int map [10] [10] [10] =
    { //level 1
       {11, 11,  3, 11, 11, 11,  1, 11, 11, 11},  // 0
       {11, 11,  4, 11,  0, 11,  2, 11, 11, 11},  // 1
       {11, 11,  5, 11,  1, 11, 11, 11, 11, 11},  // 2
       {11, 11,  6, 11, 11, 11,  4, 11,  1, 11},  // 3
       {11, 11,  7, 11,  3, 11,  5, 11,  1, 11},  // 4
       {11, 11,  8, 11,  4, 11, 11, 11,  2, 11},  // 5
       {11, 11, 11, 11, 11, 11,  7, 11,  3, 11},  // 6
       {11, 11, 11, 11,  6, 11,  8, 11,  4, 11},  // 7
       {11, 11, 11, 11,  7, 11,  9, 11,  5, 11},  // 8
       {11, 11, 11, 11, 11, 11, 11, 11, 11, 11}   // 9
      },
      { //level 2
       {11, 11,  3, 11, 11, 11,  1, 11, 11, 11},  // 0
       {11, 11,  4, 11,  0, 11,  2, 11, 11, 11},  // 1
       {11, 11,  5, 11,  1, 11, 11, 11, 11, 11},  // 2
       {11, 11,  6, 11, 11, 11,  4, 11,  1, 11},  // 3
       {11, 11,  7, 11,  3, 11,  5, 11,  1, 11},  // 4
       {11, 11,  8, 11,  4, 11, 11, 11,  2, 11},  // 5
       {11, 11, 11, 11, 11, 11,  7, 11,  3, 11},  // 6
       {11, 11, 11, 11,  6, 11,  8, 11,  4, 11},  // 7
       {11, 11, 11, 11,  7, 11,  9, 11,  5, 11},  // 8
       {11, 11, 11, 11, 11, 11, 11, 11, 11, 11}   // 9
      }, //... 
Yes, so on till 10 levels...
I'd be totally lost without you all!

Many, many thanks. (To save me driving you totally mad, can you recomend any reference sites, to visit regarding multidimensional arrays)?

Leppie.
First read your books... I am sure they would have enough explanation about them.
Good Luck.
You could have a pointer to the array, and modify it (increment/decrement the right component) depending on the direction entered.
@ptz

what for?
Somebody actually saw the movie "Cube" besides me??? Wow.
Topic archived. No new replies allowed.