Advertisement
Guest User

Untitled

a guest
May 24th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.64 KB | None | 0 0
  1. package com.example.lib;
  2.  
  3. import java.util.Random;
  4.  
  5. public class PlaySimulator {
  6.  
  7.     /**
  8.      * The closer the random generated number is to the lower bound, the more chance of success the offense will have
  9.      */
  10.     private final int lowerBound = -100;
  11.  
  12.     /**
  13.      * If the randomly generated number is within the range from the lower bound to this point,
  14.      * then the offense gets a "critical" on that play. IE A touchdown is scored, regardless of situation
  15.      */
  16.     private int offensiveCriticalBound = -95;
  17.  
  18.     /**
  19.      * The closer the random generated number is to the upper bound, the more chance of success the defense will have
  20.      */
  21.     private final int upperBound = 100;
  22.  
  23.     /**
  24.      * If the randomly generated number is within the range from the upper bound to this point,
  25.      * then the defense gets a "critical" on that play. IE a touchdown is scored, regardless of situation
  26.      */
  27.     private int defensiveCriticalBound = 99;
  28.  
  29.     /**
  30.      * Anything to the left (less than) the breakingPoint will be equal to an offensive win for that play.
  31.      * Anything to the right (greater than) or equal to the breakingPoint will be equal to a defensive
  32.      * win for that play.
  33.      */
  34.     private int breakingPoint = 0;
  35.  
  36.     private final static double smallSuccessPercentage = .65;
  37.  
  38.     private final static double mediumSuccessPercentage = .85;
  39.     /**
  40.      * This will be the "short success" boundary. This will be from 0-this percent in a range.
  41.      *
  42.      * A short success for the offense will be something between a 2-4 yard gain (randomly chosen)
  43.      * A short success for the defense will be something between a 1 and 2 yard gain
  44.      */
  45.     private final Range shortSuccessOffenseRange = new Range(2,4);
  46.     private final Range shortSuccessDefenseRange = new Range(1,2);
  47.  
  48.     /**
  49.      * This will be the "medium success" boundary. This will be from the shortSuccess point, up to this point.
  50.      * Both the {@link #smallSuccessPercentage} and the {@link #mediumSuccessPercentage} are not inclusive variables, which means
  51.      * that the range that we are checking against will not include the point specified.
  52.      *
  53.      * A medium success for the offense will be something between 4 and 10 yards gained
  54.      * A medium success for the defense will be something between -2 and 0 yards gained
  55.      *
  56.      * Anything over the {@link #mediumSuccessPercentage} will be considered a "large success".
  57.      *
  58.      * A large success for the offense will be anything more than 10 yards.
  59.      * A large success for the defense will be anything less than -2 yards gained
  60.      */
  61.     private final Range mediumSuccessOffenseRange = new Range(shortSuccessOffenseRange.maximumPoint,10);
  62.     private final Range mediumSuccessDefenseRange = new Range(-2,shortSuccessDefenseRange.minimumPoint);
  63.  
  64.  
  65.     /**
  66.      * This switch is thrown after {@link #simulate()}} has been called.
  67.      */
  68.     private boolean hasBeenSimulated = false;
  69.  
  70.     /**
  71.      * This will be set to true if the play ends in a touchdown
  72.      */
  73. //    private boolean touchdownStatus = false;
  74.  
  75. //    private int finalResult = 0;
  76.  
  77. //    private int yardsGained = 0;
  78.  
  79.     private PlayResults mResults;
  80.  
  81.     /**
  82.      * This will be used as one of the final results for the play
  83.      */
  84.     public final static int OFFENSIVE_WIN = 0x111111;
  85.  
  86.     /**
  87.      * This will be used as the other final result for the play
  88.      */
  89.     public final static int DEFENSIVE_WIN = 0x222222;
  90.  
  91.     public final static int LARGE_SUCCESS = 0x333333;
  92.  
  93.     public final static int MEDIUM_SUCCESS = 0x444444;
  94.  
  95.     public final static int SMALL_SUCCESS = 0x555555;
  96.  
  97.     PlaySimulator(){
  98.         reset();
  99.     }
  100.  
  101.     final void simulate(){
  102.         if(!hasBeenSimulated) {
  103.             final int result = new Random().nextInt(upperBound - lowerBound) + lowerBound;
  104.             int minYardRange=0;
  105.             int maxYardRange=0;
  106.             if (result <= offensiveCriticalBound) {
  107.                 mResults.setTouchdownStatus(true).setFinalResult(OFFENSIVE_WIN);
  108.             }
  109.             if (result >= defensiveCriticalBound) {
  110.                 mResults.setTouchdownStatus(true).setFinalResult(DEFENSIVE_WIN);
  111.             }
  112.             if (mResults.getFinalResult() == 0) {
  113.                 boolean dun = false;
  114.                 if (result < breakingPoint) {
  115.                     mResults.setFinalResult(OFFENSIVE_WIN);
  116.                     final int breakingPointGap = Math.abs(lowerBound)-Math.abs(breakingPoint);
  117.                     if(result < (breakingPoint - (breakingPointGap*mediumSuccessPercentage))){
  118.                         minYardRange = mediumSuccessOffenseRange.maximumPoint;
  119.                         maxYardRange = 100;
  120.                         dun = true;
  121.                     }
  122.                     if(!dun && result < breakingPoint-(breakingPointGap*smallSuccessPercentage)){
  123.                         minYardRange = shortSuccessOffenseRange.maximumPoint;
  124.                         maxYardRange = mediumSuccessOffenseRange.maximumPoint;
  125.                         dun=true;
  126.                     }
  127.                     if(!dun){
  128.                         minYardRange = shortSuccessOffenseRange.minimumPoint;
  129.                         maxYardRange = shortSuccessOffenseRange.maximumPoint;
  130.                     }
  131.                 } else {
  132.                     final int breakingPointGap = Math.abs(upperBound)-Math.abs(breakingPoint);
  133.                     mResults.setFinalResult(DEFENSIVE_WIN);
  134.                     if(result > breakingPoint+(breakingPointGap*mediumSuccessPercentage)){
  135.                         minYardRange = mediumSuccessDefenseRange.maximumPoint;
  136.                         maxYardRange = -100;
  137.                         dun=true;
  138.                     }
  139.                     if(!dun && result > breakingPoint+(breakingPointGap*smallSuccessPercentage)){
  140.                         minYardRange = mediumSuccessDefenseRange.maximumPoint;
  141.                         maxYardRange = shortSuccessDefenseRange.maximumPoint;
  142.                         dun=true;
  143.                     }
  144.                     if(!dun){
  145.                         minYardRange = shortSuccessDefenseRange.maximumPoint;
  146.                         maxYardRange = shortSuccessDefenseRange.minimumPoint;
  147.                     }
  148.                 }
  149.             }
  150.             final int rangeModifier = 100;
  151.             final int bound = (((maxYardRange-minYardRange)+rangeModifier)+minYardRange)-rangeModifier;
  152. //            Check to see if bound is 0. If it is, we assume that the randomly chosen value will be 0,
  153. //            so we just set it to avoid any error with the Random class
  154.             mResults.setYardsGainedOnPlay(bound == 0 ? 0 :
  155. //                    Due to the nature of the Random class, we cant actually
  156. //                    use a negative bound, so we check after the random is set
  157. //                    and if it is supposed to be negative, we set it to be the
  158. //                    appropriate side of 0 (be it positive or negative)
  159.                     new Random().nextInt(Math.abs(bound))*(bound<0 ? -1 : 1));
  160.             hasBeenSimulated = true;
  161.         }
  162.     }
  163.  
  164.     final boolean isTouchdown(){
  165.         return mResults.getTouchdownStatus();
  166.     }
  167.  
  168.     final int resultStatus(){
  169.         return mResults.getFinalResult();
  170.     }
  171.  
  172.     final int getYardsGained(){
  173.         return mResults.getYardsGained();
  174.     }
  175.  
  176.     final void reset(){
  177.         mResults = new PlayResults();
  178.         hasBeenSimulated = false;
  179.     }
  180.  
  181.     private class Range{
  182.         private final int minimumPoint;
  183.         private final int maximumPoint;
  184.  
  185.         private Range(final int minPoint, final int maxPoint){
  186.             minimumPoint = minPoint;
  187.             maximumPoint = maxPoint;
  188.         }
  189.     }
  190.  
  191.     final class PlayResults{
  192.         private boolean wasTouchdown = false;
  193.         private int finalResult = 0;
  194.         private int yardsGainedOnPlay = 0;
  195.  
  196.         PlayResults(){}
  197.  
  198.         private final PlayResults setTouchdownStatus(final boolean isTouchdown){
  199.             wasTouchdown = isTouchdown;
  200.             return this;
  201.         }
  202.  
  203.         private final PlayResults setFinalResult(final int result){
  204.             finalResult = result;
  205.             return this;
  206.         }
  207.  
  208.         private final PlayResults setYardsGainedOnPlay(final int yardsGained){
  209.             yardsGainedOnPlay = yardsGained;
  210.             return this;
  211.         }
  212.  
  213.         public final boolean getTouchdownStatus(){
  214.             return wasTouchdown;
  215.         }
  216.  
  217.         public final int getFinalResult(){
  218.             return finalResult;
  219.         }
  220.  
  221.         public final int getYardsGained(){
  222.             return yardsGainedOnPlay;
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement