Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * MontyHall.java
- * Feargal Wright
- * 19/12/14
- */
- import java.util.Random;
- public class MontyHall {
- //variables
- private int chosenDoor;
- private int revealedDoor;
- private int winningDoor;
- private int remainingDoor;
- private int winCounter;
- //Create Random
- Random rand = new Random();
- // constructor
- public MontyHall() {
- //give variables default values
- chosenDoor = 0;
- revealedDoor = 0;
- winningDoor = 0;
- remainingDoor = 0;
- winCounter = 0;
- }//end of constructor
- //setters
- public void setChosenDoor(int chosenDoor) {//setter start
- this.chosenDoor = chosenDoor;//set chosen door from the app class
- }//setter end
- //Computation to reveal door and select winnig door
- public void revealCompute() {//start reveal
- switch (chosenDoor) {//start of case switch
- case 1:
- revealedDoor = rand.nextBoolean() ? 2 : 3;
- if (revealedDoor == 2) {
- remainingDoor = 3;
- winningDoor = rand.nextBoolean() ? 1 : 3;
- } else if (revealedDoor == 3) {
- remainingDoor = 2;
- winningDoor = rand.nextBoolean() ? 1 : 2;
- }
- break;
- case 2:
- revealedDoor = rand.nextBoolean() ? 1 : 3;
- if (revealedDoor == 1) {
- remainingDoor = 3;
- winningDoor = rand.nextBoolean() ? 2 : 3;
- } else if (revealedDoor == 2) {
- remainingDoor = 1;
- winningDoor = rand.nextBoolean() ? 1 : 2;
- }
- break;
- case 3:
- revealedDoor = rand.nextBoolean() ? 1 : 2;
- if (revealedDoor == 1) {
- remainingDoor = 2;
- winningDoor = rand.nextBoolean() ? 2 : 3;
- } else if (revealedDoor == 2) {
- remainingDoor = 1;
- winningDoor = rand.nextBoolean() ? 1 : 3;
- }
- break;
- }//end of case switch
- }//end reveal
- //getter
- public int getRevealedDoor() {//start getter
- return revealedDoor;//return which door is revealed
- }//end getter
- // if the user decides to swap this allows it
- public void swapCompute() {//swap start
- int swapStore = chosenDoor;//Int swap store facilitates the switching of the int values if the user changes door
- chosenDoor = remainingDoor;//change the chosen door to the remaing door
- remainingDoor = swapStore;//change the remaing door to the swapStore value of the chosen door
- if (winningDoor == chosenDoor) {//if start
- winCounter = 1;//set the win counter to 1 / a win
- }//if end
- else {
- winCounter = 0;
- }
- }//swap computation end
- //result getter
- public int Result() {//getter start
- return winCounter;//return the win counter if = 1 Win!
- }//getter end
- }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement