Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.lib;
- import java.util.Random;
- public class PlaySimulator {
- /**
- * The closer the random generated number is to the lower bound, the more chance of success the offense will have
- */
- private final int lowerBound = -100;
- /**
- * If the randomly generated number is within the range from the lower bound to this point,
- * then the offense gets a "critical" on that play. IE A touchdown is scored, regardless of situation
- */
- private int offensiveCriticalBound = -95;
- /**
- * The closer the random generated number is to the upper bound, the more chance of success the defense will have
- */
- private final int upperBound = 100;
- /**
- * If the randomly generated number is within the range from the upper bound to this point,
- * then the defense gets a "critical" on that play. IE a touchdown is scored, regardless of situation
- */
- private int defensiveCriticalBound = 99;
- /**
- * Anything to the left (less than) the breakingPoint will be equal to an offensive win for that play.
- * Anything to the right (greater than) or equal to the breakingPoint will be equal to a defensive
- * win for that play.
- */
- private int breakingPoint = 0;
- private final static double smallSuccessPercentage = .65;
- private final static double mediumSuccessPercentage = .85;
- /**
- * This will be the "short success" boundary. This will be from 0-this percent in a range.
- *
- * A short success for the offense will be something between a 2-4 yard gain (randomly chosen)
- * A short success for the defense will be something between a 1 and 2 yard gain
- */
- private final Range shortSuccessOffenseRange = new Range(2,4);
- private final Range shortSuccessDefenseRange = new Range(1,2);
- /**
- * This will be the "medium success" boundary. This will be from the shortSuccess point, up to this point.
- * Both the {@link #smallSuccessPercentage} and the {@link #mediumSuccessPercentage} are not inclusive variables, which means
- * that the range that we are checking against will not include the point specified.
- *
- * A medium success for the offense will be something between 4 and 10 yards gained
- * A medium success for the defense will be something between -2 and 0 yards gained
- *
- * Anything over the {@link #mediumSuccessPercentage} will be considered a "large success".
- *
- * A large success for the offense will be anything more than 10 yards.
- * A large success for the defense will be anything less than -2 yards gained
- */
- private final Range mediumSuccessOffenseRange = new Range(shortSuccessOffenseRange.maximumPoint,10);
- private final Range mediumSuccessDefenseRange = new Range(-2,shortSuccessDefenseRange.minimumPoint);
- /**
- * This switch is thrown after {@link #simulate()}} has been called.
- */
- private boolean hasBeenSimulated = false;
- /**
- * This will be set to true if the play ends in a touchdown
- */
- // private boolean touchdownStatus = false;
- // private int finalResult = 0;
- // private int yardsGained = 0;
- private PlayResults mResults;
- /**
- * This will be used as one of the final results for the play
- */
- public final static int OFFENSIVE_WIN = 0x111111;
- /**
- * This will be used as the other final result for the play
- */
- public final static int DEFENSIVE_WIN = 0x222222;
- public final static int LARGE_SUCCESS = 0x333333;
- public final static int MEDIUM_SUCCESS = 0x444444;
- public final static int SMALL_SUCCESS = 0x555555;
- PlaySimulator(){
- reset();
- }
- final void simulate(){
- if(!hasBeenSimulated) {
- final int result = new Random().nextInt(upperBound - lowerBound) + lowerBound;
- int minYardRange=0;
- int maxYardRange=0;
- if (result <= offensiveCriticalBound) {
- mResults.setTouchdownStatus(true).setFinalResult(OFFENSIVE_WIN);
- }
- if (result >= defensiveCriticalBound) {
- mResults.setTouchdownStatus(true).setFinalResult(DEFENSIVE_WIN);
- }
- if (mResults.getFinalResult() == 0) {
- boolean dun = false;
- if (result < breakingPoint) {
- mResults.setFinalResult(OFFENSIVE_WIN);
- final int breakingPointGap = Math.abs(lowerBound)-Math.abs(breakingPoint);
- if(result < (breakingPoint - (breakingPointGap*mediumSuccessPercentage))){
- minYardRange = mediumSuccessOffenseRange.maximumPoint;
- maxYardRange = 100;
- dun = true;
- }
- if(!dun && result < breakingPoint-(breakingPointGap*smallSuccessPercentage)){
- minYardRange = shortSuccessOffenseRange.maximumPoint;
- maxYardRange = mediumSuccessOffenseRange.maximumPoint;
- dun=true;
- }
- if(!dun){
- minYardRange = shortSuccessOffenseRange.minimumPoint;
- maxYardRange = shortSuccessOffenseRange.maximumPoint;
- }
- } else {
- final int breakingPointGap = Math.abs(upperBound)-Math.abs(breakingPoint);
- mResults.setFinalResult(DEFENSIVE_WIN);
- if(result > breakingPoint+(breakingPointGap*mediumSuccessPercentage)){
- minYardRange = mediumSuccessDefenseRange.maximumPoint;
- maxYardRange = -100;
- dun=true;
- }
- if(!dun && result > breakingPoint+(breakingPointGap*smallSuccessPercentage)){
- minYardRange = mediumSuccessDefenseRange.maximumPoint;
- maxYardRange = shortSuccessDefenseRange.maximumPoint;
- dun=true;
- }
- if(!dun){
- minYardRange = shortSuccessDefenseRange.maximumPoint;
- maxYardRange = shortSuccessDefenseRange.minimumPoint;
- }
- }
- }
- final int rangeModifier = 100;
- final int bound = (((maxYardRange-minYardRange)+rangeModifier)+minYardRange)-rangeModifier;
- // Check to see if bound is 0. If it is, we assume that the randomly chosen value will be 0,
- // so we just set it to avoid any error with the Random class
- mResults.setYardsGainedOnPlay(bound == 0 ? 0 :
- // Due to the nature of the Random class, we cant actually
- // use a negative bound, so we check after the random is set
- // and if it is supposed to be negative, we set it to be the
- // appropriate side of 0 (be it positive or negative)
- new Random().nextInt(Math.abs(bound))*(bound<0 ? -1 : 1));
- hasBeenSimulated = true;
- }
- }
- final boolean isTouchdown(){
- return mResults.getTouchdownStatus();
- }
- final int resultStatus(){
- return mResults.getFinalResult();
- }
- final int getYardsGained(){
- return mResults.getYardsGained();
- }
- final void reset(){
- mResults = new PlayResults();
- hasBeenSimulated = false;
- }
- private class Range{
- private final int minimumPoint;
- private final int maximumPoint;
- private Range(final int minPoint, final int maxPoint){
- minimumPoint = minPoint;
- maximumPoint = maxPoint;
- }
- }
- final class PlayResults{
- private boolean wasTouchdown = false;
- private int finalResult = 0;
- private int yardsGainedOnPlay = 0;
- PlayResults(){}
- private final PlayResults setTouchdownStatus(final boolean isTouchdown){
- wasTouchdown = isTouchdown;
- return this;
- }
- private final PlayResults setFinalResult(final int result){
- finalResult = result;
- return this;
- }
- private final PlayResults setYardsGainedOnPlay(final int yardsGained){
- yardsGainedOnPlay = yardsGained;
- return this;
- }
- public final boolean getTouchdownStatus(){
- return wasTouchdown;
- }
- public final int getFinalResult(){
- return finalResult;
- }
- public final int getYardsGained(){
- return yardsGainedOnPlay;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement