-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a blackjack command to gamble #5821
base: master
Are you sure you want to change the base?
Conversation
Adds the blackjack game to the gamble command. Allows players to win up to 2x their stake by beating the dealer/bot.
you madlad, i love it |
Big tip: Try to code this in a fully abstracted way, so that its a lot easier to test (which is important so we know exactly for a fact the odds/rates of this, and just generally to test it for bugs), e.g. such as how |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is still a work in progress, and I haven't gone thru all of it, but here are some good notes :)
|
||
// Function to shuffle the deck | ||
function shuffleDeck(deck: Card[]): void { | ||
for (let i = deck.length - 1; i > 0; i--) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be i >=0 otherwise the loop where i = 0 is not executed, and you're just hoping the shuffle chooses card zero.
Basically if you don't do >=0, then the 0th card ends up unshuffled (2 of Hearts) a disproportionate amount of the time.
// Function to shuffle the deck | ||
function shuffleDeck(deck: Card[]): void { | ||
for (let i = deck.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use a nodeCrypto random, try cryptoRand() from util.ts, and you don't need to floor it.
Alternatively, randFloat() from util.ts also uses nodeCrypto, but why use two functions when you can use one? :D
} | ||
|
||
// Function to shuffle the deck | ||
function shuffleDeck(deck: Card[]): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The shuffle looks pretty good, but I would still shuffle the deck at least 2-3 times. (either multiple calls to shuffle deck, or more likely just loop inside the function 2-3 times)
const dealerHand = [dealCard(deck), dealCard(deck)]; | ||
|
||
// Check for player blackjack | ||
if (calculateHandValue(playerHand) === 21) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blackjack should pay 2.5x (1.5x their bet in profit).
Or a push if dealer also has blackjack.
You could get crazy with insurance, even-money on blackjack vs A, etc, but that's probably not necessary :D
|
||
// Define constants for suits and values | ||
const suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']; | ||
const values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It MIGHT make formatting the game easier if you use a single char for card values (and suits?).
eg. 10 = 'T', J, K , Q, A
It would be REALLY cool to dynamically generate the result like slots. You don't have to draw the entire card, just a simple box around painted unicode letters, 3♠, A♥ etc. Paint spades/clubs black, and hearts/diamonds red. T♣
Look at slots for simple graphics stuff, and the bank code has examples of drawing text (including unicode symbols) onto canvas)
Appreciate all the feedback, especially like the single char idea for card values. Paying 2.5x for blakcjack, i didnt know if magna would be happy with the win % and that's also why i made it so you currently have to beat the dealer, and there is no pushes. |
Adds the blackjack game to the gamble command.
Allows players to win up to 2x their stake by beating the dealer/bot.