Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1.  
  2. /**
  3. * The class <b>Statistics</b> accumulates information about a series of games:
  4. * <ol>
  5. * <li>Number of game played</li>
  6. * <li>Number of times the switching strategy was successful</li>
  7. * <li>Number of times the switching strategy was not successful</li>
  8. * <li>Number of times each door has the prize behind it</li>
  9. * <li>Number of times each door was chosen by the player</li>
  10. * <li>Number of times each door was open by the host</li>
  11. * </ol>
  12. *
  13. * @author gvj (gvj@eecs.uottawa.ca)
  14. *
  15. */
  16. public class Statistics {
  17. private int numberOfGames;
  18. private int switchWins;
  19. private int switchLose;
  20. private int doorSelectedA;
  21. private int doorSelectedB;
  22. private int doorSelectedC;
  23. private int hostDoorA;
  24. private int hostDoorB;
  25. private int hostDoorC;
  26. private int prizeDoorA;
  27. private int prizeDoorB;
  28. private int prizeDoorC;
  29. private int doorNumber;
  30. private int[] selectedDoors;
  31. private int[] prizeDoors;
  32. private int[] openDoors;
  33. private Door prizeDoorVariable;
  34. private Door selectedDoorVariable;
  35.  
  36. // ADD HERE YOUR MEMBER VARIABLES
  37.  
  38. /**
  39. * Initializes the statistics.
  40. *
  41. * @param numberOfDoors the number of doors used
  42. */
  43. public Statistics(int numberOfDoors){
  44. openDoors = new int[numberOfDoors];
  45. prizeDoors = new int[numberOfDoors];
  46. selectedDoors = new int[numberOfDoors];
  47. for(int i = 0; i < numberOfDoors; i++){
  48. prizeDoors[i] = 0;
  49. selectedDoors[i]=0;
  50. openDoors[i]=0;
  51. }
  52. numberOfGames = 0;
  53. switchWins = 0;
  54. switchLose = 0;
  55. doorNumber = numberOfDoors;
  56.  
  57. selectedDoors = new int[numberOfDoors];
  58.  
  59.  
  60. }
  61.  
  62. /**
  63. * Updates statistics after one game.
  64. * @param doorArry the list of Doors used during the game
  65. */
  66. public void updateStatistics(Door[] doorArray){
  67. // REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
  68. for(int i = 0; i<doorNumber;i++){
  69. if(doorArray[i].hasPrize()){
  70. prizeDoors[i] = prizeDoors[i]++;
  71. }
  72. else if (doorArray[i].hasPrize()){
  73. prizeDoorVariable=doorArray[i];
  74. }
  75. else if(doorArray[i].isOpen()){
  76. openDoors[i]=openDoors[i]++;
  77. }
  78. else if(doorArray[i].isChosen()){
  79. selectedDoors[i] = selectedDoors[i]++;
  80. }
  81. else if(doorArray[i].isChosen()){
  82. selectedDoorVariable = doorArray[i];
  83. }
  84.  
  85.  
  86. }
  87. numberOfGames = numberOfGames++;
  88. if(selectedDoorVariable == prizeDoorVariable){
  89. switchWins = switchWins++;
  90. }
  91. else{
  92. switchLose = switchLose++;
  93. }
  94. }
  95.  
  96.  
  97.  
  98. /**
  99. * @return Returns the complete statistics information
  100. */
  101. public String toString(){
  102. // REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
  103.  
  104. String string = "";
  105. string = "Number of games played: " + numberOfGames + "\nStaying strategy won " + switchLose + " games (" + 100*switchLose/numberOfGames + "%)\nSwitching stragety won " + switchWins + " games (" + 100*switchWins/numberOfGames + "%)\nSelected doors:\n";
  106. for(int i = 0;i<doorNumber;i++){
  107. string = string + "door" + (i+1) + ": " + selectedDoors[i] + " (" + 100*selectedDoors[i]/numberOfGames + "%)" + "\r\n";
  108. }
  109. string = string + "Winning Doors: " + "\r\n";
  110. for(int i = 0; i < doorNumber; i++){
  111. string = string + "door " + (i+1) + ": " + prizeDoors[i] + " (" + 100*prizeDoors[i]/numberOfGames + "%)" + "\r\n";
  112. }
  113. string = string + "Opened doors: " + "\r\n";
  114. for(int i = 0; i<doorNumber; i++){
  115. string = string + "door " + (i+1) + ": " + openDoors[i] + " (" + 100*openDoors[i]/numberOfGames + "%)" + "\r\n";
  116. }
  117. return string;
  118.  
  119. }
  120.  
  121. /**
  122. * @return Returns the complete statistics information in CSV format
  123. */
  124. public String toCSV(){
  125. String stringCSV = "";
  126. stringCSV = "Number of games, " + numberOfGames + "\nNumber of doors,3 \n,Win,Loss\nStaying strategy, " +switchLose + "," + switchWins + "\nSwitching strategy, " + switchWins + "," + switchLose +"\nSelected doors,Winning doors,Open doors" + "\r\n";
  127. for(int i = 0; i<doorNumber; i++){
  128. stringCSV = stringCSV + "Door" + (1+i) + " ," + selectedDoors[i] + "," + prizeDoors[i] + "," + openDoors[i] + "\r\n";
  129.  
  130. }
  131. return stringCSV;
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement