Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. public class Character {
  2. private int hitpoints;
  3. private boolean isDead;
  4. public Character(){
  5. this.hitpoints=100;
  6. this.isDead=false;
  7. }
  8. public void setHP (int points) {
  9. hitpoints=points;
  10. }
  11. public int getHP () {
  12. return hitpoints;
  13. }
  14. public Boolean isDead() {
  15. return isDead;
  16. }
  17. public void Dies() { //does this need to be after it dies? can be changed to check for HP levels and then declare death.
  18. if (hitpoints<0){
  19. isDead=true;
  20. }
  21. else isDead=false;
  22. }
  23. public String isType (){
  24. return "Character";
  25. }
  26. public int attack (Character c) { //LOOK AT THIS!!!
  27. if (!isDead){//Check whether the attack is possible, attacks all by default as long as the target is alive
  28. System.out.println("Houston, we have a live one");// line to print if this method is used, I want it overriden and never used.
  29. }
  30. return 0;
  31. //This method always returns 0
  32. }
  33. public void HP10 () {
  34. hitpoints=hitpoints-10;
  35. }
  36. public void HP20(){
  37. hitpoints=hitpoints-20;
  38. }
  39. }
  40.  
  41.  
  42. public class World {
  43. //Here go your private member variables
  44. private Character[] players;
  45. private int active;
  46. private int N;
  47.  
  48. public World(int N) { //Constructor for the World objects, takes one integer parameter
  49. this.active = N;
  50. this.players = new Character[N]; //Initialize array player of Characters
  51. this.N = N;
  52. }
  53.  
  54. public void initializeWorld() {
  55. //Use random to decide which character
  56. for (int i = 0; i < players.length; i++) { //You need a loop to go through the array
  57. // if x is 0 then it's good, if it's 1, it's bad, or if it's 2, it's zombie
  58. this.players[i] = new Character();
  59. int x = (int) (Math.random() * 3.0);
  60. if (x == 0) {
  61. players[i] = new Good();
  62. } else if (x == 1) {
  63. players[i] = new Bad();
  64. } else if (x == 2) {
  65. players[i] = new Zombie();
  66. }
  67. System.out.println(players[i].isType()); //line to test character assignment
  68. }
  69. }
  70.  
  71. public void printStatus() {
  72. int G = 0, B = 0, Z = 0;//Variables for good, bad, zombie initialization
  73. int dead = N - active; //What is a trick to automatically get the dead players?
  74. System.out.println("Alive playes: " + active + " / " + " Dead players: " + dead); //Print how many active players and how many dead players
  75. for (int i = 0; i < players.length; i++) { //Loop through the array to find the goods, bads, zombies
  76. if (players[i].isType().equals("Good")) { //How do you check their types?--> isType method
  77. G = G + 1;
  78. }
  79. if (players[i].isType().equals("Bad")) {
  80. B = B + 1;
  81. }
  82. if (players[i].isType().equals("Zombie")) {
  83. Z = Z + 1;
  84. }
  85. System.out.println(i + " : " + players[i].isType() + " " + players[i].getHP());
  86.  
  87. }
  88. System.out.println(" Good: " + G + " Bad: " + B + " Zombies:" + Z);
  89. }//Print numbers of good, bad, zombies.
  90.  
  91. public void startBattle() {
  92. int attacks = 0;
  93.  
  94. for (int i = 0; i < active; i++) {
  95. attacks = attacks + 1; //keeping track of number of attacks, have an iddue with double attacks being called
  96. System.out.println(" amount of attacks" + attacks);
  97. if (i < active - 1) {//!!!
  98.  
  99. if (players[i].attack(players[i + 1]) == (1)) { //zombie special attack
  100. int points = players[i + 1].getHP();
  101. players[i + 1] = new Zombie();
  102. new Zombie().setHP(points);
  103. } else if ((players[i].attack(players[i + 1]) == 0)) { //regular attack
  104. players[i + 1].isDead();//You should also check after each *normal* attack, whether the attacked player is dead or not.
  105. if (players[i + 1].isDead() == true) {//If dead skip next array element
  106. i++;
  107. }
  108. }
  109. } if (i == (active - 1)) { //!!!
  110. if ((players[active - 1].attack(players[0]) == 1)) {//zombie attack first character
  111. int temp = players[i + 1].getHP();
  112. players[i + 1] = new Zombie();
  113. new Zombie().setHP(temp);
  114. } else if ((players[active - 1].attack(players[0]) == 0)) { //regular attack !
  115. players[0].isDead();//You should also check after each *normal* attack, whether the attacked player is dead or not.
  116. if (players[0].isDead()) { //If dead skip next array element
  117. break; //got code from lab session
  118. }
  119. }
  120. }
  121.  
  122.  
  123. }
  124. }
  125.  
  126. /**
  127. * Created by samtak on 20/10/16.
  128. */
  129. public class Good extends Character {
  130. public Good(){
  131. super();
  132. }
  133. public void setHP (int points) {
  134. super.setHP(points);
  135. }
  136. public int getHP () {
  137. return super.getHP();
  138. }
  139. public Boolean isDead() {
  140. return super.isDead();
  141. }
  142. public void Dies() {
  143. super.Dies();
  144. }
  145. public String isType () { //over ride Characters
  146. return "Good";
  147. }
  148. public int attack (Character c) {
  149. int calls=0;//LOOK AT THIS!!!
  150. if (!c.isType().equals("Good")) {//Check whether the attack is possible
  151. calls=calls+1;
  152. if (this.isDead()==false){
  153. c.HP20();//If yes call HP20
  154. this.Dies(); //check if HP lowers till death
  155. System.out.println("Houston, we have an attack, G"); //line to print if this method is used, I want it overriding original attack method
  156. }
  157. else {
  158. System.out.println("Good guys stick together"); //line to check good doesn't attack good if detected
  159. }System.out.println("Calls"+calls);} return 0;
  160.  
  161. } //This method always returns 0
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement