The discard pile is simply another allocation in memory similar to the deck and the hands.
The technical process for removing a card from a hand and placing it in the discard pile is as follows:
1) Put the card value in the next available memory slot in the discard pile allocation.
2) Remove the card value from the hand's memory allocation slot
Step 2 in this process took a bit of thinking - since we read a hand by traversing the memory slots until we reach a value of 0 we couldn't just replace the discarded card value with a 0. Suppose you were discarding the second card in a hand of 6 cards... the next time we iterate through that hand only the first card would be counted. The way we overcame this was copying the card value from the last slot in the hand's allocation to the slot of the discarded card value and then replacing the last slot's card value with 0.
For example:
Hand: 2 | 3 | 4 | 5 | 6
Discarding the 3 would result in the following hand:
Hand: 2 | 6 | 4 | 5
Here is the code. The second half handles moving the last card value into the discarded card's slot. There is also a check to see if the discarded card is the last card in the hand in which case we just replace it with 0.
: playcard
discardpile ( add card to discard pile )
gethandcardcount
swap
+
c!
dup
rot
gethandcardcount 1 -
rot
= if
+
0
swap
c!
else
dup
gethandcardcount 1 -
swap
+
c@ ( last card in hand )
rot rot
dup
gethandcardcount 1 -
swap
+
0
swap
c!
+
c!
then
;

