grid icon indicating copy to clipboard operation
grid copied to clipboard

Consider adding adjacency functions for groups of indices

Open rpglover64 opened this issue 11 years ago • 3 comments

For example, consider a game in which a player controls a set of indices, and one category of legal moves is to expand to a index adjacent to the controlled set. I want to get a collection of legal moves. In principle, I could take neighbors of each of these, union them, and subtract the initial set, but there may be a better way with access to internals.

Relatedly, I may want to ask if two sets are adjacent to each other.

I realize this may be feature creep, but I came across a situation where I needed similar functionality, so I mention it here.

rpglover64 avatar Dec 10 '13 19:12 rpglover64

Good idea. I've just uploaded grid-7.6. The Grid class now contains:

-- | @'neighboursOfSet' g as@ returns the indices of the tiles in the
--   grid @g@ which are adjacent to any of the tiles with index in
--   @as@.
neighboursOfSet :: Eq (Index g) => g -> [Index g] -> [Index g]

At the moment, it just has the naive implementation (take all the neighbours, remove duplicates, and subtract the initial set), but later on I might implement something better.

As for your related request, what does it mean to say that two sets A and B are adjacent? Does it mean:

  • every tile in A is adjacent to at least one tile in B?
  • every tile in A is adjacent to every tile in B?
  • at least one tile in A is adjacent to at least one tile in B?

I think all of these conditions could have their uses. Which were you thinking of, and how would you envision it being used?

mhwombat avatar Dec 12 '13 14:12 mhwombat

I was thinking the last of those. If we call a territory a collection of grid tiles, I would imagine this being useful for determining whether the two territories border one another (think countries on a map).

Now that I think about it, another useful function is for computing that border.

rpglover64 avatar Dec 12 '13 16:12 rpglover64

Those are both good ideas. I'll try to add them in the next version.

mhwombat avatar Dec 16 '13 10:12 mhwombat