✅ Blackjack Dealer goes into an infinite loop (SOLVED)
https://paste.ofcode.org/egKC5vE9biPBKhFyNhuksQ
Loop only happens if a) the Dealer stands, i.e. has a total between 17 and 20 inclusive and b) the player is at exactly 21/Blackjack.
The loop then only gives out debug output on Line 44 ("the dealer stands on {cardDealer}").
There are two variables, called stand (for the dealer) and playerStand (for the player) that should end the while loop of card drawing opportunities and go into the result calculation beginning with the if @ Line 77
57 Replies
send the log please
Where do I find that on Visual Studio 2022?
what log lol
you'll want to $debug
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
of execution
see exactly why your while condition remains true
it would help me to understand where exactly it may cause the inf loop
the code works fine for me, what am i missing?
i've tested like 4 different outcomes and they all worked fine
This is what debug gives me https://paste.ofcode.org/MaZvvJQrAEhSghJhQLckG (hope that's the correct debug)
So, I think it's because you have else on the 70 line
I advice you to not use while
and come up with different logic processing approach
what a thing to say
you have to have a loop to make the game continue
I'm worst at it, but, I'm trying to help tho :)
sure, that's fine
you're just wrong is all
Well, I don't think you really should do it that way, you can make it just iterative if we speak about blackjack
It's a turn based game
you can do multiple actions per turn
every turn needs to be in a loop iteration though
so, best approach is using iterative, with determined count of players and actions
Why
you can't know how many actions there are
well, isn't it determined? Like, you can rise and you can do safe deposit
You don't have multiple actions per turn. You have one. Which is deciding whether you draw or not. The Dealer gets no input, they just follow their logic, i.e. create the information for the turn
it's not determined, no
Ah so in that case it's only 2 actions
Two results*
one action
I am not looking to add full casino logic with bet trees every turn until I fix the loop problem
it is
it is not
what are you talking about
you played RDR2 blackjack?
You can do actions, but, per action, for sure you can wait
While loop is not the best case for this kind of wait
How can you know how many turns there will be?
cause actions and results except dealers cards is determined
Sure you could calculate the limit and stick it in a for loop
Well, analyzing the game
you can't know how many turns there will be before the game started...
unless you hard-coded the order of the cards drawn
In example, RDR2 gives you a turn and using states of the game, like cards of a dealer and other things
and even then you can't!
cause i can still stand on the first round, or i could continue until i bust
Yeah, but, still it's determined by each turn
At least I'm saying playing and analyzing how blackjack in RDR2 done
Sorry, but if you want to discuss RDR2's blackjack, please do it in another channel. I'm trying to get my code to work
So, as I was saying
line 70
maybe you placed else in the wrong place
I actually made an error transfering the code to PoC, here's the corrected version (forgot to translate variables) https://paste.ofcode.org/egKC5vE9biPBKhFyNhuksQ
That else is in the Player structure, which is not part of the loop problem. The looping debug message comes from the exact same structure on the Dealer side, i.e. @ Line 41
the Line 70 else only gets called if the Player chooses no on the Console.ReadLine() @ Line 50, which I'm currently bypassing entirely by hardcoding the value of the Player's hand at 21 (for debugging) before the while even starts
look at 24 if statement
maybe in there you actually stuck with stand == false
shouldn't be possible. Even if it starts with
stand == false
, it should check both cardDealer < 17
AND stand == false
. If I hardcore cardDealer = 18
for example, that should kick it out of the L24 if statementyou have to remember to also set
playerStand = true
for testing
because that's what happens in the real logic as well
once you get a blackjack, you "stand"Now there's something. I may be missing a playerStand change, apparently
Changing the Player Draw routine starting if L47 from
< Blackjack
to <= Blackjack
seems to solve it...
Why does checking that if statement with an exact Blackjack on the player side cause the loop though?it doesn't?
not in a real game anyway
Because your logic will never hit the other if statements I assume either
legit just can't replicate your issue
But yeah it's probably easier to debug and go line by line
That's so weird???
to fix it on my end, I need to put <= Blackjack in the player structure to stop the loop, which means it doesn't automatically stand if a blackjack is already achieved...?
Well, I guess it's a case of "the computer just decided this"
how are you testing it exactly?
Currently, I have hardcoded
cardDealer = 18
to but them in stand routine and cardPlayer = 21
to put Player at Blackjack.
If I keep L47, if (cardPlayer < Blackjack && playerStand == false)
, it goes into the loop.
If I change it to to (cardPlayer <= Blackjack && playerStand == false)
, it gives me the prompt, asking if I want to draw again and the options go on normally, with n winning and y busting me because I draw another card..
Yeah, that works too, but it should work with either stand = true or 21. When I play normally without hardcoding the values, the other ways to put playerStand = true work. The bug only started happening when player happened to get exactly 21. Which should put playerStand = true just like everywhere else, but just also making the starting if @ L47 false for both statements
since the player shouldn't even have 21 points at the beginning of the game, making it work with such a condition is superfluous
it shouldn't work in such a case, because it's a bogus case
You're right. I kept that because it was similar to the Dealer's routine (which doesn't make sense for the player). Took it out, now it works without issue when you "naturally" achieve 21.
I still don't understand why having both conditions being false put it into a loop, but now I've reduced L47 to just
if (playerStand == false)
and it works as expected \o/
Thanks matecouple things here
myBool == false
can just be !myBool
where !
is the negating operator on a bool
. meaning NOT myBool
(as in, NOT true
)
i assume you've been given guidelines by your instructor, but it looks like you're on an old .NET version. update to .NET 7 if you can
and presumably you haven't learned it yet, but a lot of this logic is better offloaded to classes (Player
, Card
, Deck
) and methods
$closeUse the
/close
command to mark a forum thread as answeredThat's good to know!
I'm using .NET 6 to update less frequently, didn't think there'd be an issue like this since it's still in support
Yeah, I'll tackle classes in a while. Just wanted to keep honing the knowledge I've gained sofar
sure, you're just not using top-level statements or file-scoped namespaces
Apparently I don't have the permissions to close this thread ¯\_(ツ)_/¯
Don't even know what that means rofl