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 {
- // These are temporary until we figure out how to dynamically change them later
- /**
- * 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;
- /**
- * 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;
- /**
- * This switch is thrown after {@link #simulate()} has been called.
- */
- private boolean hasBeenSimulated = false;
- private PlayResults mResults = new PlayResults();
- private CriticalRange mCritRange;
- PlaySimulator(){
- reset();
- }
- final void reset(){
- mResults.reset();
- // Need to figure out how we are going to adjust the Critical Range
- mCritRange = new CriticalRange(offensiveCriticalBound, defensiveCriticalBound);
- hasBeenSimulated = false;
- }
- final PlayResults getResults(){
- return mResults;
- }
- final void simulate(){
- if(hasBeenSimulated) return ;
- // Breaking Point will eventually be a dynamically calculated number
- Result result = new Result(0, mCritRange);
- Range yards = new Range(0,0);
- if (result.isOffensiveWin()) {
- yards = handleOffensiveWin(result, mResults);
- }
- if (result.isDefensiveWin()){
- yards = handleDefensiveWin(result, mResults);
- }
- final int rangeModifier = 100;
- final int bound = (((yards.getMax()-yards.getMin())+rangeModifier)+yards.getMin())-rangeModifier;
- mResults = result.getPlayResults();
- // 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
- // TODO We need to calculate how many yards gained on a crit win.
- 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;
- }
- private final Range handleOffensiveWin(final Result result, final PlayResults pResults){
- final int breakingPointGap =
- Math.abs(Bounds.LOWERBOUND.getBound()-Math.abs(result.getBreakingPoint())) *
- (result.getGeneratedResult() < 0 ? -1 : 1);
- boolean dun = false;
- final Range yardRange = new Range(0,0);
- pResults.setWinResult(Win.OFFENSIVE_WIN);
- if(result.isLargeSuccess(breakingPointGap, Team.OFFENSE)){
- yardRange.setMin(SuccessRange.MEDIUM_OFFENSE_RANGE.getMax());
- yardRange.setMax(100);
- dun = true;
- }
- if(!dun && result.isMediumSuccess(breakingPointGap, Team.OFFENSE)){
- yardRange.setMin(SuccessRange.MEDIUM_OFFENSE_RANGE.getMin());
- yardRange.setMax(SuccessRange.MEDIUM_OFFENSE_RANGE.getMax());
- dun = true;
- }
- if(!dun && result.isSmallSuccess(breakingPointGap, Team.OFFENSE)){
- yardRange.setMin(SuccessRange.SHORT_OFFENSE_RANGE.getMin());
- yardRange.setMin(SuccessRange.SHORT_OFFENSE_RANGE.getMax());
- }
- return yardRange;
- }
- private final Range handleDefensiveWin(final Result result, final PlayResults pResults){
- final int breakingPointGap = Math.abs(Bounds.UPPERBOUND.getBound()-Math.abs(result.getBreakingPoint())) *
- (result.getGeneratedResult() < 0 ? -1 : 1);
- boolean dun = false;
- final Range yardRange = new Range(0,0);
- pResults.setWinResult(Win.DEFENSIVE_WIN);
- if(result.isLargeSuccess(breakingPointGap, Team.DEFENSE)){
- yardRange.setMin(SuccessRange.MEDIUM_DEFENSE_RANGE.getMax());
- yardRange.setMax(-100);
- dun = true;
- }
- if(!dun && result.isMediumSuccess(breakingPointGap, Team.DEFENSE)){
- yardRange.setMin(SuccessRange.MEDIUM_DEFENSE_RANGE.getMin());
- yardRange.setMax(SuccessRange.MEDIUM_DEFENSE_RANGE.getMax());
- dun = true;
- }
- if(!dun && result.isSmallSuccess(breakingPointGap, Team.DEFENSE)){
- yardRange.setMin(SuccessRange.SHORT_DEFENSE_RANGE.getMin());
- yardRange.setMax(SuccessRange.SHORT_DEFENSE_RANGE.getMax());
- }
- return yardRange;
- }
- enum Bounds{
- LOWERBOUND (-100),
- UPPERBOUND (100);
- private final int mBound;
- Bounds(final int bound){
- mBound = bound;
- }
- final int getBound(){
- return mBound;
- }
- }
- enum SuccessRange{
- SHORT_OFFENSE_RANGE (new Range(2,4)),
- SHORT_DEFENSE_RANGE (new Range(1,2)),
- MEDIUM_OFFENSE_RANGE (new Range(SHORT_OFFENSE_RANGE.getRange().getMax(), 10)),
- MEDIUM_DEFENSE_RANGE (new Range(-2, SHORT_DEFENSE_RANGE.getRange().getMin()));
- private final Range mRange;
- SuccessRange(final Range range){
- mRange = range;
- }
- final Range getRange(){
- return mRange;
- }
- final int getMin(){
- return mRange.getMin();
- }
- final int getMax(){
- return mRange.getMax();
- }
- }
- enum SuccessPercentage{
- SMALLSUCCESS (.65),
- MEDIUMSUCCESS (.85);
- private final double mSuccessPercent;
- SuccessPercentage(final double percent){
- mSuccessPercent = percent;
- }
- final double getPercent(){
- return mSuccessPercent;
- }
- }
- enum Team{
- OFFENSE (0x10),
- DEFENSE (0x11);
- private final int mTeam;
- Team(final int team){
- mTeam = team;
- }
- private final int getTeam(){
- return mTeam;
- }
- private final boolean isOffense(){
- return mTeam == OFFENSE.getTeam();
- }
- private final boolean isDefense(){
- return mTeam == DEFENSE.getTeam();
- }
- }
- enum Win{
- OFFENSIVE_WIN (0x1),
- DEFENSIVE_WIN (0x2),
- NONE (0x0);
- private final int mWin;
- Win(final int win){
- mWin = win;
- }
- private final int getWin(){
- return mWin;
- }
- final boolean isOffensive(){
- return mWin == OFFENSIVE_WIN.getWin();
- }
- final boolean isDefensive(){
- return mWin == DEFENSIVE_WIN.getWin();
- }
- }
- final class CriticalRange{
- final int mOffensivePoint;
- final int mDefensivePoint;
- CriticalRange(final int offensivePoint, final int defensivePoint){
- mOffensivePoint = offensivePoint;
- mDefensivePoint = defensivePoint;
- }
- final boolean isCritical(final int pointInRange){
- if(pointInRange <= mOffensivePoint ||
- pointInRange >= mDefensivePoint){
- return true;
- } else {
- return false;
- }
- }
- final boolean isOffensiveCrit(final int pointInRange){
- if(isCritical(pointInRange)){
- return pointInRange <= mOffensivePoint;
- } else {
- return false;
- }
- }
- final boolean isDefensiveCrit(final int pointInRange){
- if(isCritical(pointInRange)){
- return pointInRange >= mDefensivePoint;
- } else {
- return false;
- }
- }
- }
- final class Result {
- final int generatedResult;
- Win winStatus = Win.NONE;
- final CriticalRange critRange;
- boolean mIsCrit = false;
- final int mBreakingPoint;
- Result(final int breakingPoint, final CriticalRange cRange) {
- mBreakingPoint = breakingPoint;
- critRange = cRange;
- generatedResult =
- new Random().nextInt(
- Bounds.UPPERBOUND.getBound() - Bounds.LOWERBOUND.getBound()) + Bounds.LOWERBOUND.getBound();
- handleWinCalc();
- if(critRange.isCritical(generatedResult)){
- handleCrit();
- }
- }
- private final void handleWinCalc(){
- if(generatedResult < mBreakingPoint){
- setOffensiveWin();
- } else {
- setDefensiveWin();
- }
- }
- private final void handleCrit(){
- mIsCrit = true;
- if(critRange.isOffensiveCrit(mBreakingPoint)){
- setOffensiveWin();
- }
- else if(critRange.isDefensiveCrit(mBreakingPoint)){
- setDefensiveWin();
- }
- }
- private final void setOffensiveWin(){
- winStatus = Win.OFFENSIVE_WIN;
- }
- private final void setDefensiveWin(){
- winStatus = Win.DEFENSIVE_WIN;
- }
- final boolean isOffensiveWin() {
- return winStatus.isOffensive();
- }
- final boolean isDefensiveWin() {
- return winStatus.isDefensive();
- }
- final Win getWinStatus(){
- return winStatus;
- }
- final boolean isCrit(){
- return mIsCrit;
- }
- final boolean isOffensiveCritWin(){
- return mIsCrit && winStatus.isOffensive();
- }
- final boolean isDefensiveCritWin(){
- return mIsCrit && winStatus.isDefensive();
- }
- final int getBreakingPoint() {
- return mBreakingPoint;
- }
- final boolean isLargeSuccess(final int breakingPointGap, final Team team) {
- final double mediumSuccessPoint = getMediumSuccessPoint(breakingPointGap);
- final double critPoint = getCritPoint(team);
- if (team.isOffense()) {
- return ((generatedResult < mediumSuccessPoint) && (generatedResult > critPoint));
- } else {
- return ((generatedResult > mediumSuccessPoint) && (generatedResult < critPoint));
- }
- }
- final boolean isMediumSuccess(final int breakingPointGap, final Team team) {
- final double mediumSuccessPoint = getMediumSuccessPoint(breakingPointGap);
- final double smallSuccessPoint = getSmallSuccessPoint(breakingPointGap);
- if (team.isOffense()) {
- return ((generatedResult < smallSuccessPoint) && (generatedResult > mediumSuccessPoint));
- } else {
- return ((generatedResult > smallSuccessPoint) && (generatedResult < mediumSuccessPoint));
- }
- }
- final boolean isSmallSuccess(final int breakingPointGap, final Team team) {
- final double smallSuccessPoint = getSmallSuccessPoint(breakingPointGap);
- if (team.isOffense()) {
- return ((generatedResult > smallSuccessPoint) && (generatedResult < mBreakingPoint));
- } else {
- return ((generatedResult < smallSuccessPoint) && (generatedResult > mBreakingPoint));
- }
- }
- private final double getCritPoint(final Team team) {
- return team.isOffense() ? critRange.mOffensivePoint : critRange.mDefensivePoint;
- }
- private final double getMediumSuccessPoint(final int breakingPointGap) {
- return (breakingPointGap * SuccessPercentage.MEDIUMSUCCESS.getPercent() -
- Math.abs(breakingPoint));
- }
- private final double getSmallSuccessPoint(final int breakingPointGap) {
- return (breakingPointGap * SuccessPercentage.SMALLSUCCESS.getPercent() -
- Math.abs(breakingPoint));
- }
- final int getGeneratedResult(){
- return generatedResult;
- }
- final PlayResults getPlayResults(){
- return new PlayResults().setWinResult(getWinStatus()).setTouchdownStatus(isCrit());
- }
- }
- final class PlayResults{
- private boolean wasTouchdown = false;
- private Win mWin = Win.NONE;
- private int yardsGainedOnPlay = 0;
- PlayResults(){}
- private final void reset(){
- wasTouchdown = false;
- mWin = Win.NONE;
- yardsGainedOnPlay = 0;
- }
- private final PlayResults setTouchdownStatus(final boolean isTouchdown){
- wasTouchdown = isTouchdown;
- return this;
- }
- private final PlayResults setYardsGainedOnPlay(final int yardsGained){
- yardsGainedOnPlay = yardsGained;
- return this;
- }
- private final PlayResults setWinResult(final Win win){
- mWin = win;
- return this;
- }
- public final boolean getTouchdownStatus(){
- return wasTouchdown;
- }
- public final Win getWinResult(){
- return mWin;
- }
- public final int getYardsGained(){
- return yardsGainedOnPlay;
- }
- }
- }
- final class Range{
- private int min;
- private int max;
- Range(final int minPoint, final int maxPoint){
- setMin(minPoint);
- setMax(maxPoint);
- }
- protected final void setMin(final int minimum){
- min = minimum;
- }
- protected final void setMax(final int maximum){
- max = maximum;
- }
- protected final int getMin(){
- return min;
- }
- protected final int getMax(){
- return max;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement