C
C#•2y ago
avishy

I built a rock paper scissors game but it always returns a draw. can anyone help me?

providing code here👇
77 Replies
avishy
avishyOP•2y ago
cs
cs
ero
ero•2y ago
you already have an open forum thream why open another one?
avishy
avishyOP•2y ago
i closed it i wouldn't open a new one w/o closing the last one
ero
ero•2y ago
it's definitely still open i can see it
avishy
avishyOP•2y ago
it says "open post"
Pobiega
Pobiega•2y ago
100% still there and active.
avishy
avishyOP•2y ago
i closed it tho
Pobiega
Pobiega•2y ago
no, you deleted the initial message. thats not the same thing
avishy
avishyOP•2y ago
also the post
Pobiega
Pobiega•2y ago
and also, why did you do that? you were given a lot of useful help in that thread. there is no reason to open a new thread and ask teh same question.
avishy
avishyOP•2y ago
ero
ero•2y ago
it says close post right there
avishy
avishyOP•2y ago
then new ppl will see it bc the last person had to go
Pobiega
Pobiega•2y ago
Thats not how forum threads work
ero
ero•2y ago
that's not how it works man
avishy
avishyOP•2y ago
oh i reopened it
Pobiega
Pobiega•2y ago
you still had the 3rd most recent post "bumping" by re-posting just removes context
avishy
avishyOP•2y ago
oh ok so do i close this one and open the last one?
ero
ero•2y ago
whatever just leave it like this now it's because you instantiate a new Random both times
avishy
avishyOP•2y ago
but its a different random
ero
ero•2y ago
code too fast -> same random seed -> same random result
Pobiega
Pobiega•2y ago
^
avishy
avishyOP•2y ago
what
Pobiega
Pobiega•2y ago
when you do new Random() it takes the seed from the computer clock
avishy
avishyOP•2y ago
then how do i change it?
Pobiega
Pobiega•2y ago
use the same random instance
ero
ero•2y ago
upgrade to .net 6 and use Random.Shared
avishy
avishyOP•2y ago
i haven't learned it yet
ero
ero•2y ago
? haven't learned what? you don't even need RockPaperScissorsOne and RockPaperScissorsTwo. you can just have a singular RockPaperScissors. there's no need for two of them
Pobiega
Pobiega•2y ago
public class RPS
{
private Random _random = new Random();

public string GetChoice()
{
return _random.Next(0, 3) switch
{
0 => "rock",
1 => "paper",
2 => "scissors",
_ => throw new ArgumentOutOfRangeException()
};
}
}
public class RPS
{
private Random _random = new Random();

public string GetChoice()
{
return _random.Next(0, 3) switch
{
0 => "rock",
1 => "paper",
2 => "scissors",
_ => throw new ArgumentOutOfRangeException()
};
}
}
Random.Shared is the better way, but this shows how to re-use a random instance
avishy
avishyOP•2y ago
where do i put it in the code
Pobiega
Pobiega•2y ago
You don't just copy paste it, you read it
avishy
avishyOP•2y ago
wdym
Pobiega
Pobiega•2y ago
I mean exactly that Read the code, try to understand what it does and implement it in your code
avishy
avishyOP•2y ago
what is ArgumentOutOfRangeException()
Pobiega
Pobiega•2y ago
in this case, not really important since .Next(0,3) will never return anything else than 0, 1 or 2
avishy
avishyOP•2y ago
i need to put it in internal class tho
Pobiega
Pobiega•2y ago
Okay. Then do so.
avishy
avishyOP•2y ago
i dont get, can you please help me by using things i know?
avishy
avishyOP•2y ago
cs
cs
avishy
avishyOP•2y ago
i changed it a bit but the only time it doesnt give me draw it gives me one paper one scissors
ero
ero•2y ago
same issue still you're creating a new Random instance two times in a row, they both have the same seed (because the seed is based on the system time or something?), meaning they both get the same result
avishy
avishyOP•2y ago
how do i use another random? sometimes they dont tho
ero
ero•2y ago
pobiega gave you a version here
avishy
avishyOP•2y ago
I haven't learned these things yet. I must use things i know bc its for school is there another version?
ero
ero•2y ago
what are "these things"?
avishy
avishyOP•2y ago
switch,throw new, ArgumentOutOfRangeException() instead of switch i used if
ero
ero•2y ago
sure, you can do that
.logik.
.logik.•2y ago
Also the changes you made in the recent version don't make sense
public static string RockPaperScissorsOne()
{
Random random = new Random();
int randomI = random.Next(0, ropasc.Length);
if (randomI == 0)
return "rock";
else if (randomI == 1)
return "scissors";
else
return "scissors";
}
public static string RockPaperScissorsOne()
{
Random random = new Random();
int randomI = random.Next(0, ropasc.Length);
if (randomI == 0)
return "rock";
else if (randomI == 1)
return "scissors";
else
return "scissors";
}
you've made choice 1 be rock 1/3 of the time and scissors 2/3 of the time and then
public string GetChoiceTwoAsGraphic()
{
if (choice2 == "rock")
return ropasc[0];
else if (choice2 == "paper")
return ropasc[1];
else if (choice1 == "scissors")
return ropasc[2];
else { return "none"; }
}
public string GetChoiceTwoAsGraphic()
{
if (choice2 == "rock")
return ropasc[0];
else if (choice2 == "paper")
return ropasc[1];
else if (choice1 == "scissors")
return ropasc[2];
else { return "none"; }
}
you made the choice2 graphic depend on choice1
avishy
avishyOP•2y ago
i changed it after the scissors i changed it into paper but i opened a new random doesnt it supossed to change?
ero
ero•2y ago
no
.logik.
.logik.•2y ago
From https://learn.microsoft.com/en-us/dotnet/api/system.random?view=net-7.0 On most Windows systems, Random objects created within 15 milliseconds of one another are likely to have identical seed values.
Random Class (System)
Represents a pseudo-random number generator, which is an algorithm that produces a sequence of numbers that meet certain statistical requirements for randomness.
avishy
avishyOP•2y ago
is there another way to write random? so it wont have the same result
ero
ero•2y ago
yes, like this
avishy
avishyOP•2y ago
but its still using the same random
ero
ero•2y ago
that's the whole point
.logik.
.logik.•2y ago
yes Pobiega already showed you - you create a field in RPS that is Random object and then use that field to generate your random numbers
ero
ero•2y ago
you're supposed to use the same random what you're currently doing is create a new random every call
avishy
avishyOP•2y ago
ero
ero•2y ago
you just said you can replace switch with if-else do that we're not here to babysit you, this is incredibly basic don't expect code that you just copy and paste to work
avishy
avishyOP•2y ago
public static string RockPaperScissorsOne()
{
Random random = new Random();
int randomI = random.Next(0, ropasc.Length);

if (randomI == 0)
return "rock";
else if (randomI == 1)
return "paper";
else
return "scissors";
}
public static string RockPaperScissorsOne()
{
Random random = new Random();
int randomI = random.Next(0, ropasc.Length);

if (randomI == 0)
return "rock";
else if (randomI == 1)
return "paper";
else
return "scissors";
}
ero
ero•2y ago
(also you're on an outdated .net version, count yourself lucky to get help at all)
avishy
avishyOP•2y ago
i learned this a week ago ofc* its basic
.logik.
.logik.•2y ago
think of Random like a die and everytime you call Random.Next, you're rolling the die and getting a result if you create two Random instances within 15 milliseconds of each other, you're creating the exact same die twice and so when you roll the dice, you get the same results on each die. However if you roll one die twice instead, you'll get different results. thats why you want to use 1 die rather than creating two dice set on the same seed
avishy
avishyOP•2y ago
thx, i get the idea of it but im just not sure on how to code it this is suppossed to work?
ero
ero•2y ago
considering that's just your original code, no
avishy
avishyOP•2y ago
i just dont see whats wrong i know that the seed is the same but i used two randoms so i thought its supposed to work
ero
ero•2y ago
no, that's the exact reason it doesn't work
avishy
avishyOP•2y ago
so what do i do?
ero
ero•2y ago
2 randoms with the same seed are obviously going to give the same results you do this
avishy
avishyOP•2y ago
??? i dont understand do i use for? bc i want to use it twice?
.logik.
.logik.•2y ago
just to piggyback off this to get a better understanding of what I'm reading from the docs, the issue here is that if you have two calls to GetChoice simultaneously, one is going to return 0 but if you used Random.Shared, you won't have a problem? Guessing this also wouldn't be an issue if the program was single-threaded?
avishy
avishyOP•2y ago
so i cant have one function for each choice?
ero
ero•2y ago
you shouldn't have one for each choice both methods literally do the same thing they have the same exact content
.logik.
.logik.•2y ago
in your main you want to call GetChoice to get a random value for choice1 and then call it again for choice2
avishy
avishyOP•2y ago
how do i do that(sorry im just new to this) It just does it once and returns it twice
Want results from more Discord servers?
Add your server