Friday, April 22, 2011

Displaying a Card

In previous posts we've made mention of cards as numbers between 0-51 (soon to be 1-52). In order to translate this card number to the user in the form of a playing card we wrote a word, displaycard, which take a card number from the stack and prints that card's Face/Suit to the prompt.

This is slightly different than our Pseudo code in that it uses the mod word to determine the card's rank within the suit.

: displaycard
dup ( c -- c1 c2 ) ( duplicate the card number )
13
mod ( c 13 -- c1 ) ( use mod division to get the card face # )

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

dup 13 <= if ." of Diamonds " else
dup 26 <= if ." of Hearts " else
dup 39 <= if ." of Clubs " else
dup 52 <= if ." of Spades " else
." Error Displaying Card "
then then then then drop
;


Example of usage:
Assuming the number 34....

displaycard

8 of Clubs ok

In Action...

No comments:

Post a Comment