Advertisement
acclivity

pyFarmerGiles

Dec 22nd, 2020 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. # Move Farmer + Sheep + Corn + Wolf across river
  2. # Programmed by Mike Kerry in December 2020
  3.  
  4. def draw_image(bl, bb, br):
  5.     print("Left | Boat  | Right")
  6.     wl = bl.copy() + [" ", " ", " ", " "]
  7.     wb = bb.copy() + [" ", " ", " ", " "]
  8.     wr = br.copy() + [" ", " ", " ", " "]
  9.     for x in range(4):
  10.         print(wl[x] + "    |   " + wb[x] + "   |  " + wr[x])
  11.  
  12.  
  13. def check_user(astr):
  14.     # validate user string for any combo of C, F, S, W or comma or space, without duplicates
  15.     # no other characters are allowed
  16.     ret = []
  17.     astr = astr.upper()
  18.     for ch in astr:
  19.         if ch in ret:
  20.             return []        # duplicate item typed
  21.         if ch in ["C", "F", "S", "W"]:
  22.             ret.append(ch)
  23.         elif ch != "," and ch != " ":
  24.             return []
  25.     if "F" not in ret:
  26.         ret.append("F")         # Always include Farmer in the selection
  27.     return ret
  28.  
  29.  
  30. def check_present(ibank, sel):
  31.     # check that the selection "sel" all exist on the specified bank
  32.     for a in sel:
  33.         if a not in ibank:
  34.             return False
  35.     return True
  36.  
  37.  
  38. def check_remain(ibank, ires):
  39.     # Check for a viable combination remaining when ires are removed from ibank
  40.     obank = ibank.copy()    # make a local copy of the given bank
  41.  
  42.     # remove each item in "ires" from our copy of the bank
  43.     for ch in ires:
  44.         if ch in obank:
  45.             obank.remove(ch)
  46.  
  47.     # check what's remaining for allowable combinations
  48.     if "S" in obank:    # Sheep cannot be left alone with corn or wolf
  49.         if "C" in obank or "W" in obank:
  50.             return ["BAD"] + obank
  51.     return obank
  52.  
  53.  
  54. lbank = ["C", "F", "S", "W"]
  55. rbank = []
  56. boat = []
  57. farmerbank = "L"        # Which bank Farmer is currently on
  58.  
  59. # Start Playing
  60. moves = 0
  61. while len(rbank) < 4:
  62.     draw_image(lbank, boat, rbank)
  63.     while True:
  64.         userstr = input("Select items to move from " + farmerbank + " bank: ")
  65.         user_selection = check_user(userstr)
  66.         if not user_selection:
  67.             print("Invalid selection, try again")
  68.         break
  69.     # user_selection contains a valid combo of C, F, S, W
  70.     # check that all "user_selection" items exist on the current bank
  71.     if farmerbank == "L":
  72.         v = check_present(lbank, user_selection)
  73.     else:
  74.         v = check_present(rbank, user_selection)
  75.     if not v:
  76.         print("You selected items that are not on the current bank! FAIL!")
  77.         break
  78.  
  79.     # check what will happen if selected items get moved
  80.     if farmerbank == "L":
  81.         v2 = check_remain(lbank, user_selection)
  82.     else:
  83.         v2 = check_remain(rbank, user_selection)
  84.  
  85.     if v2 and v2[0] == "BAD":
  86.         print("You left an invalid combination on the", farmerbank, "bank")
  87.         print(v2[1:])
  88.         break
  89.  
  90.     # Move farmer to new bank
  91.     if farmerbank == "L":
  92.         lbank = v2
  93.         farmerbank = "R"
  94.     else:
  95.         rbank = v2
  96.         farmerbank = "L"
  97.  
  98.     # Put the farmer and the user selected items in the boat
  99.     boat = user_selection
  100.     draw_image(lbank, boat, rbank)
  101.  
  102.     input("Press Enter to arrive on " + farmerbank + " bank")
  103.  
  104.     # dump cargo on new bank
  105.     if farmerbank == "L":
  106.         lbank = lbank + boat
  107.     else:
  108.         rbank = rbank + boat
  109.     boat = []
  110.     moves += 1
  111.  
  112. print("You succeeded in", moves, "boat crossings")
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement