I have a 2D array of Integers that represents a game board
given an XY coordinate, I need to examine the board and see if there a 4 adjacent cells with the same value.
example
1234
5678
9ABC
DEFG
if the provided coordinates point to cell 5 I would want to examine all these adjacent cells
if 5,6,9,A all contain the same value, I’m done
otherwise I need to check 1,2,4,5, then 3,4,6,7, then 8,9,C,D etc every combination of 4 cells until either I find a set with the same values, or run out of combos
first you say “x y” then refer to cell 5 - whats cell 5 ? whats the counting sequence ? proceeding from the top row l to r down to the next all the way to the bottom ?
adjacent meaning only adjacent directly left or right & up or down ? no diagonals ?
what about the top rows ? side columns ? and bottom rows where they might have cells adjacent that are off the grid ?
var flag=check(0,0,&x,&y)
for i in(-1...1) {
for j in(-1...1) {
if i==0 && j==0 { continue } // already checked this
flag=check(i,j,&x,&y)
if flag { break }
}
if flag { break }
}
basically it is checking the selected location to see if the tile can be placed… if not it slides the tile left, right, up and down by 1 cell to see if any of those work
off the grid doesn’t count, so they would be a false match
wouldnt those loops give you checks at -1, -1 ( presumably x-1 y-1) , -1, 1 (x-1,y+1) etc which seems diagonal ?
edit : so should the general check be something like
if isOnBoard(checkX-1,checkY) then
if canPlaceAt(checkX-1, checkY ) then
// ???? now what ?
end if
end if
if isOnBoard(checkX+1,checkY) then
if canPlaceAt(checkX+1, checkY ) then
// ???? now what ?
end if
end if
if isOnBoard(checkX,checkY-1) then
if canPlaceAt(checkX, checkY-1 ) then
// ???? now what ?
end if
end if
if isOnBoard(checkX,checkY+1) then
if canPlaceAt(checkX, checkY+1 ) then
// ???? now what ?
end if
end if
does it need all 4 cells around it to have the same value or ???
then maybe your original algorithm is close as it would offset by -1 through 1 in both dimensions and skip the offset of 0,0 (which is your 0 point)
do you want to find ALL possible placements or just the first one ?
the reason I ask is that if you search for all and pick a random x,y offset from the list of them all then you might get better distributions as they would not all prefer to be left & up from the existing one