sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

I was hoping someone could either verify my template below will work or help me make it better. The object of the program is to shuffle a deck of cards and take the top card, then prompt to the user if the next card will be higher or lower than this card, after they answer the next card in the deck is shown, if the user was right the program will loop and flip another first card over..ask them again... but if they get it wrong the program exits.
do { shuffle cards print pulled card While { ask user for 'higher' or 'lower' print next pulled card } Unless ($guess was right){ quit;


"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

Replies are listed 'Best First'.
(jeffa) Re: Pseduo code planning
by jeffa (Bishop) on Jun 07, 2003 at 07:36 UTC
    ++ 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)
    
      <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 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!

Re: Pseduo code planning
by BrowserUk (Patriarch) on Jun 07, 2003 at 07:28 UTC

    A few questions to ponder.

    What happens when the pack runs out?

    Do you want to re-shuffle the pack each time the user make a guess or shuffle once at the start and then just take the next card each time?

    If the next card has the same value as the current card, does the user always win or always lose?

    Your textual description is shuffle a deck of cards and take the top card, then prompt to the user if the next card will be higher or lower than this card, after they answer the next card in the deck is shown, if the user was right the program will loop and flip another first card over..ask them again... but if they get it wrong the program exits.

    Breaking this up into steps you get

    shuffle a deck of cards and take the top card, then prompt to the user if the next card will be higher or lower than +this card, after they answer the next card in the deck is shown, if the user was right the program will loop and flip another first car +d over..ask them again... but if they get it wrong the program exits.

    Prodding this around a bit I get

    Shuffle deck; pop top card while( cards_left ) { show card prompt Higher or Lower? Get answer next_card = pop cards last unless next_card > top_card and answer = Higher or next_card < top_card and answer = Lower top_card == next_card } Game over.

    which is way short of complete, but might be a place to continue from?

    Update:Since jeffa let the cat out of the bag below, here's one of my takes on the implementation.

    Caveat: The suit symbols will probably be wrong on *nix.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: Pseudo code planning
by djantzen (Priest) on Jun 07, 2003 at 06:37 UTC

    Do you really want to exit the program if they get the card wrong? Sounds like you'll have a lot of very short games to me. A do/while loop is close to a natural fit for this, since you want to execute the shuffle once first before testing, although I think it'll make more sense to move the user input and testing part from the while condition and into the do block.

    do { shuffle and display card; read input; } while (input correct); exit(0);

    Of course, since the program as described could be written in so few lines utilizing a shuffling module (e.g., Algorithm::Numerical::Shuffle since it came up earlier today), you might as well use a label and a goto :^)


    "The dead do not recognize context" -- Kai, Lexx

      This is a very popular game in japan, and yes if the answer is wrong they lose, if right they are asked if the want to continue, usually this means a bigger payoff.

      Might consider adding:

      if ($guess was right) { continue(); } else { exit; } sub continue() { ask user to enter y or n read in answer chomp answer lowercase input #or uppercase I just like lower default input to n unless it is y or n #or y if you were a real hu +ssler return answer }

      Yes my pseudo code has code in it, just the way I code :). Didn't read merlyn post before posting, so this was not meant as an opinion. My opinion on that is merely that this post asked for pseudo code so I gave pseudo code. Thought this was both because I really don't know who to do this, and I'm just weird.

      Update: changed last statment

      "Pain is weakness leaving the body, I find myself in pain everyday" -me