Quincunx is that little game where you drop a ball or disk down rows of nails in a pyramid formation until they land in a slot. This was a project for my class, but it's already been submitted. I am just looking for personal feedback since the professor does not give much in-depth feedback on these assignments. The goals of the program were to:
Ask the user for how many slots they want the game to have (which also determines how many rows of nails there are).
Ask how many balls/disks they wish to drop.
Create a path for each ball dropped to determine what slot it lands in.
Create a histogram to visually represent how many balls are in each slot. It should look like a bell curve.
import java.util.Scanner;
public class Quincunx {
public static void main(String[] args) {
Scanner ballNumber = new Scanner(System.in); //Scanners for slots and number of balls
Scanner slotNumber = new Scanner(System.in);
System.out.println("Enter the Number of slots you want.");
int numberOfSlots = slotNumber.nextInt();
System.out.println("Enter the number of balls you wish to drop into the slots");
int numberOfBalls = ballNumber.nextInt();
System.out.println("You are dropping " + numberOfBalls + " balls into " + numberOfSlots + " slots"); //Displaying what the user entered
int Slots[] = new int[numberOfSlots]; //create an array for the slots based on how many slots the user wants
int rowsOfNails = numberOfSlots - 1; //create the rows of nails to determine path size
eachBallPath(numberOfBalls, rowsOfNails, Slots);
for (int i = 0; i < Slots.length; i++) {
System.out.println("Slot " + i + " has " + Slots[i] + " balls in it");
}
makeAsteriskHistogram(Slots);
}
public static void eachBallPath(int numberOfBalls, int rowsOfNails, int Slots[]) {
for (int i = 1; i <= numberOfBalls; i++) { //loop to make a path fr each ball
String path = new String(pathTaken(rowsOfNails)); //generate and print the path of a ball
System.out.println("Ball " + i + " took the path " + path + " and landed in slot " + ballInSlot(path, Slots));
}
}
public static int ballInSlot(String path, int Slots[]) {
//Take the path string and find the number of times it went right to decide which slot it landed in
int frequencyOfRight = 0;
char R = 'R';
for (int i = 0; i < path.length(); i++) {
char currentLetter = path.charAt(i); // compare each character of the string to 'R' and add to frequency if R is detected
if (currentLetter == R)
frequencyOfRight++;
}
Slots[frequencyOfRight] = Slots[frequencyOfRight] + 1; //the number f Rs is the slot, add a value of 1 to a slot each time a ball lands there
return frequencyOfRight;
}
public static String pathTaken(int rowsOfNails) {
String path = new String(""); //start the path a ball takes
for (int i = 0; i < rowsOfNails; i++) { //do this for every row of nails, as each row is a chance to go left or right
double random = Math.random(); //random number to determine left or right
if (random < 0.5)
path = path + "L";
if (random >= 0.5)
path = path + "R";
}
return path;
}
public static void makeAsteriskHistogram(int Slots[]) {
//find the max value within the dataset to create a scale for the histogram
int max = Slots[0];
for (int i = 0; i < Slots.length; i++) {
if (Slots[i] > max)
max = Slots[i];
} //end of the loop that found the max value for scaling the histogram
for (int k = 0; k < Slots.length; k++) {
System.out.print("_______"); //lines to clearly seperate the graph from the previous output
}
System.out.println();
System.out.println("----------Number of Balls in Slot----------"); //space for aesthetic/organization
System.out.print("#######|"); //aesthetic
for (int i = 1; i <= max; i++) { //create a horizontal scale for the values of the bars
if (i < 10)
System.out.print("0"+ i + " ");
else
System.out.print(i + " ");
}
System.out.println();
System.out.print("#######|--");
for (int i = 0; i <= max; i++) { //add dashed lines under the value scale for aesthetic
System.out.print("-----");
}
System.out.println(); //new line for the labels on the other axis
for (int i = 0; i < Slots.length; i++) {
if (i < 10)
System.out.print(" Slot " + i + "|");
if ( i >= 10)
System.out.print("Slot " + i + "|");//create the label
for (int j = 1; j <= Slots[i]; j++) { //nested loop to print the appropiate amount of *'s to show how many in a slot
System.out.print(" * ");
}
System.out.println();// new line for next slot
}
} //end of makeAsterisksHistogram method
} //End of Class file