CGI/Perl Guide | Learning Center | Forums | Advertise | Login Site Search: in Perl Guide PerlGuru Forums Learning Ctr

Home: Perl Programming Help: Intermediate:
Blackjack total algorithm

Zhris
Enthusiast

Oct 2, 2012, 11:44 AM

Post #1 of 3 (5015 views)
 Blackjack total algorithm Can't Post
Hey,

I'm trying to write a command line blackjack spinoff in Perl, but am stuck on writing the algorithm that will generate/process all the possible totals from a set of cards. The main issue is that an Ace can have a value of 1 or 11 (or configurably, whatever I desire).

Below, I have a hand, which is an array of arrays, representing each card and their possible values:
 Code
`my \$hand =  [ 	[1,11],[2],[1,11],[10] # Represents the values of Ace, Two, Ace, King. ];`

I would like to generate another array of arrays, representing every possible combination of values:
 Code
`my \$combinations = [ 	[1,2,1,10], 	[1,2,11,10], 	[11,2,1,10], 	[11,2,11,10] ];`

I can then use this data to return an appropriate total based on a number of blackjack rules. In this case "14".

However, I can't get my head round the type of processing \$hand requires in order to generate \$combinations. My thought is it will have to be some kind of recursive process, reiterating per value of cards with multiple values.

I would really appreciate some advise on how to approach this task. Where do I begin!

Thanks alot,

Chris

(This post was edited by Zhris on Oct 2, 2012, 11:46 AM)

Laurent_R
Veteran / Moderator

Oct 2, 2012, 2:13 PM

Post #2 of 3 (5007 views)
 Re: [Zhris] Blackjack total algorithm [In reply to] Can't Post
Although a recursive procedure is certainly possible, I think I would chose here two nested loops: one on the main array (the cards) and the second more inner loop on the different values that each card can get.

(I know nothing about blackjack, so I rely exclusively on your description of the rules.)

Zhris
Enthusiast

Oct 2, 2012, 3:56 PM

Post #3 of 3 (5000 views)
 Re: [Laurent_R] Blackjack total algorithm [In reply to] Can't Post
Hi Laurent,

Thank you very much for your response.

Two nested loops might become the basis of the solution, but the complex bit is the code in between. Regardless of the game, I am only concerned that the example output can be generated from the example input (so long as the pattern formation can be understood).

I have had a couple of attempts at doing this now, but am left with a convoluted piece of code that is missing the critical component(s) necessary to appropriately construct the combinations array (honestly in no state to be worth sharing). Usually a fresh day brings fresh ideas, but not this time.

Thanks again,

Chris

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0