in reply to Pseduo code planning

++ to you for designing before you code. It's hard work, but the hard work pays off. Unfortunately, not in the short run. Your pseudo code has a few problems, and while you have the main section mostly finished, you mention nothing of what it means to shuffle, pull cards, or what a card or group of cards look like.

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()

What about pull()? Well, all that does is remove the top card. Perl has two built-in function to remove a single element (ok, so it's really three) - shift and pop. As long as you pick one and stick with it, it doesn't really matter - cards can be represented by a queue just as easily as a stack, it depends upon what game you are playing. The important thing is to remain consistent, so let's pick pop since it's shorter.

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

Back to the cards object. How do we program the hierarchy? It's obvious that a 9 is greater than a 2, but is a Q higher or lower than a K? If use array indices as precedence, we get a nice solution:
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
    <rant>

    Sorry, but I feel I have to downvote this.. If it was me that had asked the question, I'd be pretty peeved at someone trying to solve it for me.. (Especially if my point was to try and learn something for myself.) Suggestions and pointing out errors I can understand.. But actually writing the code.. *shakes her head* ..

    .oO( And then there are the posts that actually *want* code, and nobody provides any, because they should 'do it themselves'.. I don't get it..)

    </rant>

    C.

      You seem to be complaining that jeffa wrote some code. Well, yes he did, but he also did a good job with the pseudo code (as the OP requested). If you really want to write the code yourself you still can. Yes jeffa provided some code but you don't have to use it do you? I mean you can still take a stab at writing it yourself using your pseudo code as a guide surely. Am I missing something?

      -- vek --
        I am with castaway on this one. I don't think she downvoted me because i gave away the solution, but instead she downvoted me for the manner in which i gave away the solution. All i had was a <readmore> tag ... i gave no clue that my solution was behind the curtain. bad jeffa ;)

        And thanks for the kind words.

        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)
        
Re: (jeffa) Re: Pseduo code planning
by sulfericacid (Deacon) on Jun 07, 2003 at 12:48 UTC
    I will have to agree with castaway on this, it's not that I don't appreciate all the help and work you must have put into it for such a reply but I don't exactly like the idea the entire problem is solved and layed out before me before I get my first verison done. I'm not going to take your code but there are parts in there that helped some of my mind boggling questions (like how would I use card hierarchy (this had me really stumped!) ).

    From this I think I have a good idea of what I have to do to get going, thanks so much for your help!

    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

      jeffa has every right of answering your question to the best of his abilities.

      You are complaining that he told you too much?

      merlyn was right!

        I know al he was trying to do was help and that's what he did but I wasn't asking for help on the program itself, all I was asking was for someone to draw the basic outline so I could begin from there.

        It is possible to give someone to much help, the point of most perl scripts (atleast that I've done) was to help me learn through trials and errors and to try new things, when someone delivers you a premade script before you start writing the program it kind of defeats the purpose.

        I'm not saying I don't appreciate his help but I just think you shouldn't give off too much information unless they really ask for it.

        "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

        sulfericacid