3
\$\begingroup\$

I thought a fun way for my son and I to learn and improve our python programming skills respectively is to digitize one of his (and my and my mom's when we were his age) favorite board games, "Hey Pa! There's a goat on the roof". When I suggested the idea, I didn't remember that the game board had three branching paths that connect and diverge in different ways and now I am flummoxed on how to store the board position for the players logically.

Ultimately, you're trying to get from the goat pen on the left to the barn in the upper right. There's "power up" cards that can warp you around the board to the haystack / flower bed / wood pile / etc, so each position needs to be numbered. Since there's an uneven number of spaces in the three tracks, and the top and middle one meet at two different points, I didn't think an array would work. I couldn't wrap my head around how to use consecutive integers cleanly.

What are your thoughts on how we can make this work?

Board layout of hey pa theres a goat on the roof

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ This is along the ideas of what I was wondering the solution was, but was hoping for tighter solution... and more complicated than what we're prepared to deal with at the moment... :-) \$\endgroup\$
    – Nick
    Commented Jan 10, 2022 at 0:32
3
\$\begingroup\$

Use an array of board space objects whose members include an array of other board space objects that it connects to. Then build a model of the board out of them.

In pseudocode:

object BoardSpace
    int Index
    array BoardSpace Connections()
    function AddConnection(BoardSpace space)
        Connections.AddElement(space)

So in your main program you have

array BoardSpace TheBoard()
function InitBoard
    for i = 0 to 9 # Let's say there are 10 spaces on the board
        TheBoard(i) = new BoardSpace
        TheBoard(i).Index = i

    # Board now has its objects, set up the connections:
    TheBoard(1).AddConnection(TheBoard(2))
    TheBoard(1).AddConnection(TheBoard(3))
    TheBoard(2).AddConnection(TheBoard(1))
    TheBoard(3).AddConnection(TheBoard(1))
    # etc, set up all connections.

In this example, space 1 connects to spaces 2 and 3. There are way more efficient ways to do this, but this idea can help you get going.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .