Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Simple Blackjack progam help

by ria1328 (Initiate)
on Oct 13, 2012 at 17:10 UTC ( #998860=perlquestion: print w/replies, xml ) Need Help??
ria1328 has asked for the wisdom of the Perl Monks concerning the following question:

I'm having some trouble with this program. We're supposed to create a program with subroutines for Blackjack. The requirements are: Deck (1-10) Random Numbers; Plaver vs. Dealer; Bank (Sufficient Funds); Draw on 16; Stick on 17; 21 Wins; Dealer wins all ties; Calculate Funds available; Play until you want to quit or "Go bust"; Contains one or more subroutines I've never played Blackjack before so I'm more than a little confused. Here's my code:

#!/usr/bin/perl srand; print "How much money are you playing?"; $total = <STDIN>; print "What is your bet? "; $bet = <STDIN>; print "Here's your first card: "; $draw1 = int &draw; print $draw1; print "\nDealer has "; $dDraw = int &draw; print "\n $dDraw"; print "Would you like another card? y/n"; $another = <STDIN>; if ($another == 'y') { do{ $count = 2; print "You're $count card is: \n"; $draw2 = int &draw; print $draw2; print "Dealer has "; $dDraw = int &draw; print "$dDraw "; $count ++; $total = $draw1 + $draw2; print "\nWould you like another card? y/n"; $another = <STDIN>; }while ($total>0 && $another == 'y'); } else {if ($another == 'n') { print "$draw1 $draw2 ";}} sub draw { $draw = int rand(10); }

First, I'm getting stuck in an infinite loop in the if/dowhile part. I also can't figure out a way to count the cards up for both dealer and player and to calculate funds available. Any help?

Replies are listed 'Best First'.
Re: Simple Blackjack progam help
by moritz (Cardinal) on Oct 13, 2012 at 17:48 UTC

    First of all, please start your code with

    use strict; use warning;

    and declare your variables with my when you use them first.

    Some others errors I noticed at first glance:

    When you read in a line with $variable = <STDIN>, then the variables has a trailing newline character. Usually you don't need that, so remove it by calling chomp $variable.

    if ($another == 'y')

    == is for comparing numbers, but 'y' isn't a number. Instead use

    my $another = <STDIN>; chomp $another; if ($another eq 'y') { ... }


    $draw1 = int &draw;

    Your draw subroutine already calls int, there's no need to do it twice

    sub draw { $draw = int rand(10); }

    That returns an integer between 0 and 9, not between 1 and 10.

    Please search a bit for debugging tips & tricks here on perlmonks, there a quite a few that could have caught many of these errors, and probably many other errors that still lurk in your code.

Re: Simple Blackjack progam help
by toolic (Bishop) on Oct 13, 2012 at 17:49 UTC
Re: Simple Blackjack progam help
by davies (Parson) on Oct 13, 2012 at 21:02 UTC

    There are a few points of which to beware. First, I believe (this needs checking & I'm not going to do it today) that in Blackjack, an Ace can count as 1 or 11. Second, the odds of drawing a card of a given value are not the same, either at any time or at all times. That needs explanation in two parts. (A) Of the 52 cards in a pack, there are 16 with a value of 10. (B) After you have drawn one card, the odds change, so that if you have a deck of one pack, the chance of drawing a card with a value of 10 is 16/52. But once you have drawn one, the odds change to 15/51, and so on (if you draw a non-10, the odds change to 16/51 - still different). Finally, I believe that casinos use a deck of six packs, although this may have changed since the days of Strategic Investments (JFGI - you can legally beat the house at Blackjack).

    However many packs you include in your deck, you might well start with the standard dealing algorithm I demonstrated in Re: Random data generation. and Re: getting random number 8 times never the same, although you will need to amend it to deal cards one by one rather than use the $length variable I used in those.


    John Davies

Re: Simple Blackjack progam help
by CountZero (Bishop) on Oct 14, 2012 at 14:46 UTC
    Don't re-invent the wheel: there is a Games::Blackjack you can use as the basis of your program.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998860]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2018-04-22 11:11 GMT
Find Nodes?
    Voting Booth?