Skip to content

Commit

Permalink
Update TorpedoStore.java
Browse files Browse the repository at this point in the history
  • Loading branch information
paleros authored Apr 26, 2024
1 parent 6bcbd01 commit 6bf1514
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/java/hu/bme/mit/spaceship/TorpedoStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class TorpedoStore {
private double FAILURE_RATE = 0.0; //NOSONAR

private int torpedoCount = 0;
Random generator = new Random();

public TorpedoStore(int numberOfTorpedos){
this.torpedoCount = numberOfTorpedos;
Expand All @@ -30,18 +31,18 @@ public TorpedoStore(int numberOfTorpedos){

public boolean fire(int numberOfTorpedos){
if(numberOfTorpedos < 1 || numberOfTorpedos > this.torpedoCount){
new IllegalArgumentException("numberOfTorpedos");
throw new IllegalArgumentException("numberOfTorpedos");
}

boolean success = false;

// simulate random overheating of the launcher bay which prevents firing
Random generator = new Random();
double r = generator.nextDouble();

if (r >= FAILURE_RATE) {
// successful firing
this.torpedoCount =- numberOfTorpedos;
this.torpedoCount -= numberOfTorpedos;
success = true;
} else {
// simulated failure
Expand Down

0 comments on commit 6bf1514

Please sign in to comment.