Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sympy import *
- from getpass import getpass
- def print_instructions():
- print("""
- This is a test that helps me to solve a problem\n
- These are the keys to play (type exactly):
- 7: fills bottle a
- 9: fills bottle b
- 46: pass water from a to b
- 64: pass water from b to a
- 1: empties bottle a
- 3: empties bottle b
- q: quit the game
- Start playing!
- """)
- def display_current_state(a,b):
- print("{} {}".format(a.water,b.water))
- def action(input_char,a,b):
- if input_char == '7':
- a.fills()
- elif input_char == '9':
- b.fills()
- elif input_char == '46':
- pour(a,b)
- elif input_char == '64':
- pour(b,a)
- elif input_char == '1':
- a.empties()
- elif input_char == '3':
- b.empties()
- elif input_char == 'q':
- b.water=1
- else:
- print("no command\n")
- def pour(x,y):
- poured = sympify(min(x.water, y.capacity-y.water))
- x.water = x.water-poured
- y.water = y.water+poured
- class bottle:
- def __init__(self,capacity, initial_water):
- self.capacity = capacity
- self.water = initial_water
- def fills(self):
- global used_water
- global times_filled
- if(self.water==0):
- times_filled+=1
- used_water+=(self.capacity-self.water)
- self.water = self.capacity
- def empties(self):
- global times_emptied
- if(self.water==self.capacity):
- times_emptied+=1
- self.water = 0
- def main():
- print_instructions()
- a=bottle(sympify(2)/3,0)
- b=bottle(sympify(5)/4,0)
- movs=[]
- while(b.water!=1 or a.water!=0):
- display_current_state(a,b)
- input_char = getpass(prompt='')
- movs.append(input_char)
- action(input_char,a,b)
- print("game ended")
- print("winning method {}".format(movs))
- print("used water {}".format(used_water))
- print("times filled (x)= {}".format(times_filled))
- print("times emptied (y)= {}".format(times_emptied))
- print("x+y={}".format(times_filled+times_emptied))
- if __name__=='__main__':
- used_water=times_filled=times_emptied=0
- main()
Add Comment
Please, Sign In to add comment