T O P

Hey everyone! May I please have some constructive criticism regarding a C written blackjack game?

Hey everyone! May I please have some constructive criticism regarding a C written blackjack game?

UnhandledNaughtyBoy

> cardSum = cardSum + rand() % (10 + 1 - 1) + 1; I don't think that works. You need to make a deck of cards(array), shuffle it, and draw from it. A chance of card being equal to 10 should be bigger than a chance than anything else(because J, K, and Q give you 10). You shouldn't be able to draw five two's. And so on and so forth. Plus: 1. You should .gitignore binaries. 2. You should separate scenarios(won, lost, draws) into separate functions. You have duplicate code. 3. Try to not use variables if you don't need them. You don't need isTwentyOne variable, you can check21 within your if statement. 4. Try to declare variables RIGHT BEFORE using them. Small scope good, big scope bad


MountainNoob

You're absolutely right! An array is much better approach, and my scope I realized was way to big haha. Thank you :D


UnhandledNaughtyBoy

https://www.techiedelight.com/shuffle-given-array-elements-fisher-yates-shuffle/ Consider this


marko312

> if (isTwentyOne == true) Any conditional statement (`if`, `while`, the second part of a `for`) is an implicit "is true" (well, "is truthy") check, so the `== true` is extraneous This corresponds to reading the program in words: instead of "if `isTwentyOne` is true", it's `if `isTwentyOne`". *** The `CheckBust` and `Check21` functions are quite unnecessary in my opinion: a conditional is required after calling these functions anyway, so the extra text could be put directly in the `if`. Something like generating a random card (note u/UnhandledNaughtyBoy's comment if you want a more realistic distribution of values) would be a good candidate to be in a separate function, though.


MountainNoob

Youre right, I completely forgot that saying if it's == true is kinda redundant. I'll take a look at his approach, thank you :P


azzal07

In the last if chain you always have the two lines: printf("Player's hand: %d\n", cardSum); printf("Dealer's hand: %d\n", dealerSum); You could just move these to the end of main. You also have fully overlapping conditions: if (cardSum > dealerSum) /* snip ... */ else if (cardSum > dealerSum)


OnYaBikeMike

"An Ace will have a value of 11 unless that would give a player or the dealer a score in excess of 21; in which case, it has a value of 1." If implemented this rule needs you to keep track of the card values, not just the totals. Also I am not a fan of the structure of the Check21() and CheckBust() functions. I would refactor to not have the 'else' clause: bool CheckBust(int cardSum, int dealerSum) { if (cardSum <= 21) { return false; } printf("Your card total is %d: ... bust (returning to terminal).\n", cardSum); printf("Dealer Total: %d\n", dealerSum); return true; } However this is a matters of style...