This is a Plinko game from the game show The Price Is Right written in Java with Maven and JUnit. A chip is dropped from the middle slot of a 9x13 board, bounces on pegs on its way down, and then lands in one of nine buckets.
This experiment is run 10,000 times, and then the actual results are compared to the expected results based on the probability of the chip landing in each bucket. The actual results compared to the expected results are extremely close, often within less than 1% difference for each bucket.
-
Set Java environment variable in
~/.bash_profile
file:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-18.jdk/Contents/Home"
- Verify Java is installed by running
java -version
. Output looks like:
java version "18" 2022-03-22
Java(TM) SE Runtime Environment (build 18+36-2087)
Java HotSpot(TM) 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)
-
Set Maven environment variables in
~/.bash_profile
file:
export MAVEN_ROOT="$HOME/Code/apache-maven-3.8.5"
export PATH="$MAVEN_ROOT/bin:$PATH"
- Verify Maven is installed by running
mvn --version
. Output looks like:
Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /Users/tylerhawkins/Code/apache-maven-3.8.5
Java version: 18, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-18.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.4", arch: "x86_64", family: "mac"
- Generate the
plinko
project using themaven-archetype-quickstart
plugin:
mvn archetype:generate -DgroupId=com.tylerhawkins.examples -DartifactId=plinko -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
The newly created project directory structure will look like this:
- Add the following properties to the
pom.xml
file to specify the version of Java you'd like to use:
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
</properties>
-
Rename
App.java
andAppTest.java
files to bePlinkoGame.java
andPlinkoGameTest.java
-
In the
pom.xml
file, upgrade JUnit from 3.8.1 to 4.13.2 by replacing the version number -
Write some code to build the Plinko game!
To build and test the app, run the following from the project root directory:
mvn clean package
To run the Plinko app, run the following from the project root directory:
java -cp target/classes com.tylerhawkins.examples.PlinkoGame
Alternatively, you can run the JAR file by doing:
java -cp target/plinko-1.0-SNAPSHOT.jar com.tylerhawkins.examples.PlinkoGame
Either approach will run the app.
The experiment results will then be output to your console and look something like this:
----------------------------
| Slot | Expected | Actual |
|--------------------------|
| 1 | 0.019 | 0.015 |
| 2 | 0.054 | 0.054 |
| 3 | 0.121 | 0.126 |
| 4 | 0.193 | 0.202 |
| 5 | 0.226 | 0.220 |
| 6 | 0.193 | 0.191 |
| 7 | 0.121 | 0.123 |
| 8 | 0.054 | 0.053 |
| 9 | 0.019 | 0.017 |
----------------------------