This project realizes the simulation of the well-known carnival game Tin Can Alley in a slightly modified form. The target is represented by all characters of an LCD filled with x. The simulation starts selecting randomly a character position and shoots a ball o. If the ball hits a tin can the cell is marked as empty, if it hits an empty cell the shot only adds to the total sum of shots. A round lasts until all tin cans are hit and the target is empty. Now the question is: How many balls have we to throw on average to clear the target?
The first throw is a sure hit, its probability is 1. The last throw has a chance of 1/32 to hit the only tin can remaining, if we assume a LCD wit 2 lines and 16 columns. For the las tin can we have to throw 32 balls on average to hit, if there are still 2 tin cans we have to throw 32/2 times to hit one, if there are 3 we have to throw 32/3 to hit one, and so on ...
If we sum all average throws up we get 32 x (1/1 + 1/2 + 1/3 + ... 1/32). That is the number of cells times the sum of the Harmonic series up to that number.
For a 2 x 16 LCD we get 130 throws on average and for a 4 x 20 LCD we get 397. The simulation allows to veryfy these numbers.
To demonstrate some features of the LCD a number of special and custom characters are shown on startup.
A greeting is displayed at start up and the line with special characters is erased by a bouncing ball.Then the simulation starts and the configured number of rounds are played. After each round the results are shown for some time and then the next round is played until all are done.
After all rounds are played we can continue for an other number of rounds by pushing the pushbutton. The results after each round are also written to the serial monitor so that we can see how the average number of shots converges to the calculated value.
The main program contains nothing special. In addition to the standard functions setup() and loop() there are 6 other functions defined to run the simulation and visualize the results. The header file tinCanShooting.h contains the needed definitions and function prototypes.
Interested? Please download the entire program code. The zip-file contains the complete PlatformIO project.
My programming environment is not the native Arduino™ IDE but PlatformIO™ on top of Microsoft's Visual Studio Code™. This combination offers many advantages and allows a much better structuring of the code into several modules especially when we adopt The Object Oriented way.