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

Player.java
Sunday, December 16th, 2007 at 6:23:10pm MST 

  1. /**
  2.         Player class for Text Fighter
  3.         - 20070928, Cheung Ho Yin
  4.         */
  5.  
  6. import java.util.Random;
  7.  
  8. public class Player {
  9.  
  10.         // Instance variables
  11.         private String name = "Somebody";
  12.         private int power = 10;
  13.         private static Random r = new Random();
  14.  
  15.         // Constructors
  16.         public Player( String name ){ this.name = name; }
  17.  
  18.         // Methods
  19.         public String getName() { return name; }
  20.         public int getPower() { return power; }
  21.         public void addPower( int x ) { power += x; }
  22.         public boolean isDead() { return power>=0?false:true; }
  23.  
  24.         public void attack( Player victim ) {
  25.                 // Attack other players
  26.                 System.out.printf( "%s attacks %s!\n", getName(), victim.getName() );
  27.                 int diff = Math.abs( victim.getPower() - getPower() );
  28.                 if(diff==0) diff=1;
  29.                 if(r.nextDouble() < 0.5){ // Win! Swap power.
  30.                         System.out.printf( "%s wins", getName() );
  31.                         addPower( diff );
  32.                         victim.addPower( -diff );
  33.                 } else { // Lose! Give power difference to the other party
  34.                         System.out.printf( "%s loses", getName() );
  35.                         addPower( -diff );
  36.                         victim.addPower( diff );
  37.                 }
  38.                 System.out.println();
  39.                 // Is somebody dead after this turn?
  40.                 if(isDead()) System.out.printf( "%s is killed!\n", getName() );
  41.                 if(victim.isDead()) System.out.printf( "%s is killed!\n", victim.getName() );
  42.         }
  43.  
  44.         // Test main program
  45.         public static void main( String [] args ) {
  46.  
  47.                 int counter=0;
  48.                 System.out.print("Java Text Fighter! v20070928\n\n");
  49.  
  50.                 // Define new players
  51.                 Player p1 = new Player("Ho Yin");
  52.                 Player p2 = new Player("Justin");
  53.  
  54.                 do {
  55.                         System.out.printf("== Round %d ==\n", ++counter);
  56.                         System.out.println("Player " + p1.getName() + "'s power = " + p1.getPower());
  57.                         System.out.println("Player " + p2.getName() + "'s power = " + p2.getPower());
  58.                         p2.attack(p1);
  59.                 } while ( !p1.isDead() && !p2.isDead() );
  60.  
  61.                 System.out.println("== Game over ==");
  62.                 System.out.println("Player " + p1.getName() + "'s power = " + p1.getPower());
  63.                 System.out.println("Player " + p2.getName() + "'s power = " + p2.getPower());
  64.         }
  65. }

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
fantasy-obligation
fantasy-obligation