C
C#14mo ago
Nomnomfighter

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