java: creating a method to operate on two objects (beginner) -


i complete java/programming beginner , trying make basic fighting game. part of goal trying learn object orientation.

i have created player class has variables (att, str etc, hitpoints) , in process of trying make strike() method. method should take data both players , perform basic calculation on hitpoints variable of 1 player object.

i think strike() method should take 2 player objects (which have defined in main method) input , specify variables each player in body code, not sure how this.

code:

public class player {    string name;   int hitpoints = 250;   int str = 10;   int att = 10;   int def = 10;    public void strike(player player)   {       hitpoints = hitpoints - str * att / def;   }  } 

thanks reading far. grateful if point me in right direction.

consider happening here. 1 player striking. being struck. implement separate methods 2 pieces:

public class player {    string name;   private int hitpoints = 250;   private int str = 10;   private int att = 10;   private int def = 10;    public void strike(player other){     other.receivestrike(str * att);   }    private void receivestrike(int strikepower){     int hitpoints = hitpoints - computedamage(strikepower);   }    protected int computedamage(int strikepower){     return strikepower / this.def;   }    public static void main(string[] args){      player 1 = new player();     player 2 = new player();      while (two.hitpoints > 0){             one.strike(two);             system.out.println("two's hitpoints: " + two.hitpoints);     }   } } 

so strike method tells other player how hard struck. player responsible determining how damage strike inflicted , updating state appropriately.

there few keys notice here. first, didn't add code didn't write. arranged methods differently. second, example code in main never calls receivestrike method. calls public method strike. notice player instance not directly touch hitpoints property of player strikes. other player modifies own hitpoints property when struck through private receivestrike method. method uses method protected int computedamage determine how damage strike inflict. allows this:

public class invincibleplayer extends player {   @override   protected int computedamage(int strikepower){     system.out.println("mwa-ha-ha-ha!!! invincible!!! ... almost");     return 0;   } } 

you can strike invincibleplayer want , won't damage @ all. more generally, allows whole range of defensive abilities different player types. that's real benefit of splitting methods this. attacker determines how powerful strike , defender determine how damage does.

that's tip of iceberg of can do. wanted stay close original code possible. 1 next step might create type strike , change receivestrike(int strikepower) receivestrike(strike strike). players respond differently to, say, kick , punch, or counter-strike attacks. that's little advanced now, it's think once you've grasped this.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -