Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. public class Duelist {
  2.  
  3. private String name;
  4. private double probabilityOfHitting;
  5. private boolean alive = true;
  6.  
  7.  
  8. //Only declared instance variables. Must created setters and getters
  9. public void setName(String newName){
  10. name = newName;
  11. }
  12. //name setter created
  13.  
  14. public void setProbabilityOfHitting( double newProbabilityOfHitting){
  15. probabilityOfHitting = newProbabilityOfHitting;
  16. }
  17. //probability of hitting setter created
  18. public void setAlive(boolean newAlive){
  19. alive = newAlive;
  20. }
  21. //name setter created
  22. //now must create getters
  23. public String getName(){
  24. return name;
  25. }
  26. //created the name getter
  27. public double getProbabilityOfHitting(){
  28. return probabilityOfHitting;
  29. }
  30. //created the probability of hitting getter
  31. public boolean getAlive(){
  32.  
  33. return alive;
  34. }
  35. //created the alive getter
  36. //no constructors created before
  37. public Duelist(String tempName, double tempProbability){
  38. name = tempName;
  39. probabilityOfHitting = tempProbability;
  40. }
  41. //constructor is now created
  42. //need to create a method for the duelists to shoot at each other
  43. public void shootAtTarget(Duelist target){
  44. double randomNum = Math.random();
  45. if (this.probabilityOfHitting ==1){
  46. target.setAlive(false);
  47. target.getAlive();
  48. }
  49.  
  50. else if (randomNum <= this.probabilityOfHitting){
  51. target.setAlive(false);
  52. target.getAlive();
  53. }
  54. else {
  55. target.getAlive();
  56. }
  57. }
  58. }
  59.  
  60.  
  61. public class Tester {
  62. public static void main(String[] args) {
  63.  
  64. int winsA = 0;
  65. int winsB = 0;
  66. int winsC = 0;
  67.  
  68. Duelist aaron = new Duelist("Aaron",(1/3));
  69. Duelist bob = new Duelist("Bob", (1/2));
  70. Duelist charlie = new Duelist("Charlie", 1);
  71.  
  72. if(aaron.getAlive() == true){
  73.  
  74. if(charlie.getAlive()== true){
  75. aaron.shootAtTarget(charlie);
  76. }
  77. else if(bob.getAlive() == true){
  78. aaron.shootAtTarget(bob);
  79. }
  80. else{
  81. winsA++;
  82. }
  83.  
  84. }
  85. else if(bob.getAlive() == true){
  86. if(charlie.getAlive() == true){
  87. bob.shootAtTarget(charlie);
  88. }
  89. else if(aaron.getAlive() == true){
  90. bob.shootAtTarget(aaron);
  91. }
  92. else{
  93. winsB++;
  94. }
  95. }
  96. else{
  97. if (bob.getAlive() == true){
  98. charlie.shootAtTarget(bob);
  99. }
  100. else if(aaron.getAlive() == true){
  101. charlie.shootAtTarget(aaron);
  102. }
  103. else{
  104. winsC++;
  105. }
  106. }
  107. System.out.println(winsA);
  108. System.out.println(winsB);
  109. System.out.println(winsC);
  110.  
  111.  
  112. }
  113.  
  114. }
  115.  
  116. Duelist aaron = new Duelist("Aaron",(1/3));
  117.  
  118. Duelist aaron = new Duelist("Aaron",(1.0/3.0));
  119.  
  120. private static Random random = new Random(0);
  121.  
  122. //need to create a method for the duelists to shoot at each other
  123. public void shootAtTarget(Duelist target)
  124. {
  125. double randomNum = random.nextDouble();
  126. if (randomNum <= this.probabilityOfHitting)
  127. {
  128. target.setAlive(false);
  129. }
  130. }
  131.  
  132. int alive = 3;
  133. while (alive > 1)
  134. {
  135. System.out.println("Status: A:"+aaron.getAlive()+" B:"+bob.getAlive()+" C:"+charlie.getAlive());
  136. if (aaron.getAlive())
  137. {
  138. if(charlie.getAlive())
  139. {
  140. System.out.println("A shoots at C");
  141. aaron.shootAtTarget(charlie);
  142. if (!charlie.getAlive())
  143. {
  144. System.out.println("A killed C");
  145. alive--;
  146. }
  147. }
  148. else if(bob.getAlive())
  149. {
  150. System.out.println("A shoots at B");
  151. aaron.shootAtTarget(bob);
  152. if (!bob.getAlive())
  153. {
  154. System.out.println("A killed B");
  155. alive--;
  156. }
  157. }
  158. }
  159. // Other cases ...
  160. }
  161.  
  162. if (aaron.getAlive())
  163. {
  164. winsA++;
  165. }
  166. if (bob.getAlive())
  167. {
  168. winsB++;
  169. }
  170. if (charlie.getAlive())
  171. {
  172. winsC++;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement