

Before calling CoinView's flip method at all, MainActivity's flip will set CoinView's loads on heads and the edge according to user specifications (loads are set as integers standing for percentages, but when the setters for loads are called in the CoinView class these integer values are divided by 100). This method accepts a numOfFlips to run in a trial, although SensorEvents will only flip the coin once regardless of what is present next to "Number of Flips" in Images 1-3. This method is called when the used pressed the button Flip and whenever any SensorEvent breaks the SHAKE_THRESHOLD as discussed in Image 9.
#COIN FLIP ANIMATION TRIAL#
Where the CoinView class abstracted the flip of a single coin to its own flip method, the MainActivity class abstracts a trial of any number of coins to its own flip method in Image 10. As another consequence of the accuracy and precision of the accelerometer, we also limit the number of SensorEvents that are regarded by our app in determining the speed of the device with UPDATEPERIOD in Image 8. This means we can have the app flip only when the device travels a certain speed or higher, as defined by SHAKE_THRESHOLD in Image 8. If we keep track of the device's position over time, we may determine its average speed since the last call to onSensorChanged. In our case, the SensorEvent cointains an array of ints which represent the x,y,z coordinates of the device according to preset virtual axes on the device. Every time onSensorChanged is called it receives a SensorEvent containing all values pertaining to said event. However, the accelerometer is much more precise than we need it to be, so we filter its output in the following manner. Whenever the device's accelerometer detects motion, onSensorChanged is called, shown in Image 9. The sensor is initiated to an accelerometer when the app starts. Once the if/else statement is done, the image of the result of the flip is drawn on the canvas and a Result is returned to the calling class - in our case, MainActivity. And if neither condition is met, it is assumed that the number fell within the range of tails. If not, it then checks whether the random number falls within the range formed by the addition of the heads and edge ranges (in which case the number must have fallen in the range of edge). When flip is called, an if/else statement first checks if the random number falls within the range of values representing heads. If there are no custom loads preset by the calling class, the load on heads is preset to 50% (a fair coin), or 33% for both heads and edge if the calling class wishes to include a third side to the coin. The result of a flip is determined by the custom load of heads and the edge of the coin, as specified by the calling class prior to the execution of flip. A random double is generated from java's Math class, which returns any number in the interval from 0 to 1, including 0 but excluding 1. If animation evaluated to true, the animation runs on a separate thread. The method flip returns a Result, a member of the enum declared in the CoinView class visible in Image 4. The constant FRAMES_OF_ANIMATION is used to time the animation correctly on a separate thread, shown below.Ī single flip of a coin was abstracted into a method in the CoinView class called flip, illustrated in Image 8.

The constant SPRITE_SHEET_COLUMNS indicates how many different columns the coin sprite sheet contains - and thus how many different images there are in the coin flipping animation. The coin animation will only run if the boolean animation evaluates to true, and the coin will only have a chance of landing on its edge if threeSides evaluates to true. The CoinView will keep track of the number of heads, tails, or edges it has flipped during a single trial (a single click event of the Flip button or a single SensorChanged event that meets the requirements for a flip - explained later) with three integers. A small inner enum Results represents all possible results from a flip of the coin, as illustrated by Image 4. As a subclass of SurfaceVIew, CoinView cointains a SurfaceHolder object by default, which manages a specific "surface" including a Canvas object used to edit the format and pixels of said surface.
