Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /**
  2. * Created by Ethan on 10/26/16.
  3. */
  4.  
  5. //TEST OUTPUT:
  6. //Switch Games ->
  7. //Total games: 1041359347
  8. //Total wins: 694272112
  9. //Ratio: 0.6666978528241282
  10. //
  11. //Stay Games ->
  12. //Total games: 1066094998
  13. //Total wins: 355383475
  14. //Ratio: 0.3333506629333671
  15. //
  16. //Random Games ->
  17. //Total games: 676058595
  18. //Total wins: 338024412
  19. //Ratio: 0.499992773555375
  20.  
  21. public class MonteyHall {
  22. public static void main(String[] args) throws InterruptedException {
  23. boolean shouldStop = false;
  24. MonteyHallGame[] games = new MonteyHallGame[3];
  25.  
  26. games[0] = new MonteyHallGame();
  27. games[1] = new MonteyHallGame();
  28. games[2] = new MonteyHallGame();
  29.  
  30. new Thread(new Runnable() {
  31. @Override
  32. public void run() {
  33. while (true) {
  34. games[0].playSwitchGame();
  35. }
  36. }
  37. }).start();
  38.  
  39. new Thread(new Runnable() {
  40. @Override
  41. public void run() {
  42. while (true) {
  43. games[1].playStayGame();
  44. }
  45. }
  46. }).start();
  47.  
  48. new Thread(new Runnable() {
  49. @Override
  50. public void run() {
  51. while(true) {
  52. if (Math.random() > .5) {
  53. games[2].playStayGame();
  54. } else {
  55. games[2].playSwitchGame();
  56. }
  57. }
  58. }
  59. }).start();
  60.  
  61. Thread.sleep(900000);
  62.  
  63. System.out.printf("Switch Games ->%nTotal games: %s%nTotal wins: %s%nRatio: %s%n%n",
  64. games[0].getNumOfGamesPlayed(), games[0].getNumOfGamesWon(), (double) games[0].getNumOfGamesWon() / games[0].getNumOfGamesPlayed());
  65.  
  66. System.out.printf("Stay Games ->%nTotal games: %s%nTotal wins: %s%nRatio: %s%n%n",
  67. games[1].getNumOfGamesPlayed(), games[1].getNumOfGamesWon(), (double) games[1].getNumOfGamesWon() / games[1].getNumOfGamesPlayed());
  68.  
  69. System.out.printf("Random Games ->%nTotal games: %s%nTotal wins: %s%nRatio: %s%n%n",
  70. games[2].getNumOfGamesPlayed(), games[2].getNumOfGamesWon(), (double) games[2].getNumOfGamesWon() / games[2].getNumOfGamesPlayed());
  71.  
  72. System.exit(1); // because i was too lazy to properly multithread, this essentially crashes it. (immediate stop)
  73. }
  74.  
  75.  
  76. }
  77.  
  78. /**
  79. * Created by Ethan on 10/26/16.
  80. */
  81. class MonteyHallGame {
  82. private long numOfGamesPlayed = 0;
  83. private long numOfGamesWon = 0;
  84.  
  85. public void playStayGame() {
  86. boolean[] doors = new boolean[3];
  87. int selectedDoor = (int) (Math.random() * 3);
  88. doors[(int) (Math.random() * 3)] = true;
  89.  
  90. if (doors[selectedDoor]) {
  91. ++numOfGamesWon;
  92. }
  93. ++numOfGamesPlayed;
  94. }
  95.  
  96.  
  97. public void playSwitchGame() {
  98. boolean[] doors = new boolean[3];
  99. int selectedDoor = (int) (Math.random() * 3);
  100. doors[(int) Math.random() * 3] = true;
  101.  
  102. // remove wrong door
  103. for (int i = 0; i < doors.length; i++) {
  104. if (!doors[i] && selectedDoor != i) {
  105. if (!doors[selectedDoor]) {
  106. for (int j = 0; j < doors.length; j++) {
  107. if (j == i) {
  108. continue;
  109. } else if (j != selectedDoor) {
  110. if (doors[j]) {
  111. ++numOfGamesWon;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. ++numOfGamesPlayed;
  119. }
  120.  
  121. public long getNumOfGamesPlayed() {
  122. return numOfGamesPlayed;
  123. }
  124.  
  125. public long getNumOfGamesWon() {
  126. return numOfGamesWon;
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement