- Unnamed
- Friday, October 5th, 2007 at 8:42:20am MDT
- import java.util.Random;
- public class SuperPlayers
- {
- //define Power and Difference and Round
- private int Power=100,Difference,Round=1;
- //define the Player Name
- private String Name;
- //define the Player is dead or not
- public boolean isDead=false;
- Random randomNumber;
- public SuperPlayers(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(SuperPlayers A)
- { //to check whether a player is dead or not
- if (getPower() <= 0)
- {
- isDead=true;
- System.out.printf("==========GameOver===========\nWinner is %s!!!!!!", A.getName());
- return;
- }
- if (A.getPower() <= 0)
- {
- A.isDead=true;
- System.out.printf("==========GameOver===========\nWinner is %s!!!!!!", getName());
- return;
- }
- //print out which round for this attack
- System.out.println("====Round " + 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!");
- /*if own Power>enermy'sPower then own Power increases by the difference
- and enermy's Power decreases by ther difference*/
- if(Power>A.getPower())
- {
- Difference=(Power-A.getPower());
- setPower(Power,Difference);
- A.setPower(A.getPower(),-Difference);
- }
- else
- /*if own Power=enermy'sPower then own Power increases by1
- and enermy's Power decreases by 1*/
- {
- if (Power == A.getPower())
- {
- setPower(Power,1);
- A.setPower(A.getPower(),-1);
- }
- else
- {
- /*if own Power<enermy'sPower then own Power increases to the enermy's Power
- and enermy's Power decreases to the winner's Power*/
- if(Power<A.getPower())
- {
- Difference=(A.getPower()-getPower());
- setPower(getPower(),Difference);
- A.setPower(A.getPower(),-Difference);
- }
- }
- }
- }
- else
- {
- //vice versa
- System.out.println(getName()+" loses!");
- if(Power > A.getPower())
- {
- Difference=(Power-A.getPower());
- setPower(Power,-Difference);
- A.setPower(A.getPower(),Difference);
- }
- else
- {
- if (Power == A.getPower())
- {
- setPower(Power,-1);
- A.setPower(A.getPower(),1);
- }
- else
- {
- if (Power < A.getPower())
- {
- Difference=(A.getPower()-Power);
- A.setPower(getPower(),Difference);
- setPower(getPower(),-Difference);
- }
- }
- }
- }
- //show out players hp at last
- System.out.println("J POWER="+getPower());
- System.out.println("H POWER="+A.getPower());
- }
- }
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.