in reply to Pseduo code planning
First, the main section revisited. I am going to call a
collection of cards a deck.
create deck
shuffle deck
this_card = pull from deck
last_card = this_card
answer = prompt('higher or lower')
for (rest of cards):
this_card = pull from deck
exit('was higher') if this_card > last_card and answer is 'lower'
exit('was lower') if this_card < last_card and answer is 'higher'
answer = prompt('higher or lower')
last_card = this_card
end for
By using two variables, this_card and last_card, we can compare the new card (the one the
user is going to predict) to the previous card. You
can't just use one variable, you need two so you can
com "pair". Generally, you assign the current item to the variable that holds the last item at the end of the loop.
So, what is deck? Probably just a plain old Perl array.
But, what is a card? It could be a simple scalar or it
could be a more complicated object. One advantage of going
the OO route is using overload to handle the
comparison of cards:
... if this_card > last_card ...
At any rate, no matter how you decide to represent a card,
a deck is going to be an array of cards, and we need two
methods to operate on this deck: shuffle() and
pull(). Well, i'm lazy. I am not going to write a
shuffle routine, when our pals Fisher and
Yates have already written one that blows away anything i
could write. Also, i know of two CPAN modules that already
have the Fisher-Yates shuffle implemented:
List::Util and
Algorithm::Numerical::Shuffle. That takes care
of shuffle()
And what about prompt()? It will probably look
something like:
sub prompt:
ask user to enter H or L
read in input
chomp off newline
uppercase input
default input to H unless it is H or L
return input
end sub
my @card_precedence = ('A',2..10,'J','Q','K');
# we will also need suites
my @suite_precedence = qw(clubs diamonds hearts spades);
What we really need is one array that stores some simple
numerical value for ease of comparison. And then we will
probably need a hash to map those values to labels ... but, the hour is growing late, and surely there is a CPAN module that could handle all of this ...
Games::Cards perhaps?
(update) WARNING: SPOILERS! (click and cheat)
(note that the update is for the warning, not the code within - i want to give a better warning, but i didn't want to invalidate castaway's reply. castaway++ please read this before you click vote, eh?)
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (jeffa) Re: Pseduo code planning
by castaway (Parson) on Jun 07, 2003 at 11:03 UTC | |
by vek (Prior) on Jun 07, 2003 at 17:15 UTC | |
by jeffa (Bishop) on Jun 07, 2003 at 18:13 UTC | |
|
Re: (jeffa) Re: Pseduo code planning
by sulfericacid (Deacon) on Jun 07, 2003 at 12:48 UTC | |
by Anonymous Monk on Jun 07, 2003 at 13:08 UTC | |
by sulfericacid (Deacon) on Jun 07, 2003 at 13:17 UTC |