Identifying similar variables

First of all, I want to apologise because the subject title is not very descriptive. So I am creating a video game map that has around 100 tiles. Each tile has a code number that describes its location (column and row). So every tile has a variable called location and then the number of the column and the row. For instance, a tile could be called location100. So now if the player moves to the left the location should change to location99. As you can imagine I can't make a very large switch statement with every possible move. Here is the code I had in mind:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main(){
    
    string location99 = "left";
    string location100 = "center";
    string location101 = "right";
    
    int c = 100;
    string position = location100;
    
    
    //he moves to the left
    c--;
    position = location(c);
    
    
    //he moves 2 right
    c +=2 ;
    position = location(c);

    return 0;
}


Obviously, the code doesn't work but is there any way to use the same logic?
Put the tiles into a 2D array (or 2D vector) indexed by row and column.
The original map is not made out of square tiles as shown in the question but out of hexagonal tiles. I did it like that so the user can have 6 different options at any moment but the problem is that I don't have straight rows(only columns).
https://en.wikipedia.org/wiki/Hexagonal_tiling#/media/File:1-uniform_n1.svg

The problem as shown in the above diagram is that you can't move simply left or right.
You have to move in multiples of 60 degrees. i.e. If moving right, you have to move up and right or down and right.

What I would suggest is to have a TILE class with a constructor that identifiers each of a TILE's six neighbors (or -1 if no neighbor in a particular direction).

I'm sure there are tiling algorithms out that can help with populating a surface with a series of hexagonal tiles. If each TILE identifies all of it's neighbors, then you can store all the TILEs in a vector. Navigating from TILE to TILE is simply deciding which of six directions to move, taking the neighbor # of that direction and changing the current TILE to that location.

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
#include <vector>
#include <iostream>
using namespace std;

class TILE
{   int neighbor[6];
public:
    TILE (int n0, int n1, int n2, int n3, int n4, int n5)
    {   neighbor[0] = n0;
        neighbor[1] = n1;
        neighbor[2] = n2;
        neighbor[3] = n3;
        neighbor[4] = n4;
        neighbor[5] = n5;
    }
    TILE * move (vector<TILE> & grid, int dir)
    {   if (dir < 0 || dir > 5)
        {   cout << "Illegal direction" << endl;
            return NULL;     //  Couldn't move            
        }
        if (neighbor[dir] == -1)
        {   cout << "That's out of bounds" << endl;
            return NULL;
        }
        return &grid[neighbor[dir]];
    }
};

int main ()
{   vector<TILE>    grid;
    TILE *          curr_tile;
    int             dir = 0;
    
    //  First tile based on linked diagram
    //  Numbering vertically (first col = 0-10)
    grid.push_back (TILE(11,-1,-1,-1,1,12));        
    //  Continue for all the other TILEs
    curr_tile = &grid[0];   //  Starting tile
    //  Get the direction (0-5)
    curr_tile = curr_tile->move (grid, dir);
}
Last edited on
Topic archived. No new replies allowed.