Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Lets the user play the Monty Hall game where
- * you pick a door, Monty opens a losing remaining door and asks if you wish
- * to switch. There is a prize behind just one door.
- *
- * This is a mostly incomplete outline of the problem.
- * Be sure and edit this opening comment as well.
- *
- * @author Charlie McDowell
- */
- PFont buttonFont;
- // used by mousePressed to detect and indicate door selection
- int doorWidth, doorHeight, xDoorGap, yDoorGap, doorSelected = 0;
- // stages of the game: START - all doors closed, DOOR_SELECTED - one door open, END - all doors open
- final int START = 1, DOOR_SELECTED = 2, END = 3;
- int gameStage = START;
- int winner; // randomly selected winning door number (1, 2, or 3)
- int rounds = 0; // total number of rounds played
- int numWins = 0; // number of times the player ultimately selected the winning door
- /**
- * Display the initial panel with the 3 doors and some instructions.
- */
- void setup() {
- size(600, 400);
- buttonFont = loadFont("AppleCasual-48.vlw");
- textFont(buttonFont, 48);
- textAlign(CENTER);
- // make door size proportional to the display size
- doorWidth = width/4;
- // 3 doors plus 4 door gaps should span the display
- xDoorGap = (width - 3*doorWidth)/4;
- doorHeight = (int)(height*.6);
- yDoorGap = doorHeight/10; // distance from the top of the display
- startRound();
- }
- /**
- * Draw a door open or closed at the specified location and size (x,y,width,height).
- * (x,y) is the location of the upper left corner of the door.
- *
- * @param doorNum - the door number (1, 2, or 3)
- * @param x - the x value of the upper left corner of the door
- * @param y - the y value of the upper left corner of the door
- * @param width - the width of the door
- * @param height - the height of the door
- * @param open - true if the door is shown as open (displaying what is behind) or false if closed
- * @param winner - the number of the winning door. Used along with doorNum to decide what to display if the door is open.
- */
- void drawDoor(int doorNum, int x, int y, int width, int height, boolean open, int winner) {
- fill(0);
- if (open) {
- fill(255);
- }
- rect(x,y,width,height);
- }
- /**
- * Sets the global variable doorSelected to 0 if no door is selected
- * or the door number (1, 2, or 3) if a door is clicked on.
- * Uses the globals doorWidth, doorHeight, xDoorGap, and yDoorGap,
- * to determine if the click was in fact on a door.
- * Door one is on the left, xDoorGap from the left and yDoorGap from the top.
- * There is xDoorGap between each door.
- */
- void mousePressed() {
- if (mousePressed && mouseX > xDoorGap + doorWidth && mouseY > yDoorGap + doorHeight) {
- doorSelected = 1;
- } else {
- doorSelected = 0;
- }
- if (mousePressed && mouseX > xDoorGap+doorWidth+xDoorGap + doorWidth && mouseY > yDoorGap + doorHeight) {
- doorSelected = 2;
- } else {
- doorSelected = 0;
- }
- if (mousePressed && mouseX > xDoorGap+doorWidth+xDoorGap+doorWidth+xDoorGap + doorWidth && mouseY > yDoorGap + doorHeight) {
- doorSelected = 3;
- } else {
- doorSelected = 0;
- }
- }
- /**
- * Respond to some button click by updating the game state and the display as necessary.
- * The game state is comprised of the globals:
- * gameStage - START, DOOR_SELECTED, or END
- * doorSelected - the player selected door (1, 2, or 3) or 0 if no door is selected
- * winner - the random assigned winning door (1, 2, or 3)
- */
- void mouseReleased() {
- // need to complete
- }
- /**
- * Draws the doors with one open and displays instructions for player's next move
- * (i.e. switch or stay?). The door opened is not the selected door and not the winner door.
- * @param selected - the door the user selected 1, 2, or 3.
- * @param winner - the winning door 1, 2, or 3.
- */
- void openOneDoor(int selected, int winner) {
- // need to complete
- }
- /*
- * Opens all of the doors showing player lost.
- */
- void displayYouLose() {
- // need to complete
- }
- /*
- * Opens all of the doors showing player won.
- */
- void displayYouWin() {
- // need to complete
- }
- /*
- * Draw all three doors closed and print some instructions.
- */
- void startRound() {
- textFont(buttonFont, 48);
- background(255);
- drawDoors(false, false, false);
- winner = int(random(3) + 1); // pick the winning door
- textFont(buttonFont, 48);
- text("Pick a door", width/2, height*0.8);
- }
- /*
- * Draws three doors, each can be either open or closed.
- */
- void drawDoors(boolean open1, boolean open2, boolean open3) {
- drawDoor(1, xDoorGap, yDoorGap, doorWidth, doorHeight, open1, winner);
- drawDoor(2, xDoorGap+doorWidth+xDoorGap, yDoorGap, doorWidth, doorHeight, open2, winner);
- drawDoor(3, xDoorGap+doorWidth+xDoorGap+doorWidth+xDoorGap, yDoorGap, doorWidth, doorHeight, open3, winner);
- }
- // without draw() the mouse handlers don't work
- void draw() {
- // nothing should go in here for this program
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement