Off the top of my head, I would use a graph. If you are unfamiliar with what a graph is in programming, here’s some info: https://en.wikibooks.org/wiki/A-level_Computing/AQA/Paper_1/Fundamentals_of_data_structures/Graphs Basically it’s a collection of objects that are connected together, typically used for searching said collection.
Basically, give each space a
checked boolean value (set to false by default), and search for an unoccupied space by choosing a starting space, and then returning “found” if the space is unoccupied, returning “not found” if the space has
checked set to
true or the space belongs to the other player, or setting
checked for the space to
true and repeat this for each space connected to this space, returning “found” if any of those spaces returned “found” or returning “not found” if none of those spaces returned “found”. Once you have finished searching, reset all
checked values back to
To set this up, you just have to give each space a
checked boolean, a value indicating if the space is unoccupied or which player occupies it, and which spaces it is connected to.