# BlackJack algorithm - Homework (kinda)

• 02-01-2013
HeirofRome
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
• 02-01-2013
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! :cool:
• 02-01-2013
HeirofRome

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.
• 02-01-2013
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.
• 02-02-2013
iMalc
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.
• 02-02-2013
Tclausex
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.
• 02-03-2013
HeirofRome
Thanks for the reply guys! I really appreciate it

Oh btw, branchless aint allowed , i think
• 02-03-2013
HeirofRome
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