Fix some bugs with new Probability type and unit tests

This commit is contained in:
nossr50 2023-03-26 13:41:31 -07:00
parent 36adde7b3d
commit 6845fb4c44
3 changed files with 24 additions and 11 deletions

View File

@ -9,16 +9,28 @@ public interface Probability {
/**
* The value of this Probability
* Should return a result between 0 and 1 (inclusive)
* 1 should represent something that will always succeed
* 0.5 should represent something that succeeds around half the time
* etc
* A value of 1 or greater represents something that will always succeed
* A value of around 0.5 represents something that succeeds around half the time
* A value of 0 represents something that will always fail
*
* @return the value of probability
*/
double getValue();
static @NotNull Probability ofPercent(double percentageValue) {
return new ProbabilityImpl(percentageValue);
/**
* Create a new Probability with the given value
* A value of 100 would represent 100% chance of success
* A value of 50 would represent 50% chance of success
* A value of 0 would represent 0% chance of success
* A value of 1 would represent 1% chance of success
* A value of 0.5 would represent 0.5% chance of success
* A value of 0.01 would represent 0.01% chance of success
*
* @param percentage the value of the probability
* @return a new Probability with the given value
*/
static @NotNull Probability ofPercent(double percentage) {
return new ProbabilityImpl(percentage);
}
/**
@ -29,7 +41,7 @@ public interface Probability {
* @return true for succeeding, false for failing
*/
static private boolean isSuccessfulRoll(double probabilityValue) {
return (probabilityValue * 100) >= ThreadLocalRandom.current().nextDouble(100D);
return (probabilityValue) >= ThreadLocalRandom.current().nextDouble(1D);
}
/**

View File

@ -10,14 +10,15 @@ public class ProbabilityImpl implements Probability {
/**
* Create a probability with a static value
*
* @param staticProbability the value to assign to this probability
* @param percentage the percentage value of the probability
*/
ProbabilityImpl(double staticProbability) throws ValueOutOfBoundsException {
if (staticProbability < 0) {
ProbabilityImpl(double percentage) throws ValueOutOfBoundsException {
if (percentage < 0) {
throw new ValueOutOfBoundsException("Value should never be negative for Probability! This suggests a coding mistake, contact the devs!");
}
probabilityValue = staticProbability;
// Convert to a 0-1 floating point representation
probabilityValue = percentage / 100.0D;
}
ProbabilityImpl(double xPos, double xCeiling, double probabilityCeiling) throws ValueOutOfBoundsException {

View File

@ -78,7 +78,7 @@ class ProbabilityTest {
@ParameterizedTest
@MethodSource("provideProbabilitiesForWithinExpectations")
void testOddsExpectationsImplConstructor(Probability probability, double expectedWinPercent) {
void testOddsExpectationsConstructor(Probability probability, double expectedWinPercent) {
assertExpectations(probability, expectedWinPercent);
}