Like Tree1Likes
  • 1 Post By iMalc

BlackJack algorithm - Homework (kinda)

This is a discussion on BlackJack algorithm - Homework (kinda) within the C Programming forums, part of the General Programming Boards category; Hey everyone, So i'll come out and say it. This is a homework assignment I and didnt understand at all. ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    BlackJack algorithm - Homework (kinda)

    Hey everyone,

    So i'll come out and say it. This is a homework assignment I and didnt understand at all. The deadline has passed, and i could not figure this out, but the problem still bugs me.

    THE PROBLEM:
    So we get to input three card numbers (blackjack)
    We get three variables , a, b ,c. Each holds a number from 2 - 10. Or a number say 1 , which can be treated as a 11 or a 1. Given those three integers, we have to print out the best hand i.e the addition of integers closest or equal to 21. If there is no way we can get 21 or less than 21 with our hand , we print a 0.

    Sounds simple enough right? Well, not so much. The catch our brilliant instructor put in , is to do this problem without selection (so no ifs, whiles, for, etc) , no recursion, no external functions.

    ATTEMPT:
    Quite simple i tried using math by first adding the three integers and trying various combinations like mod 21, divide by 21 etc. I am completely bowled over by this problem


    I would greatly appreciate any help in understanding this algorithm.

    Thanks
    HeirofRome

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is no known algorithm for weird problems like this. It's really an exercise in problem solving, for you. So, how to proceed?

    1) Instructors give you hints about how to do your work. You may need to "read between the lines" of the chalkboard, a bit - they might just be a subtle verbal reference of some kind. Look for those. Talk to your fellow students about them.

    2) Probably, nothing will give you the answer you want, right away, so start with programming a regular program of blackjack**. When you have the algorithm for that well in hand (whether the program is finished or not), then try to solve the part about the restrictions. Trying to solve both of these at the same time, is a hefty load to carry, sometimes.

    **The initial deal in Blackjack is 2 cards, not three.

    It's difficult to get help with these kinds of problems, because it's like asking someone to play soccer while holding their right foot, with their right hand, all the time - just seems a bit silly.

    Just my 2 cents.

    Welcome to the forum, however!

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Hey Adak,

    Oh yes! I did spend some time seething about what the heck the instructor wanted from us but im afraid this is the problem definition, no hins whatsoever. He's given us three integers with 2-10 , there MAY or may not be a 1 which can be treated as a 1 or 11. And yes, it is quite funny how he says three cards when blackjack is a two card game.
    So i do have a program written that can do this perfectly. The problem? It uses atleast one or two if statements. And i cant get around it mathematically.

    Anyways thanks for the response.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What about using a trigraph, instead of an if statement, is that allowed? What about switch statements?

    I hate these "let's have a sack race, but you can't use your legs", kind of problems.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Considering aces as 1 or 11, there are at worst eight possible sums, when all three are aces. Realising that when you have multiple aces that at most one of them can be an 11 or else you exceed 21, and with three aces a total of 3 is pointless, reduces that down to just three sums that are worth considering.
    These sums which for aces may be 1 or 11 can be calculated in a "branchless" fashion by using an implicit conversion from bool to int and a multiplication.

    After calculating all three sums, many of which will typically be the same unless you did have aces, and then use implicit conversion from bool to int combined with multiplication again, to mathematically "select" the largest one that is not above the threshold.

    All up this would take 7 lines of code. So yeah, totally do-able.
    stahta01 likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    The sum can be div'd with 22 and then bit-shifted by the quotient*N. If the sum is less than 22 it won't get shifted, otherwise you'll be left with zero.
    Similarly for dealing with aces, take the first card value and div by 11. Add that value to the card bit-shifted by the quotient*N and store it back in the card.
    Then do the sum and check again, storing results in an array of 4.
    Move to the second card, and so on.
    I used chars and just chose 6 as N.
    You can use two indices, one for results array, and one for cards array, then you can use the result of div'ing the card by 11 to advance results index or not to avoid repeating values.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Thanks for the reply guys! I really appreciate it

    Oh btw, branchless aint allowed , i think
    Last edited by HeirofRome; 02-03-2013 at 06:27 PM. Reason: Additional Info

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Hey iMalc,
    That actually makes a lot of sense. Thank You.
    I think branchless qualifies as a selection statement.

    However, may i ask how to proceed? i only have 3 integers and the user inputs any three number he/she likes. How do i know if an integer is a 1 or an 11 without selection.
    I get what you are saying, make three 'sum' variables considering all the sums and select the one that is largest. I can do that, however, i do not know how to do so without crossing the threshold. As in, how do i put the threshold condition in?
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone else think it's kinda weird...
    By Babkockdood in forum General Discussions
    Replies: 38
    Last Post: 07-29-2011, 07:00 PM
  2. kinda new need help
    By drkylec in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2011, 02:30 PM
  3. I'm kinda new to C++
    By DeanDemon in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2002, 11:52 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21