Condition overlaps?
Can anyone confirm this as the above code will not cause any overlap in value
54 Replies
No overlap or gaps occur? and every value in the range from 0 to 1 is accounted for. Am i good to go?
instead of going into floating points, a better approach is to have 3 numbers 0,1,2 where
0 = rock
1 = paper
2 = scissors
so basically u cab randomly generate an int between 0 to 2 and then based on which number is picked u make the corresponding move
So this may result in errors ?
how about this
how do you handle the player input?
yes, because
1/3
is not the same as 0.1 + 0.2
and is not the same as 0.3
.yea here we are taking the entire value of 1/3 right 0.33333333333
we dont require any round off result here as in case of calcuating money , its pretty accurate result (floating value we are comparing 1/3= 0.33333333333).
you're right, but 0.3333... isn't 0.3333....
1/3 is actually 0.3333333730697632
knew that was coming
it is an automatic fail
there's ~0.00000004 of bias towards an outcome
and 0.666666 has a bias too: 0.666666686534881591796875
that's another fail too
https://stackoverflow.com/a/42321673 <-- i found this to generate a single cryptographically secure random number
Stack Overflow
Javascript: Generate a random number within a range using crypto.ge...
I understand you can generate a random number in JavaScript within a range using this function:
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
it's overkill, but unbiased
This is so frustrating then why in the console it gives 1/3 as 0.3333333333
because javascript is tricking you
it's hidding the error part from you
how can i scan when its hiding ?when its not hiding
no idea ¯\_(ツ)_/¯
always assume all floats are wrong
then this gives error too while cabing
my answer depends on the rest of your code
can you show it?
so, you don't even have a way for the player to pick an option?
I was using 1/3 actually
I am still working on it
you've started """"wrong""""
you don't have anywhere that has the possible values
Among three buttons he will click one which he wants either rock or paper or scissor
which values?
well, the valid inputs
something like this:
and then, you can just select a random key, for the enemy
yes this is the further modification
it should be the first step
you have to know the valid inputs before you write anything
planning is extremely important
ohh
<button onclick="playGame('Rock')">Rock</button>
<button>Paper</button>
<button>Scissors</button>
i have planned na
it's 2024, there's almost no place for
onclick
also, here's a much better way to do it:
YES
As the first step right?
yes, as part of the ui
instead of onClick we can use event listeners.
instead of 3 onclicks, you can have 1 onsubmit on the form
This 1 onSubmit gives any value right either rock,paper,or scissor
yes, through
event.submitter
which contains the button that submitted the form
and buttons can have html, which means that you can add an image and text too, to make it pretty
or use some html to make the emoji bigyes getting
my main doubt is about the float condition comparision
What can i do to avoid the float issues
nothing, throw away the code
this is a way to pick option for the players i got it
yes, that is correct
Comparision is becomming tricky
that's why i said to throw it away
it really is a lost cause
Meaning?
means: start over
seriously i am not getting what are you comming to say.
im coming at that the approach is not good
it's dead on arrival
it's just troubles and won't do what you need
to stop it and use a different one
apart from gettting random number and then comparing what are the other ways🤔
not using that
get a random key from this, for example
const keys = Object.keys(moves);
const randomKey = keys[Math.floor(Math.random() * keys.length)];
console.log(randomKey, moves[randomKey]);
that's one way to do it, yes
that should work
Yes exactly now i am getting planning is very very important
planning is the essence of a project
a project without planning is just a pile of bad code over bad code
I was doing this as how everyone normally does
nobody sane does that
actually i have seen this exact approach before once as well 😂
but yah I don't think pretty much no one does it like this
it doesnt make sense for this project
yep
working with the keys is a lot easier
and you always guarantee a valid input