Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import acm.graphics.*;
- import acm.program.*;
- import acm.gui.*;
- import java.awt.event.*;
- import java.awt.*;
- import java.util.*;
- import acm.util.*;
- // -------------------------------------------------------------------------
- /**
- * Write a one-sentence summary of your class here.
- * Follow it with additional details about its purpose, what abstraction
- * it represents, and how to use it.
- *
- * @author (and if in lab, partner's pid on same line)
- * @version (place the date here, in this format: yyyy.mm.dd)
- */
- public class JugGame extends StudentTestableGraphicsProgram
- {
- //~ Instance/static variables .............................................
- private GRect AtoB,
- BtoA,
- fillA,
- fillB,
- emptyA,
- emptyB,
- AtoTank,
- BtoTank;
- private GLabel toBlabel,
- toAlabel,
- fillAlabel,
- fillBlabel,
- emptyAlabel,
- emptyBlabel,
- toTankA,
- toTankB,
- inA,
- inB,
- inTank;
- private int capacityA;
- private int capacityB;
- private int filledA;
- private int filledB;
- private int filledTank;
- private int goal;
- private RandomGenerator random;
- // ----------------------------------------------------------
- /**
- * procedures to initialize the game
- */
- public void init()
- {
- //get new random capacities...
- random = new RandomGenerator();
- capacityA = random.nextInt(1,5);
- capacityB = random.nextInt(1,5);
- goal = random.nextInt(6,20);
- //make all containers empty...
- filledA=0;
- filledB=0;
- filledTank=0;
- //draw controls...
- //HERE draw the buttons using drawButtons()
- //draw containers...
- //HERE draw a jug at (100,100) called "Jug A" with the max capacity the same as <capacityA> using drawJug()
- //HERE draw a jug at (300,100) called "Jug B" with the max capacity the same as <capacityB> using drawJug()
- //HERE draw a tank at (100,300) using drawTank()
- //draw labels...
- inA=new GLabel("Jug A has: " + filledA + " oz.");
- add(inA,100,95);
- inB=new GLabel("Jug B has: " + filledB + " oz.");
- add(inB,300,95);
- inTank=new GLabel("Tank has: " + filledTank + " oz. of: " + goal + "oz. desired");
- add(inTank,100,295);
- addMouseListeners();
- }
- /**
- * procedures to run the game
- */
- public void run()
- {
- }
- /**
- * handles mouse clicks for different buttons and runs the appropriate functions
- *
- * @ param click - information about where the mouse has clicked
- */
- public void mouseClicked(MouseEvent click)
- {
- int x = click.getX();
- int y = click.getY();
- boolean changed = false;
- if( AtoB.contains(x,y) )
- {
- //pour contents of Jug A into Jug and update how much BOTH jugs are holding...
- changed = true;
- }
- else if( BtoA.contains(x,y) )
- {
- //pour contents of Jug B into Jug A and update how much BOTH jugs are holding...
- changed = true;
- }
- else if( fillA.contains(x,y) )
- {
- filledA = capacityA;
- changed = true;
- }
- else if( fillB.contains(x,y) )
- {
- filledB = capacityB;
- changed = true;
- }
- else if( emptyA.contains(x,y) )
- {
- filledA = 0;
- changed = true;
- }
- else if( emptyB.contains(x,y) )
- {
- filledB = 0;
- changed = true;
- }
- else if( AtoTank.contains(x,y) )
- {
- //pour contents of Jug A into the Tank (Jug A should be empty after)
- changed = true;
- }
- else if( BtoTank.contains(x,y) )
- {
- //pour contents of Jug B into the Tank (Jug A should be empty after)
- changed = true;
- }
- if( changed )
- {
- updateDisplay();
- }
- }
- /**
- * pours a specified amount of water into Jug A and returns the amount left over (if any)
- *
- * @param amount - the amount of water to be poured into Jug A
- * @return int - returns the amount of water left over if the jug would overflow
- */
- public int pourIntoA(int amount)
- {
- int remain;
- if((amount + filledA) > capacityA)
- {
- remain = amount - (capacityA-filledA);
- filledA = capacityA;
- }
- else
- {
- filledA = filledA + amount;
- remain=0;
- }
- return remain;
- }
- /**
- * pours a specified amount of water into Jug B and returns the amount left over (if any)
- *
- * @param amount - the amount of water to be poured into Jug B
- * @return int - returns the amount of water left over if the jug would overflow
- */
- public int pourIntoB(int amount)
- {
- int remain;
- if((amount + filledB) > capacityB)
- {
- remain = amount - (capacityB - filledB);
- filledB = capacityB;
- }
- else
- {
- filledB = filledB + amount;
- remain=0;
- }
- return remain;
- }
- /**
- * updates all the labels on the screen with the current amount filled
- */
- public void updateDisplay()
- {
- inA.setLabel("Jug A has: " + filledA + " oz.");
- inB.setLabel("Jug B has: " + filledB + " oz.");
- inTank.setLabel("Tank has: " + filledTank + " oz. of: " + goal + "oz. desired");
- }
- /**
- * draws the buttons at set positions on the screen
- */
- public void drawButtons()
- {
- //buttons...
- AtoB = new GRect(75,20);
- BtoA = new GRect(75,20);
- fillA = new GRect(75,20);
- fillB = new GRect(75,20);
- emptyA = new GRect(75,20);
- emptyB = new GRect(75,20);
- AtoTank = new GRect(75,20);
- BtoTank = new GRect(75,20);
- toBlabel = new GLabel("A to B ->");
- toAlabel = new GLabel("<- B to A");
- fillAlabel = new GLabel("Fill A");
- fillBlabel = new GLabel("Fill B");
- emptyAlabel = new GLabel("Empty A");
- emptyBlabel = new GLabel("Empty B");
- toTankA = new GLabel("A to Tank");
- toTankB = new GLabel("B to Tank");
- add(AtoB,210,100);
- add(toBlabel,215,115);
- add(BtoA,210,125);
- add(toAlabel,215,140);
- add(fillA,100,20);
- add(fillAlabel,105,35);
- add(fillB,300,20);
- add(fillBlabel,305,35);
- add(emptyA,100,50);
- add(emptyAlabel,105,65);
- add(emptyB,300,50);
- add(emptyBlabel,305,65);
- add(AtoTank,100,220);
- add(toTankA,105,235);
- add(BtoTank,300,220);
- add(toTankB,305,235);
- }
- /**
- * draws a water jug on the screen with a label showing how much it can hold along with its name (e.g. "Jug A")
- *
- * @param x - x location where jug should be drawn (top-left corner)
- * @param y - y location where jug should be drawn (top-left corner)
- * @param oz - integer showing how many ounces the jug can hold
- * @param name - string that says what the jug will say on it
- */
- public void drawJug(int x, int y, int oz, String name)
- {
- GPolygon outline = new GPolygon();
- outline.addVertex(x,y);
- outline.addEdge(10,100);
- outline.addEdge(80,0);
- outline.addEdge(10,-100);
- outline.setFillColor(Color.gray);
- outline.setFilled(true);
- add(outline);
- GLabel text = new GLabel(name);
- add(text,x+30,y+50);
- GLabel capacity = new GLabel(oz+" oz.");
- add(capacity,x+35,y+75);
- }
- /**
- * draws a large tank
- *
- * @param x - x location where the top-left of the tank should be drawn
- * @param y - y location where the top-left of the tank should be drawn
- */
- public void drawTank(int x, int y)
- {
- GPolygon outline = new GPolygon();
- outline.addVertex(x,y);
- outline.addEdge(5,100);
- outline.addEdge(290,0);
- outline.addEdge(5,-100);
- outline.setFillColor(Color.lightGray);
- outline.setFilled(true);
- add(outline);
- GLabel text = new GLabel("Tank");
- add(text,x+140,y+50);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement