Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1. /**
  2.  * Lets the user play the Monty Hall game where
  3.  * you pick a door, Monty opens a losing remaining door and asks if you wish
  4.  * to switch. There is a prize behind just one door.
  5.  *
  6.  * This is a mostly incomplete outline of the problem.
  7.  * Be sure and edit this opening comment as well.
  8.  *
  9.  * @author Charlie McDowell
  10.  */
  11.  
  12. PFont buttonFont;
  13. // used by mousePressed to detect and indicate door selection
  14. int doorWidth, doorHeight, xDoorGap, yDoorGap, doorSelected = 0;
  15.  
  16. // stages of the game: START - all doors closed, DOOR_SELECTED - one door open, END - all doors open
  17. final int START = 1, DOOR_SELECTED = 2, END = 3;
  18. int gameStage = START;
  19.  
  20. int winner; // randomly selected winning door number (1, 2, or 3)
  21. int rounds = 0; // total number of rounds played
  22. int numWins = 0; // number of times the player ultimately selected the winning door
  23.  
  24. /**
  25.  * Display the initial panel with the 3 doors and some instructions.
  26.  */
  27. void setup() {
  28.   size(600, 400);
  29.   buttonFont = loadFont("AppleCasual-48.vlw");
  30.   textFont(buttonFont, 48);
  31.   textAlign(CENTER);
  32.   // make door size proportional to the display size
  33.   doorWidth = width/4;
  34.   // 3 doors plus 4 door gaps should span the display
  35.   xDoorGap = (width - 3*doorWidth)/4;
  36.   doorHeight = (int)(height*.6);
  37.   yDoorGap = doorHeight/10; // distance from the top of the display
  38.   startRound();
  39.  
  40. }
  41. /**
  42.  * Draw a door open or closed at the specified location and size (x,y,width,height).
  43.  * (x,y) is the location of the upper left corner of the door.
  44.  *
  45.  * @param doorNum - the door number (1, 2, or 3)
  46.  * @param x - the x value of the upper left corner of the door
  47.  * @param y - the y value of the upper left corner of the door
  48.  * @param width - the width of the door
  49.  * @param height - the height of the door
  50.  * @param open - true if the door is shown as open (displaying what is behind) or false if closed
  51.  * @param winner - the number of the winning door. Used along with doorNum to decide what to display if the door is open.
  52.  */
  53. void drawDoor(int doorNum, int x, int y, int width, int height, boolean open, int winner) {
  54.  fill(0);
  55.  if (open) {
  56.    fill(255);
  57.  }
  58.  rect(x,y,width,height);
  59. }
  60.  
  61.  
  62. /**
  63.  * Sets the global variable doorSelected to 0 if no door is selected
  64.  * or the door number (1, 2, or 3) if a door is clicked on.
  65.  * Uses the globals doorWidth, doorHeight, xDoorGap, and yDoorGap,
  66.  * to determine if the click was in fact on a door.
  67.  * Door one is on the left, xDoorGap from the left and yDoorGap from the top.
  68.  * There is xDoorGap between each door.
  69.  */
  70. void mousePressed() {
  71.    if (mousePressed && mouseX > xDoorGap + doorWidth && mouseY > yDoorGap + doorHeight) {
  72.    doorSelected = 1;
  73. } else {
  74.   doorSelected = 0;
  75. }
  76. if (mousePressed && mouseX > xDoorGap+doorWidth+xDoorGap + doorWidth && mouseY > yDoorGap + doorHeight) {
  77.   doorSelected = 2;
  78. } else {
  79.   doorSelected = 0;
  80. }
  81. if (mousePressed && mouseX > xDoorGap+doorWidth+xDoorGap+doorWidth+xDoorGap + doorWidth && mouseY > yDoorGap + doorHeight) {
  82.   doorSelected = 3;
  83. } else {
  84.   doorSelected = 0;
  85. }
  86. }
  87.  
  88. /**
  89.  * Respond to some button click by updating the game state and the display as necessary.
  90.  * The game state is comprised of the globals:
  91.  *   gameStage - START, DOOR_SELECTED, or END
  92.  *   doorSelected - the player selected door (1, 2, or 3) or 0 if no door is selected
  93.  *   winner - the random assigned winning door (1, 2, or 3)
  94.  */
  95. void mouseReleased() {
  96.   // need to complete
  97. }
  98.  
  99. /**
  100.  * Draws the doors with one open and displays instructions for player's next move
  101.  * (i.e. switch or stay?). The door opened is not the selected door and not the winner door.
  102.  * @param selected - the door the user selected 1, 2, or 3.
  103.  * @param winner - the winning door 1, 2, or 3.
  104.  */
  105. void openOneDoor(int selected, int winner) {
  106.   // need to complete
  107. }
  108.  
  109. /*
  110.  * Opens all of the doors showing player lost.
  111.  */
  112. void displayYouLose() {
  113.    // need to complete
  114. }
  115.  
  116. /*
  117.  * Opens all of the doors showing player won.
  118.  */
  119. void displayYouWin() {
  120.     // need to complete
  121. }
  122.  
  123. /*
  124.  * Draw all three doors closed and print some instructions.
  125.  */
  126. void startRound() {
  127.   textFont(buttonFont, 48);
  128.   background(255);
  129.   drawDoors(false, false, false);
  130.   winner = int(random(3) + 1); // pick the winning door
  131.   textFont(buttonFont, 48);
  132.   text("Pick a door", width/2, height*0.8);
  133. }
  134.  
  135. /*
  136.  * Draws three doors, each can be either open or closed.
  137.  */
  138. void drawDoors(boolean open1, boolean open2, boolean open3) {
  139.   drawDoor(1, xDoorGap, yDoorGap, doorWidth, doorHeight, open1, winner);
  140.   drawDoor(2, xDoorGap+doorWidth+xDoorGap, yDoorGap, doorWidth, doorHeight, open2, winner);
  141.   drawDoor(3, xDoorGap+doorWidth+xDoorGap+doorWidth+xDoorGap, yDoorGap, doorWidth, doorHeight, open3, winner);
  142. }
  143.  
  144. // without draw() the mouse handlers don't work
  145. void draw() {
  146.   // nothing should go in here for this program
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement