Sinclair QL Beginner's Manual page 115

Hide thumbs Also See for QL:
Table of Contents

Advertisement

CHAPTER 16 – SOME TECHNIQUES
In this final chapter we present some applications of concepts and facilities already discussed and we
show how some further ideas may be applied.
SIMULATION OF CARD PLAYING
It is easy to store and manipulate "playing cards" by representing them with the numbers 1 to 52. This
is how you might convert such a number to the equivalent card. Suppose, for example, that the
number 29 appears. You may decide that:
cards 1-13 are hearts
cards 14-26 are clubs
cards 27-39 are diamonds
cards 40 52 are spades
and you will know that 29 means that you have a "diamond". You can program the QL to do this with:
LET suit = (card-1) DIV 13
This will produce a value in the range 0 to 3 which you can use to cause the appropriate suit to be
printed. The value can be reduced to the range 1 to 13 by writing:
LET value = card MOD 13
IF value = 0 THEN LET value = 13
Program
The numbers 1 to 13 can be made to print Ace, 2, 3... Jack, Queen, King, or if you prefer it, such
phrases as "two of hearts" can be printed. The following program will print the name of the card
corresponding to your input number.
100 REMark Cards
110 DIM suitname$(4,8),cardval$(13,5)
120 LET f$ = " of"
130 set_up
140 REPeat cards
150
INPUT "Enter a card number 1-52:" ! card
160
IF card <1 OR card> 52 THEN EXIT cards
170
LET suit = (card-1) DIV 13
180
LET value = card MOD 13
190
IF value = 0 THEN LET value = 13
200
PRINT cardval$(value) ! f$ ! suitname$(suit)
210 END REPeat cards
220 DEFine PROCedure set_up
230
FOR s = 1 TO 4 : READ suitname$(s)
240
FOR v = 1 TO 13 : READ cardval$(v)
250 END DEFine
260 DATA "hearts","clubs","diamonds","spades"
270 DATA "Ace","Two","Three","Four","Five","Six","Seven"
280 DATA "Eight","Nine","Ten","Jack","Queen","King"
Input and Output
13
King of hearts
49
Ten of spades
27

Advertisement

Table of Contents
loading

Table of Contents