My blackjack game, needs some clarification.

Hello, there is a card example in the book that I have, that I want to morph into a black jack game with other features. So below is the code.

http://pastebin.com/aHaqPGc0

What I need clarification on is this snip it of code.

1
2
3
4
5
for (j=0; j<52; j++){
        int num = (j % 13) + 2;
        Suit su = Suit(j % 13);
        deck[j].set(num, su);
    }


how does int num once it gets to 14, starts back over to 2. So the number values are 2-14 and I am not sure how it does that when 52%13 is 52 then + 2 is 54.

Any help on showing me the light would be appreciated.

Thanks.
52%13 is 52

52 % 13 is zero because 13 divides 52 evenly (52 / 13 = 4, Remainder zero)
Thanks for the reply, I guess I am confused on how modulus work.

Would you mind explaining what it is doing in the above code?

From my understanding its going,

(I was told on how to find the remainder is you take divide the Dividend by the Divisor and then times it by the Divisor and you will get the remainder)

0 % 13 * 13 = 0 + 2 = 2
1 % 13 * 13 = 1 + 2 = 3
2 % 13 * 13 = 2 + 3 = 4

and so on, that is how I currently am understanding how its keeping count, so if you would be able to give an somewhat better clarification on modulus it would be greatly appreciated.

cheers!
(I was told on how to find the remainder is you take divide the Dividend by the Divisor and then times it by the Divisor and you will get the remainder)

If you would rewrite the modulus operation in C++ it would look something like this:

1
2
3
4
int mod(int a, int b)
{
   return a - (b * (a / b));
}


Where a / b is integer division. The modulus operator simply gets the remainder (you should be able to figure out the remainder in your head for the numbers involved in the code you posted). Note that the result of X % 13 is in the range [0..12] because you can't have a remainder of 13 or more when 13 is the divisor.

0 % 13 * 13 = 0 + 2 = 2
1 % 13 * 13 = 1 + 2 = 3
2 % 13 * 13 = 2 + 3 = 4

and so on, that is how I currently am understanding how its keeping count...

I don't know where you're getting the "* 13" from but the j % 13 terms will go from 0 to 12 and then wrap back to 0 again.
Last edited on
Topic archived. No new replies allowed.