- Player.java
- Wednesday, October 17th, 2007 at 7:24:17am MDT
- /**
- Player class for Text Fighter
- - 20070928, Cheung Ho Yin
- */
- import java.util.Random;
- public class Player {
- // Instance variables
- private int power = 100;
- // Constructors
- public Player() {}
- // Methods
- public int getPower() { return power; }
- public void addPower( int x ) { power += x; }
- public boolean isDead() { return power>=0?false:true; }
- public void attack( Player victim ) {
- // Is the victim already dead?
- if( victim.isDead() ){
- return;
- }
- // Is the player attacking himself? Assume a break.
- if( victim.getName() == getName() ){
- return;
- }
- // Attack other players
- if(diff==0) diff=1;
- if(r.nextDouble() < 0.5){ // Win! Get power difference from the other party
- addPower( diff );
- victim.addPower( -diff );
- } else { // Lose! Give power difference to the other party
- addPower( -diff );
- victim.addPower( diff );
- }
- // Is somebody dead after this turn?
- }
- // Test main program
- int counter=0;
- // Define new players
- Player p1 = new Player("Ho Yin");
- Player p2 = new Player("Justin");
- do {
- p2.attack(p1);
- } while ( !p1.isDead() && !p2.isDead() );
- }
- }
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.