Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Untitled
Monday, November 19th, 2007 at 6:19:00am MST 

  1. import java.util.Random;
  2. public class Player
  3. {
  4.         //define Power and Difference and Round
  5.         protected int Power=10,Difference,Round=1;
  6.         //define the Player Name
  7.         protected String Name;
  8.         //define the Player is dead or not
  9.         protected boolean isDead=false;
  10.         Random randomNumber;   
  11.         public Player(String Name)
  12.         {
  13.                 //將傳回的Name傳給這個類別的Name
  14.                 this.Name=Name;
  15.                 //call a Object from Random
  16.                 randomNumber=new Random();
  17.         }
  18.         //call a method for setting the Power for the players after each round
  19.         public void setPower(int Power,int Difference)
  20.         {
  21.                 this.Power=(Power+Difference);
  22.         }
  23.         //call a method to get the value of power each round
  24.         public int getPower()
  25.         {
  26.                 return Power;
  27.         }
  28.         //call a method to get the name from the Players
  29.         public String getName()
  30.         {
  31.                 return Name;
  32.         }
  33.         //call a method for attacking the Players
  34.         public void Attack(Player A)
  35.         {                            
  36.                 int temp=getPower(),temp1=A.getPower();
  37.                 Difference=Math.abs(Power-A.getPower());
  38.                 if (Difference==0)
  39.                 Difference=1;
  40.                 //print out which round for this attack
  41.                 System.out.println("====Round " + Round + "====");
  42.                 //print out who attacks who
  43.                 System.out.println(getName() + " attacks " + A.getName() + "!!!");
  44.                 //to define a player have 50% chance of winning the battle
  45.                 if (randomNumber.nextDouble() <= 0.5 )
  46.                 {
  47.                         System.out.println(getName()+" wins!");
  48.                         setPower(Power,Difference);
  49.                         A.setPower(A.getPower(),-Difference);
  50.                 }
  51.                 else
  52.                 {
  53.                         //vice versa
  54.                         System.out.println(getName()+" loses!");
  55.                         setPower(getPower(),-Difference);
  56.                         A.setPower(A.getPower(),Difference);   
  57.                 }
  58.                 if (A.getPower()<=0)
  59.                 {
  60.                         A.isDead=true;
  61.                         System.out.println(A.getName()+" is killed!!");
  62.                 }
  63.                 if (getPower()<=0)
  64.                 {
  65.                         isDead=true;
  66.                         System.out.println(getName()+" is killed!!");
  67.                 }
  68.                 //show out players hp at last
  69.                 System.out.println(getName()+" POWER  "+temp+"--->"+getPower());
  70.                 System.out.println(A.getName()+" POWER  "+temp1+"--->"+A.getPower());   
  71.         }
  72. }

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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

worth-right
worth-right