Skip to main content
Take on board previous edit, but emphasize the (probably) needless flexibility more
Source Link
Ryan1729
  • 724
  • 5
  • 15

The method described in this answer can certainly work and is incredibly flexible. For example, that method would allow modifying the board at runtime! If you know you do not need that level of flexibility though, you can make things (subjectively) simpler by writingtailoring 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 []

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 []

The method described in this answer can certainly work and is incredibly flexible. For example, that method would allow modifying the board at runtime! If you know you do not need that level of flexibility though, you can make things (subjectively) simpler by tailoring 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 []

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.
Source Link
SpartanDonut
  • 2.9k
  • 1
  • 23
  • 37

The method described in this answer can certainly work. But that method allows creating arbitrary boards and is incredibly flexible. Sometimes, ifIf you know you do not need to support arbitrary use casesthat level of flexibility, you can make things (subjectively) simpler by making things morewriting 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 []

The method described in this answer can certainly work. But that method allows creating arbitrary boards. Sometimes, if you know you do not need to support arbitrary use cases, you can make things (subjectively) simpler by making things more specific.

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 []

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 []

Source Link
Ryan1729
  • 724
  • 5
  • 15

The method described in this answer can certainly work. But that method allows creating arbitrary boards. Sometimes, if you know you do not need to support arbitrary use cases, you can make things (subjectively) simpler by making things more specific.

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 []