import java.util.Random; public class Player { //define Power and Difference and Round protected int Power=10,Difference,Round=1; //define the Player Name protected String Name; //define the Player is dead or not protected boolean isDead=false; Random randomNumber; public Player(String Name) { //將傳回的Name傳給這個類別的Name this.Name=Name; //call a Object from Random randomNumber=new Random(); } //call a method for setting the Power for the players after each round public void setPower(int Power,int Difference) { this.Power=(Power+Difference); } //call a method to get the value of power each round public int getPower() { return Power; } //call a method to get the name from the Players public String getName() { return Name; } //call a method for attacking the Players public void Attack(Player A) { int temp=getPower(),temp1=A.getPower(); Difference=Math.abs(Power-A.getPower()); if (Difference==0) Difference=1; //print out which round for this attack System.out.println("====Round " + Round + "===="); //print out who attacks who System.out.println(getName() + " attacks " + A.getName() + "!!!"); //to define a player have 50% chance of winning the battle if (randomNumber.nextDouble() <= 0.5 ) { System.out.println(getName()+" wins!"); setPower(Power,Difference); A.setPower(A.getPower(),-Difference); } else { //vice versa System.out.println(getName()+" loses!"); setPower(getPower(),-Difference); A.setPower(A.getPower(),Difference); } if (A.getPower()<=0) { A.isDead=true; System.out.println(A.getName()+" is killed!!"); } if (getPower()<=0) { isDead=true; System.out.println(getName()+" is killed!!"); } //show out players hp at last System.out.println(getName()+" POWER "+temp+"--->"+getPower()); System.out.println(A.getName()+" POWER "+temp1+"--->"+A.getPower()); } }