Using Random Numbers in Games.
(Revised and Updated)
You must really be a diehard DOS fan to get this far. So now that you're here, let's get down to business (funny buiness, that is). I've provided a routine to simulate flipping a coin and another to select cards. A partial solution to writing a card is also provided.
 Flip a Coin
To simulate the flip of a coin, the RandNumb routine only needs a little tweaking. Rather than test for all possible digits, a test for only half of them will suffice. I use a test for the odd digits, but any subset of five will work as well. A match is assigned the value of '1', while no match is assigned '0'. It could equally well be 'Heads' and 'Tails', or 'Y and 'N'. Whatever suits the need.
:: RandFlip.BAT - Simulates a random coin toss.
:: Tom Lavedas <lavedas@pressroom.bat> :: http://www.pressroom.com/~tglbatch/ @%3 echo %dbgr% off set Rand.Flip=%0 $t $t$h goto:2nd $g %temp%.\~tmp.bat echo @prompt echo %Rand.Flip%$_exit$_> %temp%.\~tmp.bat %comspec%/e:2048/c%temp%.\~tmp.bat | %comspec% > nul for %%v in (echo. echo. %temp%.\~tmp.bat) do %%v :2nd del %temp%.\~tmp.bat set Rand.Flip=0 for %%v in (1 3 5 7 9) do if %1==%2%%v set Rand.Flip=1 :: Set Example=ECHO % to display results % for %%v in (%example%) do %%v Rand.Flip: %Rand.Flip%
How the routine works should be faily obvious, if you understood the RandNumb routine. What can you do with it? I haven't a clue, but maybe you can find a use.
 Deal the Cards
I've found a fun use for the next example -- dealing cards from an infinitely large deck of cards. Can you guess? Right, Casino Blackjack. Why would I bother? For the same reason people climb mountains -- to say I did it. Unfortunately, the Blackjack program is too complicated to explain and has so many parts that I don't currently plan on publishing it here. But, I will reveal parts of it so that the adventuresome can climb there own mountain. First, let's deal the cards.
:: DealCard.BAT - Uses the system clock to pick a playing card. :: DealCard.bat - . :: Returns the Card.Text, Card.Value, Card.Color and Card.Suit. :: The deck size is infinite. :: Tom Lavedas <lavedas@pressroom.bat> :: http://www.pressroom.com/~tglbatch/ @%3 echo %dbgd% off if %Card.Series%'==' set Card.Series=A 2 3 4 5 6 7 8 9 10 J Q K set Card.Value=$t $t$h goto:2nd %Card.Series% $g %temp%.\~tmp.bat echo @prompt echo %0 %Card.Value%$_exit$_> %temp%.\~tmp.bat %comspec% /e:2048 /c %temp%.\~tmp.bat | %comspec% > nul for %%v in (echo. echo. %temp%.\~tmp.bat) do %%v :2nd del %temp%.\~tmp.bat set Card.Value=%0 set Card.Color=Black set Card.Suit=Clubs for %%v in (0 2 4 6 8) do if %1==%2%%v set Card.Color=Red if %Card.Color%==Red set Card.Suit=Diamonds if %Card.Color%==Red for %%v in (0 1 2 3 4) do if %1==%2%%v set Card.Suit=Hearts if %Card.Color%==Black for %%v in (5 6 7 8 9) do if %1==%2%%v set Card.Suit=Spades for %%v in (0 1 2 3 4 5 6 7 8 9) do if %1==%2%%v set _%%v= 0 1 2 3) do shift for %%v in (%_1% x%_2% x%_3% x%_4% x%_5% x%_6% x%_7% x%_8% x%_9% x%_0% for %%v in (0 1 2 3 4 5 6 7 8 9) do set _%%v= set Card.Series=%0 %Card.Series% set Card.Value=%Card.Value% %0 2 goto:3rd %1 %2 %3 %4 %5 %6 %7 %8 %9 for %%v in (1 2 3) do shift %Card.Value% %7 %8 %9 %Card.Series% :3rd for %%v in (1 2 3 4) do shift for %%v in (Text Value) do set Card.%%v=%1 if %Card.Text%==A set Card.Value=1 for %%v in (J Q K) do if %Card.Text%==%%v set Card.Value=10 set Card.Series=%0 %1 %2 %3 %4 %5 %6 %7 %8 %9 for %%v in (1 2 3) do shift set Card.Series=%Card.Series% %7 %8 %9 ::Set Example=ECHO % to display results % for %%v in (%example%) do %%v Card.Value: %Card.Text% of %Card.Suit% (%Card.Value% %Card.Color%) for %%v in (%example%) do %%v Card.Series: %Card.Series%
DealCard.bat is invoked without input and returns a description of a card in four environment variables: the face text in Card.Text, the numeric value in Card.Value the suit in Card.Suit and the color in Card.Color. The .Color variable is redundant with the .Suit, but can be used separately in a subsequent routine to display cards without having to interpret the suit. DealCard is derived from the MoreRand routine,with two major differences. First, the .Series has 13 items and, second, additional code is provided to select a suit. It should be noted that several sacrifices to speed result in a degradation in randomness. Specifically, a single reference to the system clock is used to select both the value and the suit. Though this may limit the variability of the suit selection, it seems like a good trade-off. I am assuming that the suit is not as important as the value of the card. Remember, this isn't 'the real world'.

However, the appearance of randomness is preserved. My informal testing gave what seemed to be a fairly uniform distribution of values and an acceptable variation in card suits.

I offer the following demonstration procedure to use to make your own tests. As is, it deals five cards, but it's a simple matter to add items to the list in the second line so that more cards are dealt.

:: TESTDEAL.BAT - Deals five cards and displays them as a test of the
:: DealCard.bat procedure. Call it WITHOUT command line inputs.
:: @echo off %1 %0 :: 1 2 3 4 5 :Loop call dealcard echo %2 Card: %Card.Text% of %Card.Suit% if not '%3==' for %%v in (shift goto:Loop) do %%v

 Show Me What Ya Got

Finally, if you're still with me, the next step is to use the word descriptions stored as Card.Text, Card.Suit, etc. to actually draw some cards on the screen. Follow the link to see a way to display cards in character based DOS.



Top | General Notes | Homepage