Slow down
Honestly, that code confuses me.
But thats why I am trying to help you learn to implement a method that will be easier to handle and work with.
This isn't exactly a linked list, but its a linked set of structs to help you understand a linked list.
With a linked list you just create your squares such as
- Code: Select all
typedef struct Square
{
Actor* mySquareActor;
int SquareNumber;
struct Square* TopSquare;
struct Square* BottomSquare;
struct Square* LeftSquare;
struct Square* RightSquare;
} Square;
That would be your square. It takes one actor which you create, then you assign the actor to the square, then you tell the square left right up or down.
The code for that looks like this:
- Code: Select all
int Position = 4; // Start at the middle square
// Create all the square structures
Square TopLeftSquare, TopMiddleSquare, TopRightSquare,
CenterLeftSquare, CenterMiddleSquare, CenterRightSquare,
BottomLeftSquare, BottomMiddleSquare, BottomRightSquare;
// Put all the squares into an array for later use.
Square grid[9] = { TopLeftSquare, TopMiddleSquare, TopRightSquare,
CenterLeftSquare, CenterMiddleSquare, CenterRightSquare,
BottomLeftSquare, BottomMiddleSquare, BottomRightSquare };
void CreateGrid()
{
//Top portion of the grid
TopLeftSquare.SquareNumber = 0; // Programmer's count starts at 0
TopLeftSquare.TopSquare = NULL; //Doesn't point to a square because there isn't one there
TopLeftSquare.LeftSquare = NULL;
TopLeftSquare.RightSquare = &TopMiddleSquare;
TopLeftSquare.BottomSquare = &CenterLeftSquare;
TopMiddleSquare.SquareNumber = 1;
TopMiddleSquare.TopSquare = NULL;
TopMiddleSquare.LeftSquare = &TopLeftSquare;
TopMiddleSquare.RightSquare = &TopRightSquare;
TopMiddleSquare.BottomSquare = &CenterMiddleSquare;
TopRightSquare.SquareNumber = 2;
TopRightSquare.TopSquare = NULL;
TopRightSquare.RightSquare = NULL;
TopRightSquare.LeftSquare = &TopMiddleSquare;
TopRightSquare.BottomSquare = &CenterRightSquare;
// Center Portion of the grid
CenterLeftSquare.SquareNumber = 3;
CenterLeftSquare.LeftSquare = NULL;
CenterLeftSquare.TopSquare = &TopLeftSquare;
CenterLeftSquare.RightSquare = &CenterMiddleSquare;
CenterLeftSqaure.BottomSquare = &BottomLeftSquare;
CenterMiddleSquare.SquareNumber = 4;
CenterMiddleSquare.TopSquare = &TopMiddleSquare;
CenterMiddleSquare.LeftSquare = &CenterLeftSquare;
CenterMiddleSquare.BottomSquare = &BottomMiddleSquare;
CenterMiddleSquare.RightSquare = &CenterRightSquare;
CenterRightSquare.SquareNumber = 5;
CenterRightSquare.RightSquare = NULL;
CenterRightSquare.TopSquare = &TopRightSquare;
CenterRightSquare.RightSquare = &CenterMiddleSquare;
CenterRightSqaure.BottomSquare = &BottomRightSquare;
//Bottom portion
BottomLeftSquare.SquareNumber = 6;
BottomLeftSquare.BottomSquare = NULL;
BottomLeftSquare.LeftSquare = NULL;
BottomLeftSquare.RightSquare = &BottomMiddleSquare;
BottomLeftSquare.TopSquare = &CenterLeftSquare;
BottomMiddleSquare.SquareNumber = 7;
BottomMiddleSquare.BottomSquare = NULL;
BottomMiddleSquare.LeftSquare = &BottomLeftSquare;
BottomMiddleSquare.RightSquare = &BottomRightSquare;
BottomMiddleSquare.TopSquare = &CenterMiddleSquare;
BottomRightSquare.SquareNumber = 8;
BottomRightSquare.BottomSquare = NULL;
BottomRightSquare.RightSquare = NULL;
BottomRightSquare.LeftSquare = &BottomMiddleSquare;
BottomRightSquare.TopSquare = &CenterRightSquare;
Now that all your squares are connected you just need to make functions to navigate them.
- Code: Select all
void MoveUp()
{
if(grid[Position]->TopSquare != NULL)
{
Position = grid[Position]->TopSquare->SquareNumber;
}
}
void MoveDown()
{
if(grid[Position]->BottomSquare != NULL)
{
Position = grid[Position]->BottomSquare->SquareNumber;
}
}
void MoveLeft()
{
if(grid[Position]->LeftSquare != NULL)
{
Position = grid[Position]->LeftSquare->SquareNumber;
}
}
void MoveRight()
{
if(grid[Position]->RightSquare != NULL)
{
Position = grid[Position]->RightSquare->SquareNumber;
}
}
Now you have your grid, you have your move functions, and you know what square you are in at all times.
To make it where your grid is attached to actors just takes a little bit more work in adding actors and having the grid positions point to those actors. I can create an example to help you understand this.
Also, with linked lists you can create grids of any size and they are fast and easy to implement and are fast to access. The issue is that you have to understand how to allocate memory for structs in order to create a linked list.
Here is an example loop that would allocate memory for the struct above.
- Code: Select all
int MapHeight = 100; // Change to any value
int MapWidth = 100;
Square *** Grid; //This is a triple pointer to create a 2D array for a grid of squares.
int AllocateMemoryForGrid()
{
int i, a;
//Allocate memory for 2D array Grid
Grid = malloc(MapHeight * sizeof(Square **));
if(Grid == NULL)
{
//ERROR: OUT OF MEMORY
return 1;
}
for(i = 0; i < MapHeight; i++)
{
Grid[i] = malloc(MapWidth * sizeof(Square*));
if(Grid[i] == NULL)
{
//ERROR: OUT OF MEMORY
return 1;
}
}
for(i = 0; i < MapHeight; i++)
{
for(a = 0; a < MapWidth; a++)
{
Grid[i][a] = (Square *)malloc(sizeof(Square));
if(Grid[i][a] == NULL)
{
//ERROR: OUT OF MEMORY
return 1;
}
}
}
return 0;
}
Then you just need to create other functions to set the variables and navigate the list just by going through the pointers.
Then when your finished with it, free the memory:
- Code: Select all
void FreeGrid()
{
for(i = 0; i < MapHeight; i++)
{
for(a = 0; a < MapWidth; a++)
{
free(Grid[i][a]);
}
free(Grid[i]);
}
}