Code, Python

Farmer River Crossing Puzzle

Download full code at at https://github.com/brettius/farmer-river-crossing-game

One of the classic puzzles. How does a farmer get a fox. a goose corn across a river? Constraints are that only one item can be taken at a time, and certain items cannot be left with each other, for example the goose who will eat the corn.

The highest level function is defined first, that of running and stopping the game

print('The Farmers River Crossing v1.0\n')

def main():
  print_game_introduction()
  play_again = True
  while play_again == True:
  run_main_game()
  replay = input('\nPlay again? Y or N?')
  if replay.lower() == 'n':
  play_again = False
  elif replay.lower() == 'y':
  continue

The function for building the main part of the program comes next, and the starting lists of the ‘north_bank’ and ‘south_bank’ are built,

def run_main_game():

game_run = True

north_bank_list = []
  south_bank_list = ['(1) Fox', '(2) Goose', '(3) Corn']

Then the functions for defining the current location of the farmer, the current items on the bank, the question of what item to take are set

while game_run == True:

river_bank_location('south bank')
  get_river_bank_items(south_bank_list)
  row = row_across_question()
  add_or_remove_river_bank_item(row, south_bank_list, north_bank_list)

Series of if statements to decide if the right item was chosen, or if the games exits the loop as an incorrect choice was made

#If incompatible items together break out of loop and end game

if '(1) Fox' in south_bank_list and '(2) Goose' in south_bank_list:
  print('The Fox ate the Goose.')
  break

elif '(2) Goose' in south_bank_list and '(3) Corn' in south_bank_list:
  print('The Goose ate the Corn.')
  break

if ('(1) Fox' in north_bank_list and '(2) Goose' in north_bank_list
  and '(3) Corn' in north_bank_list):
  print('You successfully moved your animals and produce'
  ' to the other side of the river. Now to take them to'
  ' market')
  break

Run the same functions that ran previously for the south bank, but this time for the north bank.

river_bank_location('north bank')
  get_river_bank_items(north_bank_list)
  row = row_across_question()
  add_or_remove_river_bank_item(row, north_bank_list, south_bank_list)

#If incompatible items together break out of loop and end game

if '(1) Fox' in north_bank_list and '(2) Goose' in north_bank_list:
  print('The Fox ate the Goose.')
  break

elif '(2) Goose' in north_bank_list and '(3) Corn' in north_bank_list:
  print('The Goose ate the Corn.')
  break

Here, the functions that make up the body of the game loop are defined; what river bank location the farmer is at, what item is to be taken, as well as the function to perform the removal and addition to the lists, ‘north_bank’, and ‘south_bank’.

def river_bank_location(river_bank_location):
  print('Your at the', river_bank_location, '. The'
  , river_bank_location, 'has the, \n')

def get_river_bank_items(river_bank_list):
  for item in sorted(river_bank_list):
  print(item)

def row_across_question():
  question = True
  while question == True:
  try:
  row = int(input('\nWhat do you transport?'
  '\n\nSelect a number or press (4) '
  'to row across with nothing.\n'))
  if row < 1 or row > 4:
  print('Enter a valid number')
  continue
  except:
  print('Enter a valid number')
  continue
  else:
  return row

def add_or_remove_river_bank_item(row_across_choice, bank_1, bank_2):
  if row_across_choice == 1:
  bank_1.remove('(1) Fox')
  bank_2.append('(1) Fox')
  elif row_across_choice == 2:
  bank_1.remove('(2) Goose')
  bank_2.append('(2) Goose')
  elif row_across_choice == 3:
  bank_1.remove('(3) Corn')
  bank_2.append('(3) Corn')
  elif row_across_question == 4:
  print('You row across with nothing in your boat.')

The program is ended with lowest level function, the printed game introduction.

def print_game_introduction():
  print('''Your a farmer taking a fox, a goose, and corn to market. You
  can only transfer one item at a time. The fox cannot be left alone
  with the goose, as it will eat the goose. The goose cannot be
  left alone with the corn, as it will eat the corn. How will you
  transport them?\n''')

Finally, run the entire game

main()

Leave a Reply

Your email address will not be published. Required fields are marked *