mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Boxing 15 - ported to java
This commit is contained in:
42
15 Boxing/java/Player.java
Normal file
42
15 Boxing/java/Player.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* The Player class model the user and compuer player
|
||||
*/
|
||||
public class Player {
|
||||
private final String name;
|
||||
private final Punch bestPunch;
|
||||
private final Punch vulnerability;
|
||||
private boolean isPlayer = false;
|
||||
|
||||
public Player(String name, Punch bestPunch, Punch vulnerability) {
|
||||
this.name = name;
|
||||
this.bestPunch = bestPunch;
|
||||
this.vulnerability = vulnerability;
|
||||
this.isPlayer = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Player with random Best Punch and Vulnerability
|
||||
*/
|
||||
public Player(String name) {
|
||||
this.name = name;
|
||||
|
||||
int b1;
|
||||
int d1;
|
||||
|
||||
do {
|
||||
b1 = Basic.randomOf(4);
|
||||
d1 = Basic.randomOf(4);
|
||||
} while (b1 == d1);
|
||||
|
||||
this.bestPunch = Punch.fromCode(b1);
|
||||
this.vulnerability = Punch.fromCode(d1);
|
||||
}
|
||||
|
||||
public boolean isPlayer() { return isPlayer; }
|
||||
public String getName() { return name; }
|
||||
public Punch getBestPunch() { return bestPunch; }
|
||||
|
||||
public boolean hitVulnerability(Punch punch) {
|
||||
return vulnerability == punch;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user