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:
#include <iostream>
usingnamespace 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?
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).
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.