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

Player.java
Wednesday, October 17th, 2007 at 7:24:17am MDT 

  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 = 100;
  13.   private static Random r = new Random();
  14.  
  15.   // Constructors
  16.   public Player( String name ){ this.name = name; }
  17.   public Player() {}
  18.  
  19.   // Methods
  20.   public String getName() { return name; }
  21.   public void setName(String name) { this.name = name; }
  22.   public int getPower() { return power; }
  23.   public void addPower( int x ) { power += x; }
  24.   public boolean isDead() { return power>=0?false:true; }
  25.  
  26.   public void attack( Player victim ) {
  27.     // Is the victim already dead?
  28.     if( victim.isDead() ){
  29.       System.out.printf( "--> %s is already dead!\n", victim.getName() );
  30.       return;
  31.     }
  32.     // Is the player attacking himself? Assume a break.
  33.     if( victim.getName() == getName() ){
  34.       System.out.printf( "--> %s chooses to take a break.\n", getName() );
  35.       return;
  36.     }
  37.     // Attack other players
  38.     System.out.printf( "--> %s attacks %s!\n", getName(), victim.getName() );
  39.     int diff = Math.abs( victim.getPower() - getPower() );
  40.     if(diff==0) diff=1;
  41.     if(r.nextDouble() < 0.5){ // Win! Get power difference from the other party
  42.       System.out.printf( "--> %s wins => ", getName() );
  43.       addPower( diff );
  44.       victim.addPower( -diff );
  45.       System.out.printf( "%s: +%d; %s: -%d.", getName(), diff, victim.getName(), diff );
  46.     } else { // Lose! Give power difference to the other party
  47.       System.out.printf( "--> %s loses => ", getName() );
  48.       addPower( -diff );
  49.       victim.addPower( diff );
  50.       System.out.printf( "%s: -%d; %s: +%d.", getName(), diff, victim.getName(), diff );
  51.     }
  52.     System.out.println();
  53.     // Is somebody dead after this turn?
  54.     if(isDead()) System.out.printf( "--> %s is killed!\n", getName() );
  55.     if(victim.isDead()) System.out.printf( "--> %s is killed!\n", victim.getName() );
  56.    
  57.   }
  58.  
  59.   // Test main program
  60.   public static void main( String [] args ) {
  61.  
  62.     int counter=0;
  63.     // Define new players
  64.     Player p1 = new Player("Ho Yin");
  65.     Player p2 = new Player("Justin");
  66.  
  67.     do {
  68.       System.out.printf("== Round %d ==\n", ++counter);
  69.       System.out.println("Player " + p1.getName() + "'s power = " + p1.getPower());
  70.       System.out.println("Player " + p2.getName() + "'s power = " + p2.getPower());
  71.       p2.attack(p1);
  72.     } while ( !p1.isDead() && !p2.isDead() );
  73.  
  74.     System.out.println("== Game over ==");
  75.     System.out.println("Player " + p1.getName() + "'s power = " + p1.getPower());
  76.     System.out.println("Player " + p2.getName() + "'s power = " + p2.getPower());
  77.   }
  78.  
  79. }

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