Your task is to create either a console "app" or a function which allows the user to bet on certain numbers or retire / claim rounds.
Bets
Your program or function will prompt for a command after an action, and the bet is the most important, crucial to the operation of the app.
A bet looks something like this:
bet 5
bet 4 9
Each time the user bets, your application should choose a 1-digit number (1-9, not including zeroes) at random. It is reasonable to assume that your pseudo-random number generator is uniformly random.
When your application receives a bet
command, there may be 1 or 2 numbers after bet
. They will be 1-digit numbers from 1-9.
If there is 1 number, that means that the user is willing to bet on a single number in the range 1-9
coming out.
If there are 2 numbers, things get slightly more complicated. This means the user is instead betting on a range: with bounds included. So bet 4 9
means the user is betting on a number in the range 4-9
being selected.
The first time you bet in a "round" (explained later), you win 1$ if your number is selected, but lose 2$ if your number is not selected.
However, if the user bets on a range (which has a higher chance of winning), the money won is less: the first bet in a round wins 1 - (rangeEnd - rangeBegin + 1) / 10
while losing that bet will deduct double that amount from the player's balance.
Each subsequent bet doubles the "stake". The second bet in a round wins 2$ for one number and 2 - 2 (rangeEnd - rangeBegin + 1) / 10
for a range, and losses are worth double each. Then the third bet wins 4$ for one number, 4 - 4 (rangeEnd - rangeBegin + 1) / 10
for a range, etc.
After a bet, some output should occur: an indication of the result as well as the chosen number (chosen <number>
) If the bet is won, something like
won 1 chosen 5
should be output. If the bet is lost, then something like
lost 2 chosen 7
Retirement
At any point, the user is also allowed to "retire" the current round if he or she is too afraid to lose money. This adds the player's current money to the main balance, which is initially set to zero. This also triggers a new round in which the first bet wins 1$, second 2$, etc with the above rules for ranges also applying. The command is simply
retire
This produces no output.
Claims
When the player decides to leave the "casino", he or she must "claim" the money, which in the context of your app must output the player's balance and halt. No commands should be accepted after claim
.
Command:
claim
Example
This is a console app. Rules for functions and other general info are provided below.
Note that >
before commands are not necessary. They are just shown here for clarity.
Also note that the numbers here were chosen by me and not an RNG, but that your code is expected to produce the numbers non-deterministically.
> bet 6
lost 2 chosen 3
> bet 3 7
won 1 chosen 6
> bet 9
won 4 chosen 9
> retire
> bet 1
lost 2 chosen 6
> claim
1
In the second bet of round 1, the range was 3-7, so the player won 2 - 2 (7 - 3 + 1) / 10 = 2 - 2 (5/10) = 2 - 1 = 1
dollars.
Rules for functions
Functions are allowed, but they have a different I/O format.
Input can be taken as a newline-separated string of commands, an array/list of commands, etc. Nothing new there. The commands can be taken as sub-lists too, so bet 4 9
can become ['bet', 4, 9]
in JavaScript, for instance, and retire
could be ['retire']
.
Return value can be any of: newline-separated string of outputs, array/list of outputs, etc. Post-bet output can be a list of numbers (-2 4
for lost 2 chosen 4
and 64 9
for won 64 chosen 9
), and you will have to output floating point numbers from time to time.
As @Arnauld pointed out in the comments, your function might not necessarily be reusable (see here).
Other rules
Invalid input will never be given. So no ranges where the end is less than or equal to the range beginning, no numbers outside the range 1-9, no commands other than bet
, retire
, claim
, and correct number of arguments: bet
taking 1 or 2 args, and the latter two commands taking no arguments. Floating point errors are acceptable.
This is code-golf with the shortest answerer in bytes winning. No standard loopholes, please.
claim
? If so, does it reset to the first round? (I think that's the only way to make function re-usable for several games anyway.) \$\endgroup\$When the player decides to leave the "casino", he or she must "claim" the money, which in the context of your app must output the player's balance and *halt*.
. In case this wasn't clear, I might boldhalt
. \$\endgroup\$1 - (rangeEnd - rangeBegin + 1) / 10
in this case is 0.1 - きっと勝つ! \$\endgroup\$