Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.72 KB | None | 0 0
  1. import acm.graphics.*;
  2. import acm.program.*;
  3. import acm.gui.*;
  4. import java.awt.event.*;
  5. import java.awt.*;
  6. import java.util.*;
  7. import acm.util.*;
  8.  
  9. // -------------------------------------------------------------------------
  10. /**
  11. * Write a one-sentence summary of your class here.
  12. * Follow it with additional details about its purpose, what abstraction
  13. * it represents, and how to use it.
  14. *
  15. * @author (and if in lab, partner's pid on same line)
  16. * @version (place the date here, in this format: yyyy.mm.dd)
  17. */
  18. public class JugGame extends StudentTestableGraphicsProgram
  19. {
  20. //~ Instance/static variables .............................................
  21. private GRect AtoB,
  22. BtoA,
  23. fillA,
  24. fillB,
  25. emptyA,
  26. emptyB,
  27. AtoTank,
  28. BtoTank;
  29. private GLabel toBlabel,
  30. toAlabel,
  31. fillAlabel,
  32. fillBlabel,
  33. emptyAlabel,
  34. emptyBlabel,
  35. toTankA,
  36. toTankB,
  37. inA,
  38. inB,
  39. inTank;
  40. private int capacityA;
  41. private int capacityB;
  42. private int filledA;
  43. private int filledB;
  44. private int filledTank;
  45. private int goal;
  46. private RandomGenerator random;
  47.  
  48. // ----------------------------------------------------------
  49. /**
  50. * procedures to initialize the game
  51. */
  52. public void init()
  53. {
  54. //get new random capacities...
  55. random = new RandomGenerator();
  56. capacityA = random.nextInt(1,5);
  57. capacityB = random.nextInt(1,5);
  58. goal = random.nextInt(6,20);
  59.  
  60. //make all containers empty...
  61. filledA=0;
  62. filledB=0;
  63. filledTank=0;
  64.  
  65. //draw controls...
  66. //HERE draw the buttons using drawButtons()
  67.  
  68. //draw containers...
  69. //HERE draw a jug at (100,100) called "Jug A" with the max capacity the same as <capacityA> using drawJug()
  70. //HERE draw a jug at (300,100) called "Jug B" with the max capacity the same as <capacityB> using drawJug()
  71. //HERE draw a tank at (100,300) using drawTank()
  72.  
  73. //draw labels...
  74. inA=new GLabel("Jug A has: " + filledA + " oz.");
  75. add(inA,100,95);
  76. inB=new GLabel("Jug B has: " + filledB + " oz.");
  77. add(inB,300,95);
  78. inTank=new GLabel("Tank has: " + filledTank + " oz. of: " + goal + "oz. desired");
  79. add(inTank,100,295);
  80.  
  81. addMouseListeners();
  82. }
  83. /**
  84. * procedures to run the game
  85. */
  86. public void run()
  87. {
  88.  
  89. }
  90. /**
  91. * handles mouse clicks for different buttons and runs the appropriate functions
  92. *
  93. * @ param click - information about where the mouse has clicked
  94. */
  95. public void mouseClicked(MouseEvent click)
  96. {
  97. int x = click.getX();
  98. int y = click.getY();
  99. boolean changed = false;
  100.  
  101. if( AtoB.contains(x,y) )
  102. {
  103. //pour contents of Jug A into Jug and update how much BOTH jugs are holding...
  104.  
  105. changed = true;
  106. }
  107. else if( BtoA.contains(x,y) )
  108. {
  109. //pour contents of Jug B into Jug A and update how much BOTH jugs are holding...
  110.  
  111. changed = true;
  112. }
  113. else if( fillA.contains(x,y) )
  114. {
  115. filledA = capacityA;
  116. changed = true;
  117. }
  118. else if( fillB.contains(x,y) )
  119. {
  120. filledB = capacityB;
  121. changed = true;
  122. }
  123. else if( emptyA.contains(x,y) )
  124. {
  125. filledA = 0;
  126. changed = true;
  127. }
  128. else if( emptyB.contains(x,y) )
  129. {
  130. filledB = 0;
  131. changed = true;
  132. }
  133. else if( AtoTank.contains(x,y) )
  134. {
  135. //pour contents of Jug A into the Tank (Jug A should be empty after)
  136.  
  137. changed = true;
  138. }
  139. else if( BtoTank.contains(x,y) )
  140. {
  141. //pour contents of Jug B into the Tank (Jug A should be empty after)
  142.  
  143. changed = true;
  144. }
  145.  
  146.  
  147. if( changed )
  148. {
  149. updateDisplay();
  150. }
  151. }
  152. /**
  153. * pours a specified amount of water into Jug A and returns the amount left over (if any)
  154. *
  155. * @param amount - the amount of water to be poured into Jug A
  156. * @return int - returns the amount of water left over if the jug would overflow
  157. */
  158. public int pourIntoA(int amount)
  159. {
  160. int remain;
  161. if((amount + filledA) > capacityA)
  162. {
  163. remain = amount - (capacityA-filledA);
  164. filledA = capacityA;
  165. }
  166. else
  167. {
  168. filledA = filledA + amount;
  169. remain=0;
  170. }
  171. return remain;
  172. }
  173. /**
  174. * pours a specified amount of water into Jug B and returns the amount left over (if any)
  175. *
  176. * @param amount - the amount of water to be poured into Jug B
  177. * @return int - returns the amount of water left over if the jug would overflow
  178. */
  179. public int pourIntoB(int amount)
  180. {
  181. int remain;
  182. if((amount + filledB) > capacityB)
  183. {
  184. remain = amount - (capacityB - filledB);
  185. filledB = capacityB;
  186. }
  187. else
  188. {
  189. filledB = filledB + amount;
  190. remain=0;
  191. }
  192. return remain;
  193. }
  194. /**
  195. * updates all the labels on the screen with the current amount filled
  196. */
  197. public void updateDisplay()
  198. {
  199. inA.setLabel("Jug A has: " + filledA + " oz.");
  200. inB.setLabel("Jug B has: " + filledB + " oz.");
  201. inTank.setLabel("Tank has: " + filledTank + " oz. of: " + goal + "oz. desired");
  202. }
  203. /**
  204. * draws the buttons at set positions on the screen
  205. */
  206. public void drawButtons()
  207. {
  208. //buttons...
  209. AtoB = new GRect(75,20);
  210. BtoA = new GRect(75,20);
  211. fillA = new GRect(75,20);
  212. fillB = new GRect(75,20);
  213. emptyA = new GRect(75,20);
  214. emptyB = new GRect(75,20);
  215. AtoTank = new GRect(75,20);
  216. BtoTank = new GRect(75,20);
  217. toBlabel = new GLabel("A to B ->");
  218. toAlabel = new GLabel("<- B to A");
  219. fillAlabel = new GLabel("Fill A");
  220. fillBlabel = new GLabel("Fill B");
  221. emptyAlabel = new GLabel("Empty A");
  222. emptyBlabel = new GLabel("Empty B");
  223. toTankA = new GLabel("A to Tank");
  224. toTankB = new GLabel("B to Tank");
  225. add(AtoB,210,100);
  226. add(toBlabel,215,115);
  227. add(BtoA,210,125);
  228. add(toAlabel,215,140);
  229. add(fillA,100,20);
  230. add(fillAlabel,105,35);
  231. add(fillB,300,20);
  232. add(fillBlabel,305,35);
  233. add(emptyA,100,50);
  234. add(emptyAlabel,105,65);
  235. add(emptyB,300,50);
  236. add(emptyBlabel,305,65);
  237. add(AtoTank,100,220);
  238. add(toTankA,105,235);
  239. add(BtoTank,300,220);
  240. add(toTankB,305,235);
  241. }
  242. /**
  243. * draws a water jug on the screen with a label showing how much it can hold along with its name (e.g. "Jug A")
  244. *
  245. * @param x - x location where jug should be drawn (top-left corner)
  246. * @param y - y location where jug should be drawn (top-left corner)
  247. * @param oz - integer showing how many ounces the jug can hold
  248. * @param name - string that says what the jug will say on it
  249. */
  250. public void drawJug(int x, int y, int oz, String name)
  251. {
  252. GPolygon outline = new GPolygon();
  253. outline.addVertex(x,y);
  254. outline.addEdge(10,100);
  255. outline.addEdge(80,0);
  256. outline.addEdge(10,-100);
  257. outline.setFillColor(Color.gray);
  258. outline.setFilled(true);
  259. add(outline);
  260. GLabel text = new GLabel(name);
  261. add(text,x+30,y+50);
  262. GLabel capacity = new GLabel(oz+" oz.");
  263. add(capacity,x+35,y+75);
  264. }
  265. /**
  266. * draws a large tank
  267. *
  268. * @param x - x location where the top-left of the tank should be drawn
  269. * @param y - y location where the top-left of the tank should be drawn
  270. */
  271. public void drawTank(int x, int y)
  272. {
  273. GPolygon outline = new GPolygon();
  274. outline.addVertex(x,y);
  275. outline.addEdge(5,100);
  276. outline.addEdge(290,0);
  277. outline.addEdge(5,-100);
  278. outline.setFillColor(Color.lightGray);
  279. outline.setFilled(true);
  280. add(outline);
  281. GLabel text = new GLabel("Tank");
  282. add(text,x+140,y+50);
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement