✅ 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
MIS42NE
MIS42NE2y ago
send the log please
Nomnomfighter
NomnomfighterOP2y ago
Where do I find that on Visual Studio 2022?
ero
ero2y ago
what log lol you'll want to $debug
MODiX
MODiX2y ago
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.
MIS42NE
MIS42NE2y ago
of execution
ero
ero2y ago
see exactly why your while condition remains true
MIS42NE
MIS42NE2y ago
it would help me to understand where exactly it may cause the inf loop
ero
ero2y ago
the code works fine for me, what am i missing? i've tested like 4 different outcomes and they all worked fine
Nomnomfighter
NomnomfighterOP2y ago
This is what debug gives me https://paste.ofcode.org/MaZvvJQrAEhSghJhQLckG (hope that's the correct debug)
MIS42NE
MIS42NE2y ago
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
ero
ero2y ago
what a thing to say you have to have a loop to make the game continue
MIS42NE
MIS42NE2y ago
I'm worst at it, but, I'm trying to help tho :)
ero
ero2y ago
sure, that's fine you're just wrong is all
MIS42NE
MIS42NE2y ago
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
ero
ero2y ago
every turn needs to be in a loop iteration though
MIS42NE
MIS42NE2y ago
so, best approach is using iterative, with determined count of players and actions Why
ero
ero2y ago
you can't know how many actions there are
MIS42NE
MIS42NE2y ago
well, isn't it determined? Like, you can rise and you can do safe deposit
Nomnomfighter
NomnomfighterOP2y ago
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
ero
ero2y ago
it's not determined, no
MIS42NE
MIS42NE2y ago
Ah so in that case it's only 2 actions Two results* one action
Nomnomfighter
NomnomfighterOP2y ago
I am not looking to add full casino logic with bet trees every turn until I fix the loop problem
MIS42NE
MIS42NE2y ago
it is
ero
ero2y ago
it is not what are you talking about
MIS42NE
MIS42NE2y ago
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
Haze.
Haze.2y ago
How can you know how many turns there will be?
MIS42NE
MIS42NE2y ago
cause actions and results except dealers cards is determined
Haze.
Haze.2y ago
Sure you could calculate the limit and stick it in a for loop
MIS42NE
MIS42NE2y ago
Well, analyzing the game
ero
ero2y ago
you can't know how many turns there will be before the game started... unless you hard-coded the order of the cards drawn
MIS42NE
MIS42NE2y ago
In example, RDR2 gives you a turn and using states of the game, like cards of a dealer and other things
ero
ero2y ago
and even then you can't! cause i can still stand on the first round, or i could continue until i bust
MIS42NE
MIS42NE2y ago
Yeah, but, still it's determined by each turn At least I'm saying playing and analyzing how blackjack in RDR2 done
Nomnomfighter
NomnomfighterOP2y ago
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
MIS42NE
MIS42NE2y ago
So, as I was saying line 70 maybe you placed else in the wrong place
Nomnomfighter
NomnomfighterOP2y ago
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
MIS42NE
MIS42NE2y ago
look at 24 if statement maybe in there you actually stuck with stand == false
Nomnomfighter
NomnomfighterOP2y ago
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 statement
ero
ero2y ago
you 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"
Nomnomfighter
NomnomfighterOP2y ago
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?
ero
ero2y ago
it doesn't? not in a real game anyway
friedice
friedice2y ago
Because your logic will never hit the other if statements I assume either
ero
ero2y ago
ero
ero2y ago
legit just can't replicate your issue
friedice
friedice2y ago
But yeah it's probably easier to debug and go line by line
Nomnomfighter
NomnomfighterOP2y ago
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"
ero
ero2y ago
how are you testing it exactly?
Nomnomfighter
NomnomfighterOP2y ago
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.
ero
ero2y ago
.
Nomnomfighter
NomnomfighterOP2y ago
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
ero
ero2y ago
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
Nomnomfighter
NomnomfighterOP2y ago
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 mate
ero
ero2y ago
couple 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 $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Nomnomfighter
NomnomfighterOP2y ago
That'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
ero
ero2y ago
sure, you're just not using top-level statements or file-scoped namespaces
Nomnomfighter
NomnomfighterOP2y ago
Apparently I don't have the permissions to close this thread ¯\_(ツ)_/¯ Don't even know what that means rofl
Want results from more Discord servers?
Add your server