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
94
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)
I
f$
I
suitname$(suit)
210
END
REPeat
cards
220
DEF
i
ne
PROCedu
re
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
II
Ace"
,
"lwol!
I "Th
ree"
,II
Fou r"
,"
F;
veil,
"S i
x"
I "S
even"
280
DATA
"Eight","Nine","Ten","Jack","Queen","King"
Input and Output
13
King
of
hearts
49
Ten
of
spades
27
Ace
of
di
amonds
o
COMMENT Notice the use
of
DATA
statements
to
hold a permanent
file
of data which the program
always
uses.
The
other data which changes each time the program runs
is
entered
through an INPUT statement
If
the Input data
was
known before running the program
it
would be equally correct
to
use another READ and more
DATA
statements
This
would
give better control.
•
12/84