C
C#•2y ago
vl4do

BlackJack method

im a beginner and i just learned about methods. i was wondering is this too many methods? https://paste.mod.gg/jsggvgkilcew/0
BlazeBin - jsggvgkilcew
A tool for sharing your source code with the world!
42 Replies
phaseshift
phaseshift•2y ago
your Main is about80 lines long. You dont have enough methods
vl4do
vl4doOP•2y ago
i dont see where i can extract more methods, besides from 1 or 2 places max
Omnissiah
Omnissiah•2y ago
potentially there is a lot you can do like separating interface (console) from business logic (the card game)
vl4do
vl4doOP•2y ago
in a different class or method?
Omnissiah
Omnissiah•2y ago
classes
vl4do
vl4doOP•2y ago
ill try ty also any other tips are greatly appreciated
phaseshift
phaseshift•2y ago
these can easily be extracted.
phaseshift
phaseshift•2y ago
It can be taken much further as well. Basically big if blocks are obvious candidates
Omnissiah
Omnissiah•2y ago
also in general every state could be a class in itself
vl4do
vl4doOP•2y ago
i cant convert if statements into classes with ctrl + . idk if doing it manually would fix it also can i use methods in methods? @dont
Omnissiah
Omnissiah•2y ago
you can use methods in methods but i wouldn't advice it at this time the point of extracting methods and classes is isolating concepts (and responsibilities (and states)) i don't know if it's too abstract probably it is
vl4do
vl4doOP•2y ago
so id need to unextract my other methods and then extract a bigger method?
Omnissiah
Omnissiah•2y ago
maybe we could try with an example i hope it's alright let's pick the random generator for example it's fine, it works, BUT what if let's say you have a crash somewhere at a certain point and you don't know why you get what sequence of cards would break the program, so you have to 'inject' that fixed sequence how do you do it? you isolate the random generator in advance so that you can use something else or you can plug the random generator again to play normally or maybe you plug another client which is played by another human
vl4do
vl4doOP•2y ago
yeah that makes sense i extracted the red box but cant the blue /purple i cant "ctrl + ." it
Omnissiah
Omnissiah•2y ago
wait, let's make another example, the strings you have strings because 1) you are using a console 2) they're only english what if there was a language selection you have to put the strings somewhere so that you can pass an option to select the language so you would have a layer of abstraction between the strings and their meaning obvisouly if you are never going to do it probably you wouldn't care but just to give an example this is one of the most common things to work on
vl4do
vl4doOP•2y ago
do you expect an answer from me?
Omnissiah
Omnissiah•2y ago
well this is your program, like you can do what you want i probably would expect code...? another not so brilliant thing i wouldn't use is the while (true) that i would change
vl4do
vl4doOP•2y ago
i use that every time for when i want to make a program repeat itself like play as many times as you want although i couldnt figure out how to make it so the player CHOOSES whether to play or not i was thinking of making a bool and making something like while (wantsToPlayAgain = true) idk if thats the right syntax for a bool tho
Omnissiah
Omnissiah•2y ago
yeah but reading that i wouldn't know how it exits i would need to search for the break in the code for example also you could have forgot to write the break again i would have to search for it
vl4do
vl4doOP•2y ago
so what would i need to replace the while true with?
Omnissiah
Omnissiah•2y ago
you should make a clean exit from inside there this could mean refactoring the flow of the program
vl4do
vl4doOP•2y ago
it only exits when you run out of money otherwise you play again infinitely
phaseshift
phaseshift•2y ago
so obviosuly while(HasMoney()) or similar
Omnissiah
Omnissiah•2y ago
something that gives the reader the intent it's the same for the extraction of the methods in the colore squares
vl4do
vl4doOP•2y ago
but my main objective is to give the player the choice to continue or not, running out of money is just a side scenario
Omnissiah
Omnissiah•2y ago
take the red for example, the first i need to read the code to understand what it does do i need to? not really
phaseshift
phaseshift•2y ago
then add that in... while 'not (out of money or player quits)'
vl4do
vl4doOP•2y ago
BlazeBin - jsggvgkilcew
A tool for sharing your source code with the world!
Omnissiah
Omnissiah•2y ago
and what this would translate in? a state of the game and a method that checks whether this action is allowed by the state
vl4do
vl4doOP•2y ago
i just did it
Omnissiah
Omnissiah•2y ago
mmmm this seems to need to be considered too if (bet <= playerMoney) is there a minimal bet? because if it does then it doesn't make sense to get there if the player is low on money
vl4do
vl4doOP•2y ago
yeah there isnt but thats easy to implement i think
vl4do
vl4doOP•2y ago
vl4do
vl4doOP•2y ago
done lol i tweaked it so it gives a different error when you dont have enough money to bet and when your bet isnt valid at all like its -50 or 0
Omnissiah
Omnissiah•2y ago
what about giving random numbers in the code a name put them in constants somewhere
vl4do
vl4doOP•2y ago
if youre talking about the mins and maxes in the generators, i think its more than obvious that they are for cards, since the program is blackjack i can do that though
Omnissiah
Omnissiah•2y ago
Kristijan Kralj
MethodPoet
Do Magic Numbers Hurt Your Code?
Magic numbers are often seen as bad in the programming community. Why is this? Let's explore the reasoning behind it.
vl4do
vl4doOP•2y ago
thank you, ill fix them tomorrow
Haze.
Haze.•2y ago
you dont need outOfMoney == true, could just be outOfMoney
Landan
Landan•2y ago
Great case for a feature release of variable pattern matches. If only 🥺
vl4do
vl4doOP•2y ago
wdym?
Landan
Landan•2y ago
switch(variableToCheck)
{
case {Card1: "A", Card2:"F"} or {Card1: "F", Card2: "A"}: break;
default: //check other scenarios...
break;
}
switch(variableToCheck)
{
case {Card1: "A", Card2:"F"} or {Card1: "F", Card2: "A"}: break;
default: //check other scenarios...
break;
}
could be
case Pattern<TypeArg> BlackJackCase { Card1: "A", Card2: "F" } or {Card1: "F", Card2: "A"};

switch(variableToCheck)
{
case BlackJackCase: break;
default : break;
}
case Pattern<TypeArg> BlackJackCase { Card1: "A", Card2: "F" } or {Card1: "F", Card2: "A"};

switch(variableToCheck)
{
case BlackJackCase: break;
default : break;
}
Want results from more Discord servers?
Add your server