0

I'm trying to pick random positions, but also ensure subsequent random positions aren't close to prior positions. In this instance, just adjacent "cells" are removed, but I'd be intrigued how to make this wider, too.

There's probably been similar questions, but I don't even know basic terminology to search for them, apologies!

I can't think how to do this with code other than a layered and abusive exploitation of the wondrous GameplayKit randomisation tools and Swift's elegant array handling, in a jumbled mess.

Is there a way to do this sort of thing that's kind of, well, elegant and accepted/understood and not a complete spaghetti mess?

imagine a board with 8x6 positions available. The first randomly picked position is shown here as (A) and subsequently means adjacent squares are not available...

Picking first random

A subsequent random choice (B) might be like this:

enter image description here

And yet another, and imagine this process going on... at which point I am confused about how to deal with running out of spaces, but that's another problem, for another question...

enter image description here

Confused
  • 6,048
  • 6
  • 34
  • 75
  • Even if it is a "a jumbled mess", showing your current approach would be helpful in order to understand the problem. – A simple approach (if I understand the question correctly) would be to maintain an array of all available positions, choose a random element from the array, and then remove the chosen position and its neighbors – Martin R Oct 11 '16 at 07:12
  • What you've just described is the spaghetti mess I'm talking about. I thought this might be a problem that's more commonly solved and more commonly got a more elegant solution to. But yeah, I'm thinking rows/columns, and removing adjacent cells. But this rapidly becomes a mess because of the edges and problems of finding cells that aren't there anymore, etc. – Confused Oct 11 '16 at 07:26
  • I haven't started writing the code. I know I can make the spaghetti mess work, was really hoping there was a more established and elegant way of solving this problem. – Confused Oct 11 '16 at 07:27
  • Alternatively keep a dictionary of `[cellNumber(Int): Availibilty(Bool)]`, and for each new placement, simply count the number of `true` values in this dictionary, generate a random number up to this size, and position out by traversing the `true` valued cell number ids. For each new placement, apply the `false` value to neighbours according to your wishes. – dfrib Oct 11 '16 at 07:28
  • This sounds like doubling up on spaghetti, as I'd need (in my way of thinking) to use the arrays in a conversion process that determines cell IDs to set the false switches. Or am I missing something elegant and simple? – Confused Oct 11 '16 at 07:53
  • @dfri now that I think more about this, and draw a lot of diagrams, this is a pretty cool idea! – Confused Oct 12 '16 at 03:05
  • @MartinR I have made a spaghetti monster. And it works. But it's UGLY! My next problem... how do I do a quick check to make sure there's some space left so it just get stuck in a loop when there's no more random spots to find? – Confused Oct 12 '16 at 06:26

0 Answers0