C
C#•3y ago
v14do

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•3y ago
your Main is about80 lines long. You dont have enough methods
v14do
v14doOP•3y ago
i dont see where i can extract more methods, besides from 1 or 2 places max
forces of evil
forces of evil•3y ago
potentially there is a lot you can do like separating interface (console) from business logic (the card game)
v14do
v14doOP•3y ago
in a different class or method?
forces of evil
forces of evil•3y ago
classes
v14do
v14doOP•3y ago
ill try ty also any other tips are greatly appreciated
phaseshift
phaseshift•3y ago
these can easily be extracted.
phaseshift
phaseshift•3y ago
It can be taken much further as well. Basically big if blocks are obvious candidates
forces of evil
forces of evil•3y ago
also in general every state could be a class in itself
v14do
v14doOP•3y 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
forces of evil
forces of evil•3y 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
v14do
v14doOP•3y ago
so id need to unextract my other methods and then extract a bigger method?
forces of evil
forces of evil•3y 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
v14do
v14doOP•3y ago
yeah that makes sense i extracted the red box but cant the blue /purple i cant "ctrl + ." it
forces of evil
forces of evil•3y 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
v14do
v14doOP•3y ago
do you expect an answer from me?
forces of evil
forces of evil•3y 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
v14do
v14doOP•3y 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
forces of evil
forces of evil•3y 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
v14do
v14doOP•3y ago
so what would i need to replace the while true with?
forces of evil
forces of evil•3y ago
you should make a clean exit from inside there this could mean refactoring the flow of the program
v14do
v14doOP•3y ago
it only exits when you run out of money otherwise you play again infinitely
phaseshift
phaseshift•3y ago
so obviosuly while(HasMoney()) or similar
forces of evil
forces of evil•3y ago
something that gives the reader the intent it's the same for the extraction of the methods in the colore squares
v14do
v14doOP•3y 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
forces of evil
forces of evil•3y 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•3y ago
then add that in... while 'not (out of money or player quits)'
v14do
v14doOP•3y ago
BlazeBin - jsggvgkilcew
A tool for sharing your source code with the world!
forces of evil
forces of evil•3y 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
v14do
v14doOP•3y ago
i just did it
forces of evil
forces of evil•3y 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
v14do
v14doOP•3y ago
yeah there isnt but thats easy to implement i think
v14do
v14doOP•3y ago
v14do
v14doOP•3y 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
forces of evil
forces of evil•3y ago
what about giving random numbers in the code a name put them in constants somewhere
v14do
v14doOP•3y 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
forces of evil
forces of evil•3y 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.
v14do
v14doOP•3y ago
thank you, ill fix them tomorrow
Haze.
Haze.•3y ago
you dont need outOfMoney == true, could just be outOfMoney
Landan
Landan•3y ago
Great case for a feature release of variable pattern matches. If only 🥺
v14do
v14doOP•3y ago
wdym?
Landan
Landan•3y 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;
}

Did you find this page helpful?