Hi! I've worked on a code that could give me the six nearest neighbors of a chosen site of a triangular lattice considering periodic boundary conditions.

First, the code asks the user to enter the x and y coordinates of a point inside a maximum "size" then it locates it and prints the neighbors mentioned

**What I have tried:**
This is my code:

#include <stdio.h>
#include <stdlib.h>
#define xmax 4
#define ymax 4
int up_righty(int xcoord, int ycoord){
if( (xcoord%2) != 0){
return ycoord+1;
}
else{ if( (xcoord%2) == 0 ){
return ycoord;
}
}
}
int up_lefty(int xcoord, int ycoord){
if( (xcoord%2) != 0){
return ycoord;
}
else{ if( (xcoord%2) == 0 ){
return ycoord-1;
}
}
}
int down_righty(int xcoord, int ycoord){
if( (xcoord%2) != 0){
return ycoord+1;
}
else{ if( (xcoord%2) == 0 ){
return ycoord;
}
}
}
int down_lefty(int xcoord, int ycoord){
if( (xcoord%2) != 0){
return ycoord;
}
else{ if( (xcoord%2) == 0 ){
return ycoord-1;
}
}
}
int get_limited_coord(int coord, int coord_max){
if (coord >= 0 && coord < coord_max) {
return coord;
} else if (coord >= coord_max) {
return coord - coord_max;
} else {
return coord + coord_max;
}
}
int main() {
int count = 1;
int number;
int x=0,y=0;
int uprighty, uplefty, downrighty, downlefty;
int right, left, upleft, upright, downleft, downright;
int grid[xmax][ymax];
for (y = 0; y < xmax ; y++) {
for (x = 0; x < ymax; x++) {
grid[y][x] = count++;
}
}
printf("\nEnter a X-coordinate :\n");
scanf("%d", &y);
printf("\nEnter a Y-coordinate :\n");
scanf("%d", &x);
printf("(x,y)=(%d,%d)\n",y,x);
if (x > xmax || x < 0 || y > ymax || y < 0) {
printf ("Invalid coordinates given\n");
return 1;
}
uprighty = up_righty(x,y);
uplefty = up_lefty(x,y);
downrighty = down_righty(x,y);
downlefty = down_lefty(x,y);
right = grid[get_limited_coord(y , ymax)][get_limited_coord(x+1, xmax)];
left = grid[get_limited_coord(y , ymax)][get_limited_coord(x-1, xmax)];
upleft = grid[get_limited_coord(y-1, ymax)][get_limited_coord(uplefty, xmax)];
upright = grid[get_limited_coord(y-1, ymax)][get_limited_coord(uprighty, xmax)];
downleft = grid[get_limited_coord(y+1, ymax)][get_limited_coord(downlefty, xmax)];
downright = grid[get_limited_coord(y+1, ymax)][get_limited_coord(downrighty, xmax)];
printf("The coordinates are (%d,%d) and the solicited matrix entry is: %d\n", y, x, grid[y][x]);
printf("Right: %d\nLeft: %d\nUpleft: %d\nUpright: %d\nDownleft: %d\nDownright: %d\n",right,left,upleft,upright,downleft,downright);
return 0;
}

In this case, I have chosen that the max size for the x and y coordinates is 4. The problem is that it seems that this code only gives me the correct neighbors for the following ordered pairs (0,0), (0,1), (1,0), (1,1), (2,2), (2,3), (3,2) and (3,3). On the other hand, it doesn't work for the pairs (0,2), (0,3), (1,2), (1,3), (2,0), (2,1), (3,0), (3,1) (half of the lattice! o.O)

I have checked the functions over and over again but I still don't understand why it works for some entries and not for others.

Any clarification would be immensely appreciated. Thanks in advance!