RL-Dice: What am I doing wrong??

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
LesbianHam
Prole
Posts: 5
Joined: Thu Nov 21, 2019 12:07 am

RL-Dice: What am I doing wrong??

Post by LesbianHam »

I'm working on a simple rock, paper, scissors game to further familiarize myself with Love. So far, it's been going pretty well, I've been able to solve all my issues myself up until now. I'm at a loss for this one.

I'm using RL-Dice for random number generation (I chose to use RL-Dice for this because I plan to turn this into a bigger project that will benefit from it in the future).

I told Love to print either rock, paper, or scissors depending on the value of the die roll (1 = rock, 2 = paper, 3 = scissors). This seems to work most of the time, but occasionally the raw value is printed instead of printing rock, paper, or scissors. I can't figure out why this is happening. I'm sure it's something super simple that I'm overlooking. For the time being, I added a print function to tell the user that it's a glitch and they should try again, but that's not ideal! I want it to just function correctly 100% of the time!

I'd greatly appreciate it if someone could tell me why this is happening and how I can fix it.

Thank you!
RPS_bad.jpg
RPS_bad.jpg (22.91 KiB) Viewed 2464 times
ScissorsLua.jpg
ScissorsLua.jpg (122.07 KiB) Viewed 2464 times
RPS.love
(64.93 KiB) Downloaded 100 times
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: RL-Dice: What am I doing wrong??

Post by pgimeno »

Please don't post code snippets as images, use [code]...[/code] tags.

You're rolling the die up to four times.

You roll it a first time, and set the result to the number.

You roll it a second time, and if the roll is 1, you set the result to "rock".

If the second roll is 2 or 3, you roll it a third time and if the number in this third roll is 2, you set the result to "paper".

If the third roll was 1 or 3, you roll it a fourth time, and if the roll is 3, you set the result to "scissors". But if the last roll was 1 or 2, you don't set it; instead it keeps the numeric value of the first roll, which is the first result you set it to.

Besides the method being incorrect, this makes the results biased, because the probability of rock is 1/3 which is 0.333, the probability of paper is 2/9 or approx. 0.222, the probability of scissor is 4/27 which is approx. 0.148, and the probability of neither (a number) is 8/27 which is approx. 0.296.

What you need to do instead, is keep the value of a single roll in a variable, and compare that variable. For example:

Code: Select all

die = dice:new(3)
local roll = die:roll()
local die_result

if roll == 1 then
  die_result = "rock"
elseif roll == 2 then
  die_result = "paper"
else -- it must be 3
  die_result = "scissors"
end
That way you're rolling the die only once and using the result adequately.
User avatar
LesbianHam
Prole
Posts: 5
Joined: Thu Nov 21, 2019 12:07 am

Re: RL-Dice: What am I doing wrong??

Post by LesbianHam »

pgimeno wrote: Thu Nov 21, 2019 11:16 am Please don't post code snippets as images, use [code]...[/code] tags.

You're rolling the die up to four times.

You roll it a first time, and set the result to the number.

You roll it a second time, and if the roll is 1, you set the result to "rock".

If the second roll is 2 or 3, you roll it a third time and if the number in this third roll is 2, you set the result to "paper".

If the third roll was 1 or 3, you roll it a fourth time, and if the roll is 3, you set the result to "scissors". But if the last roll was 1 or 2, you don't set it; instead it keeps the numeric value of the first roll, which is the first result you set it to.

Besides the method being incorrect, this makes the results biased, because the probability of rock is 1/3 which is 0.333, the probability of paper is 2/9 or approx. 0.222, the probability of scissor is 4/27 which is approx. 0.148, and the probability of neither (a number) is 8/27 which is approx. 0.296.

What you need to do instead, is keep the value of a single roll in a variable, and compare that variable. For example:

Code: Select all

die = dice:new(3)
local roll = die:roll()
local die_result

if roll == 1 then
  die_result = "rock"
elseif roll == 2 then
  die_result = "paper"
else -- it must be 3
  die_result = "scissors"
end
That way you're rolling the die only once and using the result adequately.
Thanks so much, I really appreciate it! And my apologies for uploading my code as an image, I'm new to forums in general, not just this one, so I'm still figuring things out!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 215 guests