Shop OBEX P1 Docs P2 Docs Learn Events
Having fun with the "Random" pbasic instruction: "Horse Race" simulation game. — Parallax Forums

Having fun with the "Random" pbasic instruction: "Horse Race" simulation game.

Douglas MDouglas M Posts: 9
edited 2016-06-01 00:19 in BASIC Stamp

[Horse Racing Game* ]

By Douglas M.
Project Date: December, 2015

horse%20race%20game.jpg

A fun exercise using the RANDOM instruction.
The Sparkfun Serial LCD four 7 segment display is used to represent the "race track" and each digit to represent a "race horse".
To generate a random number to advance the decimal digits or the "horses" 0 through 9. The first "horse" to reach 9 wins the race.
The race can be started with a button press, or to start a new race automatically. Change modes by "press and holding"
the button at the end of the race, hold in for about five to seven seconds.

Some additional features:

One Button Control:

- LED display brightness can be adjusted. Hold the button in while restarting/resetting the Basic Stamp 2.

- To make it more like a game, you may pick a "race horse to win". Hold in the button during the intro display,
"Hors Race". You only get this chance to pick at power up. Push the button for the race horse number you want
when the dashes are scrolling by. The game will keep track of the total number of wins and total races up to 250

Minimal Parts: Really easy to prototype and/or build.

As for most of my programs, they are not optimized, I may use different intructions to get the same results,
better use of the instructions and organization of the sub-routines may increase program efficiency.
Most of my programs are small and there is plenty of "memory to work with", so feel free to experiment.

The pbasic code is attached and commented.
The seed value is generated through the use of the "button instruction", "loopAround:" and "newRandomNumber:' routines.

Having fun with program variations:

- Instead on generating a new random number for each horse, in turn.
Each bit in the random number is used (hence random bits) by using the pbasic shift ">>" instruction.
If the bit is 1, the horse advances by one place, if the bit is 0, the the horse does not advance by 1 place.
When the bits are used up, a new random number is generated and the routine repeats until we have a winner.

- The Sparkfun Serial LCD four 7 segment display is digit and segment addressable,
so it would have been easy to update each digit as each of the "horses" increase in value.
However I thought it would be fun to keep the score by using one large number sending it out to the display once each loop.


[noparse] 'claculations for the four digit display output
placeTotal = placeTotal + (horse(0) * 1000) 'horse(0) is the most significant digit on the display (thousands column)
placeTotal = placeTotal + (horse(1) * 100) 'and horse(3) is the least significant digit on the display (ones column)
placeTotal = placeTotal + (horse(2) * 10) 'this works because of no carries, once any one of the horse(X) reach nine
placeTotal = placeTotal + (horse(3) * 1) 'you have a winner and the game is over
RETURN
[/noparse]


Material Used:
BS2 Basic Stamp
24 pin dip socket
Sparkfun Serial LCD four 7 segment display
2.1 power jack
two pole screw terminal - for battery or other external source
mini pcb (0.1) mount normally open push button switch
.1 spaced perf board
4 x 32 machine screws and nuts
Power supply: 6-9 volts dc.
2 - 10k ohm resistors
small pcb mount single pole, single throw slide switch - not yet installed. To be used as a power on - off switch.
- refer to the picture, just below the button is a jumper wire, that space is reserved for the slide switch.
(I did not have one in my parts drawer, I will have order one with my next project list from Jameco, or Mouser, etc...)

No separate voltage regulator required!
The voltage regulator on board the Basic Stamp 2 has enough capacity to supply the Sparkfun Serial LCD four 7 segment display.


Youtube Video:



Having fun with the "random" number instruction for the Basic Stamp 2 by Parallax Inc.
Using the LCD four 7 segment display to represent the "race track" and each digit to represent a "race horse".
Generate a random number to advance the decimal digits 0 through 9. The first digit or "horse" to reach 9 wins the race.
The video plays through three races. Audio was added to the first of the three races just for fun.

*Inspired by my friend at Emmaus Junior High School (Emmaus, PA), who, on a TRS-80 (model 1) computer,
created a simple "Horse Race" game which scrolled a number of
rows across the screen at a RANDOM progression.
The first row to reach the "finish line" won the race.
The TRS-80 computer was new technolgy back then (late 1970's).

Thank you for interest,
Doug

Posted June 2016
Sign In or Register to comment.