Mainly, we broke out the suit determination logic into its own word for code reuse.
Here is the new version of our Card logic:
: getcardsuit
dup 14 < if 1 swap drop else ( 1=Diamonds )
dup 27 < if 2 swap drop else ( 2=Hearts )
dup 40 < if 3 swap drop else ( 3=Clubs )
dup 53 < if 4 swap drop else ( 4=Spades )
." Error - can't determine suit "
drop
then then then then
;
: getcardface
dup 1 = if ." Ace " else
dup 11 = if ." Jack " else
dup 12 = if ." Queen " else
dup 0 = if ." King " else
dup .
then then then then drop
;
: displaycard
dup ( c -- c1 c2 ) ( duplicate the card number )
13
mod ( c 13 -- c1 ) ( use mod division to get the card face # )
getcardface
getcardsuit
dup 1 = if ." of Diamonds " else
dup 2 = if ." of Hearts " else
dup 3 = if ." of Clubs " else
dup 4 = if ." of Spades " else
." Error Displaying Card "
then then then then drop
;
No comments:
Post a Comment