Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, basically my problem looks something like this.

When a piece of mine is able to be surrounded by 8 other pieces, I am able to define their positions. For example,

.....
.....
..S..
.....
.....

Here I can find the positions of the dots surrounding the 'S' because if S is array[2][2], a dot north of it would just be array[2][1].

However, my issue arises when my piece is right in the corner of the board or at the bottom, so I no longer have that symmetrical arrangement of 8 points around it.

Hopefully this picture will make my question much more clear!

screeshot — Postimages[^]

screeshot — Postimages[^]

As you can see, the 'neighbours' of the selected piece are now wrapped around the board. I just can't seem to figure out how I can work out the location of these pieces now.

What I have tried:

I have tried checking via conditions such as

Java
if (row - 1 < 0){

      neighbourRow = grid.getRow();      

  }


This is one such example of the types of conditions I have been implementing. Obviously though, it is quite tedious to try manually anticipate every kind of position the selected piece will be in. How would you guys do this?
Posted
Updated 15-Sep-20 0:02am

1 solution

The simplest solution is to keep a "Border" round the four sides of your map.
So if you need a 3x3 map, you declare it as 5x5:
bbbbb
b...b
b...b
b...b
bbbbb
The dots are the n x n "play area" and the b's are the border.
Then for play you use indexes 1 to n inclusive.
Set the whole border to empty, and you can use the "play location" plus or minus one without checking the indexes for correctness.
 
Share this answer
 
Comments
helloThere555 15-Sep-20 8:13am    
Ahh I see. Thanks, just got a quick question though. Say I wanted to make a neighbour array for the current play location, then surely I'd have to reassign values of the border to the original nxn. For example

(1,1) would now be part of the neigbourhood array of the play location, since (4,1) is part of the border version of the playlocations neighbourhood?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900