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

providing code here๐Ÿ‘‡
77 Replies
ื—ื’ื™ืช ืฉืžืขื•ื ื™
cs
cs
ero
eroโ€ข3y ago
you already have an open forum thream why open another one?
ื—ื’ื™ืช ืฉืžืขื•ื ื™
i closed it i wouldn't open a new one w/o closing the last one
ero
eroโ€ข3y ago
it's definitely still open i can see it
Pobiega
Pobiegaโ€ข3y ago
100% still there and active.
Pobiega
Pobiegaโ€ข3y ago
no, you deleted the initial message. thats not the same thing
Pobiega
Pobiegaโ€ข3y 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.
ero
eroโ€ข3y ago
it says close post right there
ื—ื’ื™ืช ืฉืžืขื•ื ื™
then new ppl will see it bc the last person had to go
Pobiega
Pobiegaโ€ข3y ago
Thats not how forum threads work
ero
eroโ€ข3y ago
that's not how it works man
Pobiega
Pobiegaโ€ข3y ago
you still had the 3rd most recent post "bumping" by re-posting just removes context
ื—ื’ื™ืช ืฉืžืขื•ื ื™
oh ok so do i close this one and open the last one?
ero
eroโ€ข3y ago
whatever just leave it like this now it's because you instantiate a new Random both times
ื—ื’ื™ืช ืฉืžืขื•ื ื™
but its a different random
ero
eroโ€ข3y ago
code too fast -> same random seed -> same random result
Pobiega
Pobiegaโ€ข3y ago
^
Pobiega
Pobiegaโ€ข3y ago
when you do new Random() it takes the seed from the computer clock
ื—ื’ื™ืช ืฉืžืขื•ื ื™
then how do i change it?
Pobiega
Pobiegaโ€ข3y ago
use the same random instance
ero
eroโ€ข3y ago
upgrade to .net 6 and use Random.Shared
ื—ื’ื™ืช ืฉืžืขื•ื ื™
i haven't learned it yet
ero
eroโ€ข3y 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โ€ข3y 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
ื—ื’ื™ืช ืฉืžืขื•ื ื™
where do i put it in the code
Pobiega
Pobiegaโ€ข3y ago
You don't just copy paste it, you read it
Pobiega
Pobiegaโ€ข3y ago
I mean exactly that Read the code, try to understand what it does and implement it in your code
ื—ื’ื™ืช ืฉืžืขื•ื ื™
what is ArgumentOutOfRangeException()
Pobiega
Pobiegaโ€ข3y ago
in this case, not really important since .Next(0,3) will never return anything else than 0, 1 or 2
ื—ื’ื™ืช ืฉืžืขื•ื ื™
i need to put it in internal class tho
Pobiega
Pobiegaโ€ข3y ago
Okay. Then do so.
ื—ื’ื™ืช ืฉืžืขื•ื ื™
i dont get, can you please help me by using things i know?
ื—ื’ื™ืช ืฉืžืขื•ื ื™
cs
cs
ื—ื’ื™ืช ืฉืžืขื•ื ื™
i changed it a bit but the only time it doesnt give me draw it gives me one paper one scissors
ero
eroโ€ข3y 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
ื—ื’ื™ืช ืฉืžืขื•ื ื™
how do i use another random? sometimes they dont tho
ero
eroโ€ข3y ago
pobiega gave you a version here
ื—ื’ื™ืช ืฉืžืขื•ื ื™
I haven't learned these things yet. I must use things i know bc its for school is there another version?
ero
eroโ€ข3y ago
what are "these things"?
ื—ื’ื™ืช ืฉืžืขื•ื ื™
switch,throw new, ArgumentOutOfRangeException() instead of switch i used if
ero
eroโ€ข3y ago
sure, you can do that
.logik.
.logik.โ€ข3y 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
ื—ื’ื™ืช ืฉืžืขื•ื ื™
i changed it after the scissors i changed it into paper but i opened a new random doesnt it supossed to change?
ero
eroโ€ข3y ago
no
.logik.
.logik.โ€ข3y 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.
ื—ื’ื™ืช ืฉืžืขื•ื ื™
is there another way to write random? so it wont have the same result
ero
eroโ€ข3y ago
yes, like this
ื—ื’ื™ืช ืฉืžืขื•ื ื™
but its still using the same random
ero
eroโ€ข3y ago
that's the whole point
.logik.
.logik.โ€ข3y 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โ€ข3y ago
you're supposed to use the same random what you're currently doing is create a new random every call
ero
eroโ€ข3y 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
ื—ื’ื™ืช ืฉืžืขื•ื ื™
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โ€ข3y ago
(also you're on an outdated .net version, count yourself lucky to get help at all)
ื—ื’ื™ืช ืฉืžืขื•ื ื™
i learned this a week ago ofc* its basic
.logik.
.logik.โ€ข3y 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
ื—ื’ื™ืช ืฉืžืขื•ื ื™
thx, i get the idea of it but im just not sure on how to code it this is suppossed to work?
ero
eroโ€ข3y ago
considering that's just your original code, no
ื—ื’ื™ืช ืฉืžืขื•ื ื™
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โ€ข3y ago
no, that's the exact reason it doesn't work
ero
eroโ€ข3y ago
2 randoms with the same seed are obviously going to give the same results you do this
ื—ื’ื™ืช ืฉืžืขื•ื ื™
??? i dont understand do i use for? bc i want to use it twice?
.logik.
.logik.โ€ข3y 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?
ื—ื’ื™ืช ืฉืžืขื•ื ื™
so i cant have one function for each choice?
ero
eroโ€ข3y ago
you shouldn't have one for each choice both methods literally do the same thing they have the same exact content
.logik.
.logik.โ€ข3y ago
in your main you want to call GetChoice to get a random value for choice1 and then call it again for choice2
ื—ื’ื™ืช ืฉืžืขื•ื ื™
how do i do that(sorry im just new to this) It just does it once and returns it twice

Did you find this page helpful?