Guest User

Untitled

a guest
Jan 19th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. from sympy import *
  2. from getpass import getpass
  3.  
  4. def print_instructions():
  5. print("""
  6. This is a test that helps me to solve a problem\n
  7. These are the keys to play (type exactly):
  8. 7: fills bottle a
  9. 9: fills bottle b
  10. 46: pass water from a to b
  11. 64: pass water from b to a
  12. 1: empties bottle a
  13. 3: empties bottle b
  14. q: quit the game
  15. Start playing!
  16. """)
  17.  
  18. def display_current_state(a,b):
  19. print("{} {}".format(a.water,b.water))
  20.  
  21. def action(input_char,a,b):
  22. if input_char == '7':
  23. a.fills()
  24. elif input_char == '9':
  25. b.fills()
  26. elif input_char == '46':
  27. pour(a,b)
  28. elif input_char == '64':
  29. pour(b,a)
  30. elif input_char == '1':
  31. a.empties()
  32. elif input_char == '3':
  33. b.empties()
  34. elif input_char == 'q':
  35. b.water=1
  36. else:
  37. print("no command\n")
  38.  
  39. def pour(x,y):
  40. poured = sympify(min(x.water, y.capacity-y.water))
  41. x.water = x.water-poured
  42. y.water = y.water+poured
  43.  
  44. class bottle:
  45. def __init__(self,capacity, initial_water):
  46. self.capacity = capacity
  47. self.water = initial_water
  48. def fills(self):
  49. global used_water
  50. global times_filled
  51. if(self.water==0):
  52. times_filled+=1
  53. used_water+=(self.capacity-self.water)
  54. self.water = self.capacity
  55. def empties(self):
  56. global times_emptied
  57. if(self.water==self.capacity):
  58. times_emptied+=1
  59. self.water = 0
  60.  
  61. def main():
  62. print_instructions()
  63. a=bottle(sympify(2)/3,0)
  64. b=bottle(sympify(5)/4,0)
  65. movs=[]
  66. while(b.water!=1 or a.water!=0):
  67. display_current_state(a,b)
  68. input_char = getpass(prompt='')
  69. movs.append(input_char)
  70. action(input_char,a,b)
  71. print("game ended")
  72. print("winning method {}".format(movs))
  73. print("used water {}".format(used_water))
  74. print("times filled (x)= {}".format(times_filled))
  75. print("times emptied (y)= {}".format(times_emptied))
  76. print("x+y={}".format(times_filled+times_emptied))
  77.  
  78. if __name__=='__main__':
  79. used_water=times_filled=times_emptied=0
  80. main()
Add Comment
Please, Sign In to add comment