Advertisement
Guest User

Untitled

a guest
May 27th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.33 KB | None | 0 0
  1. package com.example.lib;
  2.  
  3. import java.util.Random;
  4.  
  5. public class PlaySimulator {
  6.  
  7. //    These are temporary until we figure out how to dynamically change them later
  8.     /**
  9.      * If the randomly generated number is within the range from the lower bound to this point,
  10.      * then the offense gets a "critical" on that play. IE A touchdown is scored, regardless of situation
  11.      */
  12.     private int offensiveCriticalBound = -95;
  13.  
  14.     /**
  15.      * If the randomly generated number is within the range from the upper bound to this point,
  16.      * then the defense gets a "critical" on that play. IE a touchdown is scored, regardless of situation
  17.      */
  18.     private int defensiveCriticalBound = 99;
  19.  
  20.     /**
  21.      * Anything to the left (less than) the breakingPoint will be equal to an offensive win for that play.
  22.      * Anything to the right (greater than) or equal to the breakingPoint will be equal to a defensive
  23.      * win for that play.
  24.      */
  25.     private int breakingPoint = 0;
  26.  
  27.     /**
  28.      * This switch is thrown after {@link #simulate()} has been called.
  29.      */
  30.     private boolean hasBeenSimulated = false;
  31.  
  32.     private PlayResults mResults = new PlayResults();
  33.  
  34.     private CriticalRange mCritRange;
  35.  
  36.     PlaySimulator(){
  37.         reset();
  38.     }
  39.  
  40.     final void reset(){
  41.         mResults.reset();
  42. //        Need to figure out how we are going to adjust the Critical Range
  43.         mCritRange = new CriticalRange(offensiveCriticalBound, defensiveCriticalBound);
  44.         hasBeenSimulated = false;
  45.     }
  46.  
  47.     final PlayResults getResults(){
  48.         return mResults;
  49.     }
  50.  
  51.     final void simulate(){
  52.         if(hasBeenSimulated) return ;
  53.  
  54. //      Breaking Point will eventually be a dynamically calculated number
  55.         Result result = new Result(0, mCritRange);
  56.         Range yards = new Range(0,0);
  57.         if (result.isOffensiveWin()) {
  58.             yards = handleOffensiveWin(result, mResults);
  59.         }
  60.         if (result.isDefensiveWin()){
  61.             yards = handleDefensiveWin(result, mResults);
  62.         }
  63.         final int rangeModifier = 100;
  64.  
  65.         final int bound = (((yards.getMax()-yards.getMin())+rangeModifier)+yards.getMin())-rangeModifier;
  66.  
  67.         mResults = result.getPlayResults();
  68.  
  69. //      Check to see if bound is 0. If it is, we assume that the randomly chosen value will be 0,
  70. //      so we just set it to avoid any error with the Random class
  71. //      TODO We need to calculate how many yards gained on a crit win.
  72.         mResults.setYardsGainedOnPlay(bound == 0 ? 0 :
  73. //              Due to the nature of the Random class, we cant actually
  74. //              use a negative bound, so we check after the random is set
  75. //              and if it is supposed to be negative, we set it to be the
  76. //              appropriate side of 0 (be it positive or negative)
  77.                 new Random().nextInt(Math.abs(bound))*(bound<0 ? -1 : 1));
  78.         hasBeenSimulated = true;
  79.     }
  80.  
  81.     private final Range handleOffensiveWin(final Result result, final PlayResults pResults){
  82.         final int breakingPointGap =
  83.                 Math.abs(Bounds.LOWERBOUND.getBound()-Math.abs(result.getBreakingPoint())) *
  84.                 (result.getGeneratedResult() < 0 ? -1 : 1);
  85.         boolean dun = false;
  86.         final Range yardRange = new Range(0,0);
  87.         pResults.setWinResult(Win.OFFENSIVE_WIN);
  88.  
  89.         if(result.isLargeSuccess(breakingPointGap, Team.OFFENSE)){
  90.             yardRange.setMin(SuccessRange.MEDIUM_OFFENSE_RANGE.getMax());
  91.             yardRange.setMax(100);
  92.             dun = true;
  93.         }
  94.         if(!dun && result.isMediumSuccess(breakingPointGap, Team.OFFENSE)){
  95.             yardRange.setMin(SuccessRange.MEDIUM_OFFENSE_RANGE.getMin());
  96.             yardRange.setMax(SuccessRange.MEDIUM_OFFENSE_RANGE.getMax());
  97.             dun = true;
  98.         }
  99.         if(!dun && result.isSmallSuccess(breakingPointGap, Team.OFFENSE)){
  100.             yardRange.setMin(SuccessRange.SHORT_OFFENSE_RANGE.getMin());
  101.             yardRange.setMin(SuccessRange.SHORT_OFFENSE_RANGE.getMax());
  102.         }
  103.         return yardRange;
  104.     }
  105.  
  106.     private final Range handleDefensiveWin(final Result result, final PlayResults pResults){
  107.         final int breakingPointGap = Math.abs(Bounds.UPPERBOUND.getBound()-Math.abs(result.getBreakingPoint())) *
  108.                 (result.getGeneratedResult() < 0 ? -1 : 1);
  109.         boolean dun = false;
  110.         final Range yardRange = new Range(0,0);
  111.         pResults.setWinResult(Win.DEFENSIVE_WIN);
  112.  
  113.         if(result.isLargeSuccess(breakingPointGap, Team.DEFENSE)){
  114.             yardRange.setMin(SuccessRange.MEDIUM_DEFENSE_RANGE.getMax());
  115.             yardRange.setMax(-100);
  116.             dun = true;
  117.         }
  118.         if(!dun && result.isMediumSuccess(breakingPointGap, Team.DEFENSE)){
  119.             yardRange.setMin(SuccessRange.MEDIUM_DEFENSE_RANGE.getMin());
  120.             yardRange.setMax(SuccessRange.MEDIUM_DEFENSE_RANGE.getMax());
  121.             dun = true;
  122.         }
  123.         if(!dun && result.isSmallSuccess(breakingPointGap, Team.DEFENSE)){
  124.             yardRange.setMin(SuccessRange.SHORT_DEFENSE_RANGE.getMin());
  125.             yardRange.setMax(SuccessRange.SHORT_DEFENSE_RANGE.getMax());
  126.         }
  127.         return yardRange;
  128.     }
  129.  
  130.     enum Bounds{
  131.         LOWERBOUND (-100),
  132.         UPPERBOUND (100);
  133.  
  134.         private final int mBound;
  135.  
  136.         Bounds(final int bound){
  137.             mBound = bound;
  138.         }
  139.  
  140.         final int getBound(){
  141.             return mBound;
  142.         }
  143.     }
  144.  
  145.     enum SuccessRange{
  146.         SHORT_OFFENSE_RANGE  (new Range(2,4)),
  147.         SHORT_DEFENSE_RANGE  (new Range(1,2)),
  148.         MEDIUM_OFFENSE_RANGE (new Range(SHORT_OFFENSE_RANGE.getRange().getMax(), 10)),
  149.         MEDIUM_DEFENSE_RANGE (new Range(-2, SHORT_DEFENSE_RANGE.getRange().getMin()));
  150.  
  151.         private final Range mRange;
  152.  
  153.         SuccessRange(final Range range){
  154.             mRange = range;
  155.         }
  156.  
  157.         final Range getRange(){
  158.             return mRange;
  159.         }
  160.  
  161.         final int getMin(){
  162.             return mRange.getMin();
  163.         }
  164.  
  165.         final int getMax(){
  166.             return mRange.getMax();
  167.         }
  168.     }
  169.  
  170.     enum SuccessPercentage{
  171.         SMALLSUCCESS (.65),
  172.         MEDIUMSUCCESS (.85);
  173.  
  174.         private final double mSuccessPercent;
  175.  
  176.         SuccessPercentage(final double percent){
  177.             mSuccessPercent = percent;
  178.         }
  179.  
  180.         final double getPercent(){
  181.             return mSuccessPercent;
  182.         }
  183.     }
  184.  
  185.     enum Team{
  186.         OFFENSE (0x10),
  187.         DEFENSE (0x11);
  188.  
  189.         private final int mTeam;
  190.  
  191.         Team(final int team){
  192.             mTeam = team;
  193.         }
  194.  
  195.         private final int getTeam(){
  196.             return mTeam;
  197.         }
  198.  
  199.         private final boolean isOffense(){
  200.             return mTeam == OFFENSE.getTeam();
  201.         }
  202.  
  203.         private final boolean isDefense(){
  204.             return mTeam == DEFENSE.getTeam();
  205.         }
  206.     }
  207.  
  208.     enum Win{
  209.         OFFENSIVE_WIN (0x1),
  210.         DEFENSIVE_WIN (0x2),
  211.         NONE (0x0);
  212.  
  213.         private final int mWin;
  214.  
  215.         Win(final int win){
  216.             mWin = win;
  217.         }
  218.  
  219.         private final int getWin(){
  220.             return mWin;
  221.         }
  222.  
  223.         final boolean isOffensive(){
  224.             return mWin == OFFENSIVE_WIN.getWin();
  225.         }
  226.  
  227.         final boolean isDefensive(){
  228.             return mWin == DEFENSIVE_WIN.getWin();
  229.         }
  230.     }
  231.  
  232.     final class CriticalRange{
  233.  
  234.         final int mOffensivePoint;
  235.         final int mDefensivePoint;
  236.  
  237.         CriticalRange(final int offensivePoint, final int defensivePoint){
  238.             mOffensivePoint = offensivePoint;
  239.             mDefensivePoint = defensivePoint;
  240.         }
  241.  
  242.         final boolean isCritical(final int pointInRange){
  243.             if(pointInRange <= mOffensivePoint ||
  244.                pointInRange >= mDefensivePoint){
  245.                 return true;
  246.             } else {
  247.                 return false;
  248.             }
  249.         }
  250.  
  251.         final boolean isOffensiveCrit(final int pointInRange){
  252.             if(isCritical(pointInRange)){
  253.                 return pointInRange <= mOffensivePoint;
  254.             } else {
  255.                 return false;
  256.             }
  257.         }
  258.  
  259.         final boolean isDefensiveCrit(final int pointInRange){
  260.             if(isCritical(pointInRange)){
  261.                 return pointInRange >= mDefensivePoint;
  262.             } else {
  263.                 return false;
  264.             }
  265.         }
  266.  
  267.     }
  268.  
  269.     final class Result {
  270.  
  271.         final int generatedResult;
  272.  
  273.         Win winStatus = Win.NONE;
  274.         final CriticalRange critRange;
  275.         boolean mIsCrit = false;
  276.         final int mBreakingPoint;
  277.  
  278.         Result(final int breakingPoint, final CriticalRange cRange) {
  279.             mBreakingPoint = breakingPoint;
  280.             critRange = cRange;
  281.             generatedResult =
  282.                 new Random().nextInt(
  283.                         Bounds.UPPERBOUND.getBound() - Bounds.LOWERBOUND.getBound()) + Bounds.LOWERBOUND.getBound();
  284.             handleWinCalc();
  285.             if(critRange.isCritical(generatedResult)){
  286.                 handleCrit();
  287.             }
  288.         }
  289.  
  290.         private final void handleWinCalc(){
  291.             if(generatedResult < mBreakingPoint){
  292.                 setOffensiveWin();
  293.             } else {
  294.                 setDefensiveWin();
  295.             }
  296.         }
  297.  
  298.         private final void handleCrit(){
  299.             mIsCrit = true;
  300.             if(critRange.isOffensiveCrit(mBreakingPoint)){
  301.                 setOffensiveWin();
  302.             }
  303.             else if(critRange.isDefensiveCrit(mBreakingPoint)){
  304.                 setDefensiveWin();
  305.             }
  306.  
  307.         }
  308.  
  309.         private final void setOffensiveWin(){
  310.             winStatus = Win.OFFENSIVE_WIN;
  311.         }
  312.  
  313.         private final void setDefensiveWin(){
  314.             winStatus = Win.DEFENSIVE_WIN;
  315.         }
  316.  
  317.         final boolean isOffensiveWin() {
  318.             return winStatus.isOffensive();
  319.         }
  320.  
  321.         final boolean isDefensiveWin() {
  322.             return winStatus.isDefensive();
  323.         }
  324.  
  325.         final Win getWinStatus(){
  326.             return winStatus;
  327.         }
  328.  
  329.         final boolean isCrit(){
  330.             return mIsCrit;
  331.         }
  332.  
  333.         final boolean isOffensiveCritWin(){
  334.             return mIsCrit && winStatus.isOffensive();
  335.         }
  336.  
  337.         final boolean isDefensiveCritWin(){
  338.             return mIsCrit && winStatus.isDefensive();
  339.         }
  340.  
  341.         final int getBreakingPoint() {
  342.             return mBreakingPoint;
  343.         }
  344.  
  345.         final boolean isLargeSuccess(final int breakingPointGap, final Team team) {
  346.             final double mediumSuccessPoint = getMediumSuccessPoint(breakingPointGap);
  347.             final double critPoint = getCritPoint(team);
  348.             if (team.isOffense()) {
  349.                 return ((generatedResult < mediumSuccessPoint) && (generatedResult > critPoint));
  350.             } else {
  351.                 return ((generatedResult > mediumSuccessPoint) && (generatedResult < critPoint));
  352.             }
  353.         }
  354.  
  355.         final boolean isMediumSuccess(final int breakingPointGap, final Team team) {
  356.             final double mediumSuccessPoint = getMediumSuccessPoint(breakingPointGap);
  357.             final double smallSuccessPoint = getSmallSuccessPoint(breakingPointGap);
  358.             if (team.isOffense()) {
  359.                 return ((generatedResult < smallSuccessPoint) && (generatedResult > mediumSuccessPoint));
  360.             } else {
  361.                 return ((generatedResult > smallSuccessPoint) && (generatedResult < mediumSuccessPoint));
  362.             }
  363.         }
  364.  
  365.         final boolean isSmallSuccess(final int breakingPointGap, final Team team) {
  366.             final double smallSuccessPoint = getSmallSuccessPoint(breakingPointGap);
  367.             if (team.isOffense()) {
  368.                 return ((generatedResult > smallSuccessPoint) && (generatedResult < mBreakingPoint));
  369.             } else {
  370.                 return ((generatedResult < smallSuccessPoint) && (generatedResult > mBreakingPoint));
  371.             }
  372.         }
  373.  
  374.         private final double getCritPoint(final Team team) {
  375.             return team.isOffense() ? critRange.mOffensivePoint : critRange.mDefensivePoint;
  376.         }
  377.  
  378.         private final double getMediumSuccessPoint(final int breakingPointGap) {
  379.             return (breakingPointGap * SuccessPercentage.MEDIUMSUCCESS.getPercent() -
  380.                     Math.abs(breakingPoint));
  381.         }
  382.  
  383.         private final double getSmallSuccessPoint(final int breakingPointGap) {
  384.             return (breakingPointGap * SuccessPercentage.SMALLSUCCESS.getPercent() -
  385.                     Math.abs(breakingPoint));
  386.         }
  387.  
  388.         final int getGeneratedResult(){
  389.             return generatedResult;
  390.         }
  391.  
  392.         final PlayResults getPlayResults(){
  393.             return new PlayResults().setWinResult(getWinStatus()).setTouchdownStatus(isCrit());
  394.         }
  395.  
  396.     }
  397.  
  398.     final class PlayResults{
  399.  
  400.         private boolean wasTouchdown = false;
  401.         private Win mWin = Win.NONE;
  402.         private int yardsGainedOnPlay = 0;
  403.  
  404.         PlayResults(){}
  405.  
  406.         private final void reset(){
  407.             wasTouchdown = false;
  408.             mWin = Win.NONE;
  409.             yardsGainedOnPlay = 0;
  410.         }
  411.  
  412.         private final PlayResults setTouchdownStatus(final boolean isTouchdown){
  413.             wasTouchdown = isTouchdown;
  414.             return this;
  415.         }
  416.         private final PlayResults setYardsGainedOnPlay(final int yardsGained){
  417.             yardsGainedOnPlay = yardsGained;
  418.             return this;
  419.         }
  420.  
  421.         private final PlayResults setWinResult(final Win win){
  422.             mWin = win;
  423.             return this;
  424.         }
  425.  
  426.         public final boolean getTouchdownStatus(){
  427.             return wasTouchdown;
  428.         }
  429.  
  430.         public final Win getWinResult(){
  431.             return mWin;
  432.         }
  433.  
  434.         public final int getYardsGained(){
  435.             return yardsGainedOnPlay;
  436.         }
  437.  
  438.     }
  439. }
  440.  
  441. final class Range{
  442.     private int min;
  443.     private int max;
  444.  
  445.     Range(final int minPoint, final int maxPoint){
  446.         setMin(minPoint);
  447.         setMax(maxPoint);
  448.     }
  449.  
  450.     protected final void setMin(final int minimum){
  451.         min = minimum;
  452.     }
  453.  
  454.     protected final void setMax(final int maximum){
  455.         max = maximum;
  456.     }
  457.  
  458.     protected final int getMin(){
  459.         return min;
  460.     }
  461.  
  462.     protected final int getMax(){
  463.         return max;
  464.     }
  465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement