/* This program implements the quincunx game or bean machine (this is sort of like a pacchinko machine if you * are familiar with those). The user specifies some number of slots and the number of "beans" to drop (we * refer to them as balls in this game). The balls drop hitting pins which causes them to randomly bounce * one position to the left or right. The way the pins are set up, we actually move them by 1/2 a slot's * position (see diagram below). When they hit the bottom, they land in a slot. We want to see, after all * balls have dropped, how many balls are in each slot and output that as a diagram. */ /* Let's assume 6 slots. The pattern of pins is shown below. Notice for every other row, the pins are either * directly above a slot or directly above a "wall" between the slots. As a ball drops and hits a pin, it will * either bounce 1 location to the left (-.5) or 1 location to the right (+.5). * * * * * * * * * * * * * * * * * * * * * |_|_|_|_|_|_| */ import java.util.*; // need both Random and Scanner public class Prog7_21 { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random g = new Random(); int size=0; int balls=0; size=get(in, "slots", 5, 10); // get is a generic data verification input method, we use it to get balls=get(in, "balls", 5, 100); // both the number of slots (between 5 and 10) and balls (between 5 and 100) int[] slots = new int[size]; // set up the slots, all of which are initially empty for(int i=0;i=size) location=size-1; // but here to ensure no runtime error return (int)location; // return the int value of location } // input and data verify that the value being requested, as denoted by the String text, is between min and max public static int get(Scanner in, String text, int min, int max) { int temp = 0; do{ System.out.print("Enter number of " + text + " between " + min + " and " + max + " "); temp = in.nextInt(); }while(tempmax); return temp; } // output the balls in the slots public static void output(int[] slots, int size) { int max = getMax(slots, size); // the tallest stack of balls will be max while(max>0) { // work our way downward from max for(int i=0;i=max) System.out.print("\t0"); else System.out.print("\t"); max--; // decrement max to indicate one fewer balls for the next iteration through the slots System.out.println(); // output a blank line to start the next row of output } } public static int getMax(int[] slots, int size) { int max = slots[0]; for(int i=1;imax) max = slots[i]; return max; } }