import objectdraw.*; import java.awt.*; /** Include your name, date, and a description of the program here. */ public class Yahtzee extends WindowController { // Constant related to the size and position of buttons and dice private static final int SAVE_LINE_LEFT = 200; private static final int REROLL_LEFT = 25; private static final int BUTTON_TOP = 10; private static final int BUTTON_WIDTH = 50; private static final int BUTTON_HEIGHT = 30; private static final int BUTTON_SPACING = 20; private static final int DIE_LEFT = 60; private static final int DIE_SPACING = 60; private static final int DIE_HEIGHT = 30; private static final int DIE_WIDTH = 30; private static final int TOP_DIES_TOP = BUTTON_TOP+BUTTON_HEIGHT+BUTTON_SPACING; // Instance variables for the 5 dice private Die die1; private Die die2; private Die die3; private Die die4; private Die die5; // Instance variables associated with dragging of the dice private boolean pressedOn = false; private Location lastPoint; private Die selectedDie; // Instance variables associated with the buttons private FramedRect rerollButton; private FramedRect doneButton; // Instance variable to count the number of dice rolls private int rollCount; // Method called at the start of the program to initialize the // dice and buttons. public void begin() { die1 = new Die(DIE_LEFT, TOP_DIES_TOP, canvas); die2 = new Die(DIE_LEFT, TOP_DIES_TOP+DIE_HEIGHT+DIE_SPACING, canvas); die3 = new Die(DIE_LEFT, TOP_DIES_TOP+2*DIE_HEIGHT+2*DIE_SPACING, canvas); die4 = new Die(DIE_LEFT, TOP_DIES_TOP+3*DIE_HEIGHT+3*DIE_SPACING, canvas); die5 = new Die(DIE_LEFT, TOP_DIES_TOP+4*DIE_HEIGHT+4*DIE_SPACING, canvas); new Text("Drag dice to be saved to this side of the line", SAVE_LINE_LEFT + 20, 10, canvas); new Text("before clicking \"Reroll\". Once you have Yahtzee", SAVE_LINE_LEFT + 20, 30, canvas); new Text("(5 of a kind) click on the \"Done\" button.", SAVE_LINE_LEFT + 20, 50, canvas); new Line(SAVE_LINE_LEFT, 0, 200, 500, canvas); rerollButton = new FramedRect(REROLL_LEFT, BUTTON_TOP, BUTTON_WIDTH, BUTTON_HEIGHT, canvas); new Text ("Reroll", REROLL_LEFT+10, BUTTON_TOP+5, canvas); doneButton = new FramedRect(REROLL_LEFT+BUTTON_WIDTH+BUTTON_SPACING, BUTTON_TOP, BUTTON_WIDTH, BUTTON_HEIGHT, canvas); new Text ("Done",REROLL_LEFT+BUTTON_WIDTH+BUTTON_SPACING+5, BUTTON_TOP+5, canvas); rollCount = 1; } // end begin /* REMOVE THIS LINE WHEN YOU ARE READY TO START PART B // Method called when the mouse is clicked. It checks to see if the click // occurred within a button, so appropriate actions can be performed. public void onMouseClick(Location point) { if (rerollButton.contains(point)) { rollCount++; // Roll the dice to the left of the "save" line if (die1.getX() < SAVE_LINE_LEFT) { die1.roll(); } // end if if (die2.getX() < SAVE_LINE_LEFT) { die2.roll(); } // end if if (die3.getX() < SAVE_LINE_LEFT) { die3.roll(); } // end if if (die4.getX() < SAVE_LINE_LEFT) { die4.roll(); } // end if if (die5.getX() < SAVE_LINE_LEFT) { die5.roll(); } // end if } else if (doneButton.contains(point)) { // check for Yahtzee (5 of a kind) and display appropriate message if (die1.getRoll() == die2.getRoll() && die1.getRoll() == die3.getRoll() && die1.getRoll() == die4.getRoll() && die1.getRoll() == die5.getRoll()) { new Text("You got Yahtzee after " + rollCount + " rolls!", 10, 180, canvas); } else { new Text("You DID NOT get Yahtzee after " + rollCount + " rolls!", 10, 180, canvas); } // end if } // end if } // end onMouseClick REMOVE THIS LINE WHEN YOU ARE READY TO START PART B */ /* REMOVE THIS LINE WHEN YOU ARE READY TO START PART C // Method called when the mouse button is pressed. If the mouse button is // pressed on a die, remember that it was selected (and its location) so // that it can be dragged correctly. public void onMousePress(Location point) { if (die1.contains(point)) { selectedDie = die1; pressedOn = true; lastPoint = point; } else if (die2.contains(point)) { selectedDie = die2; pressedOn = true; lastPoint = point; } else if (die3.contains(point)) { selectedDie = die3; pressedOn = true; lastPoint = point; } else if (die4.contains(point)) { selectedDie = die4; pressedOn = true; lastPoint = point; } else if (die5.contains(point)) { selectedDie = die5; pressedOn = true; lastPoint = point; } // end if } // end onMousePress // Method called periodly as the mouse is being dragged. If a die was // selected, move it to the new mouse location. public void onMouseDrag(Location point) { if (pressedOn) { selectedDie.move(point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY()); lastPoint = point; } // end if } // end onMouseDrag REMOVE THIS LINE WHEN YOU ARE READY TO START PART C */ // Method called when the mouse button is released. It "unsets" the flag // that indicates if the mouse was pressed. public void onMouseRelease(Location point) { pressedOn = false; } // end onMouseRelease } // end Yahtzee