Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Character {
- private int hitpoints;
- private boolean isDead;
- public Character(){
- this.hitpoints=100;
- this.isDead=false;
- }
- public void setHP (int points) {
- hitpoints=points;
- }
- public int getHP () {
- return hitpoints;
- }
- public Boolean isDead() {
- return isDead;
- }
- public void Dies() { //does this need to be after it dies? can be changed to check for HP levels and then declare death.
- if (hitpoints<0){
- isDead=true;
- }
- else isDead=false;
- }
- public String isType (){
- return "Character";
- }
- public int attack (Character c) { //LOOK AT THIS!!!
- if (!isDead){//Check whether the attack is possible, attacks all by default as long as the target is alive
- System.out.println("Houston, we have a live one");// line to print if this method is used, I want it overriden and never used.
- }
- return 0;
- //This method always returns 0
- }
- public void HP10 () {
- hitpoints=hitpoints-10;
- }
- public void HP20(){
- hitpoints=hitpoints-20;
- }
- }
- public class World {
- //Here go your private member variables
- private Character[] players;
- private int active;
- private int N;
- public World(int N) { //Constructor for the World objects, takes one integer parameter
- this.active = N;
- this.players = new Character[N]; //Initialize array player of Characters
- this.N = N;
- }
- public void initializeWorld() {
- //Use random to decide which character
- for (int i = 0; i < players.length; i++) { //You need a loop to go through the array
- // if x is 0 then it's good, if it's 1, it's bad, or if it's 2, it's zombie
- this.players[i] = new Character();
- int x = (int) (Math.random() * 3.0);
- if (x == 0) {
- players[i] = new Good();
- } else if (x == 1) {
- players[i] = new Bad();
- } else if (x == 2) {
- players[i] = new Zombie();
- }
- System.out.println(players[i].isType()); //line to test character assignment
- }
- }
- public void printStatus() {
- int G = 0, B = 0, Z = 0;//Variables for good, bad, zombie initialization
- int dead = N - active; //What is a trick to automatically get the dead players?
- System.out.println("Alive playes: " + active + " / " + " Dead players: " + dead); //Print how many active players and how many dead players
- for (int i = 0; i < players.length; i++) { //Loop through the array to find the goods, bads, zombies
- if (players[i].isType().equals("Good")) { //How do you check their types?--> isType method
- G = G + 1;
- }
- if (players[i].isType().equals("Bad")) {
- B = B + 1;
- }
- if (players[i].isType().equals("Zombie")) {
- Z = Z + 1;
- }
- System.out.println(i + " : " + players[i].isType() + " " + players[i].getHP());
- }
- System.out.println(" Good: " + G + " Bad: " + B + " Zombies:" + Z);
- }//Print numbers of good, bad, zombies.
- public void startBattle() {
- int attacks = 0;
- for (int i = 0; i < active; i++) {
- attacks = attacks + 1; //keeping track of number of attacks, have an iddue with double attacks being called
- System.out.println(" amount of attacks" + attacks);
- if (i < active - 1) {//!!!
- if (players[i].attack(players[i + 1]) == (1)) { //zombie special attack
- int points = players[i + 1].getHP();
- players[i + 1] = new Zombie();
- new Zombie().setHP(points);
- } else if ((players[i].attack(players[i + 1]) == 0)) { //regular attack
- players[i + 1].isDead();//You should also check after each *normal* attack, whether the attacked player is dead or not.
- if (players[i + 1].isDead() == true) {//If dead skip next array element
- i++;
- }
- }
- } if (i == (active - 1)) { //!!!
- if ((players[active - 1].attack(players[0]) == 1)) {//zombie attack first character
- int temp = players[i + 1].getHP();
- players[i + 1] = new Zombie();
- new Zombie().setHP(temp);
- } else if ((players[active - 1].attack(players[0]) == 0)) { //regular attack !
- players[0].isDead();//You should also check after each *normal* attack, whether the attacked player is dead or not.
- if (players[0].isDead()) { //If dead skip next array element
- break; //got code from lab session
- }
- }
- }
- }
- }
- /**
- * Created by samtak on 20/10/16.
- */
- public class Good extends Character {
- public Good(){
- super();
- }
- public void setHP (int points) {
- super.setHP(points);
- }
- public int getHP () {
- return super.getHP();
- }
- public Boolean isDead() {
- return super.isDead();
- }
- public void Dies() {
- super.Dies();
- }
- public String isType () { //over ride Characters
- return "Good";
- }
- public int attack (Character c) {
- int calls=0;//LOOK AT THIS!!!
- if (!c.isType().equals("Good")) {//Check whether the attack is possible
- calls=calls+1;
- if (this.isDead()==false){
- c.HP20();//If yes call HP20
- this.Dies(); //check if HP lowers till death
- System.out.println("Houston, we have an attack, G"); //line to print if this method is used, I want it overriding original attack method
- }
- else {
- System.out.println("Good guys stick together"); //line to check good doesn't attack good if detected
- }System.out.println("Calls"+calls);} return 0;
- } //This method always returns 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement