Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Move Farmer + Sheep + Corn + Wolf across river
- # Programmed by Mike Kerry in December 2020
- # Email: [email protected]
- def draw_image(bl, bb, br):
- print("Left | Boat | Right")
- wl = bl.copy() + [" ", " ", " ", " "]
- wb = bb.copy() + [" ", " ", " ", " "]
- wr = br.copy() + [" ", " ", " ", " "]
- for x in range(4):
- print(wl[x] + " | " + wb[x] + " | " + wr[x])
- def check_user(astr):
- # validate user string for any combo of C, F, S, W or comma or space, without duplicates
- # no other characters are allowed
- ret = []
- astr = astr.upper()
- for ch in astr:
- if ch in ret:
- return [] # duplicate item typed
- if ch in ["C", "F", "S", "W"]:
- ret.append(ch)
- elif ch != "," and ch != " ":
- return []
- if "F" not in ret:
- ret.append("F") # Always include Farmer in the selection
- return ret
- def check_present(ibank, sel):
- # check that the selection "sel" all exist on the specified bank
- for a in sel:
- if a not in ibank:
- return False
- return True
- def check_remain(ibank, ires):
- # Check for a viable combination remaining when ires are removed from ibank
- obank = ibank.copy() # make a local copy of the given bank
- # remove each item in "ires" from our copy of the bank
- for ch in ires:
- if ch in obank:
- obank.remove(ch)
- # check what's remaining for allowable combinations
- if "S" in obank: # Sheep cannot be left alone with corn or wolf
- if "C" in obank or "W" in obank:
- return ["BAD"] + obank
- return obank
- lbank = ["C", "F", "S", "W"]
- rbank = []
- boat = []
- farmerbank = "L" # Which bank Farmer is currently on
- # Start Playing
- moves = 0
- while len(rbank) < 4:
- draw_image(lbank, boat, rbank)
- while True:
- userstr = input("Select items to move from " + farmerbank + " bank: ")
- user_selection = check_user(userstr)
- if not user_selection:
- print("Invalid selection, try again")
- break
- # user_selection contains a valid combo of C, F, S, W
- # check that all "user_selection" items exist on the current bank
- if farmerbank == "L":
- v = check_present(lbank, user_selection)
- else:
- v = check_present(rbank, user_selection)
- if not v:
- print("You selected items that are not on the current bank! FAIL!")
- break
- # check what will happen if selected items get moved
- if farmerbank == "L":
- v2 = check_remain(lbank, user_selection)
- else:
- v2 = check_remain(rbank, user_selection)
- if v2 and v2[0] == "BAD":
- print("You left an invalid combination on the", farmerbank, "bank")
- print(v2[1:])
- break
- # Move farmer to new bank
- if farmerbank == "L":
- lbank = v2
- farmerbank = "R"
- else:
- rbank = v2
- farmerbank = "L"
- # Put the farmer and the user selected items in the boat
- boat = user_selection
- draw_image(lbank, boat, rbank)
- input("Press Enter to arrive on " + farmerbank + " bank")
- # dump cargo on new bank
- if farmerbank == "L":
- lbank = lbank + boat
- else:
- rbank = rbank + boat
- boat = []
- moves += 1
- print("You succeeded in", moves, "boat crossings")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement