Scoring [Randomizing whether or not you score a basket any time you shoot the ball.]

I’m adding in very basic functionality that allows a player to freely shoot around on a basketball court; they can take a two or three pointer, and can “x score” to see how many points they have.

Currently, this is the code for scoring:

Instead of shooting when the noun is a two-pointer:
now the score is the score plus 2;

Instead of shooting when the noun is a three-pointer:
now the score is the score plus 3;

I’ve researched randomness, but can’t quite piece together how to do the following: whenever a player takes a two-pointer or three-pointer, have it randomly make it in, or miss, based on some percentage.

Basically, “if player shoots two-pointer, then go in 65% of the time” for example. (Perhaps in the future I might have it so that this percentage changes based on some attributes of the player, but for now I’ll keep it simple.)

It feels like the logic is easy, but it’s clearly evading me. I’d appreciate any pointers! Cheers!

Sounds fun! I’d probably do this as a carry out rule rather than an instead one (see this thread for a discussion of why), but the syntax you’re looking for is “if a random chance of 2 in 3 succeeds” (or whatever you want the odds to be), and then a matching “otherwise” clause to handle the miss. See documentation here.

Hope this is helpful!

1 Like

”increase the score by 2" is a bit nicer to read than “now the score is the score plus 2”.

So easy! I even looked at that earlier and misinterpreted how that might address my needs. Upon trying it out, it works perfectly (it didn’t dawn on me initially that I could tailor this to percentages by doing something like “65 in 100”). Super helpful, thanks!

@rockwalrus You’re totally right. I believe I pulled this wording from one of the first examples I came across, but I’ll clean this up. Thanks for the pointer!

1 Like