Skip to main content
2 of 3
Original version seemed to me to paint the other answer in a negative light. Updated wording to be more true to the two different use cases.
SpartanDonut
  • 2.9k
  • 1
  • 23
  • 37

The method described in this answer can certainly work and is incredibly flexible. If you know you do not need that level of flexibility, you can make things (subjectively) simpler by writing the code to your specific use case.

For example, if we wanted to allow moving on this board:

     3--4--5
    /  /
0--1--2

Then the following implementation which hard-codes in the split and join could be used:

SPLIT = 1
JOINS_TO_FOUR = [2,3]
LAST_SPACE = 5

def moves_from(space_index):
  """Returns an array of the space indexes of the moves that can
  be made from this space index.
  """
  if space_index == SPLIT:
      return [2,3]
  elif space_index in JOINS_TO_FOUR:
      return [4]
  elif 0 <= space_index < LAST_SPACE:
      return [space_index + 1]
  else:
      return []

Ryan1729
  • 724
  • 5
  • 15