5
\$\begingroup\$

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.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ Are we supposed to accept new commands after 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\$
    – Arnauld
    Jun 29, 2021 at 8:28
  • 1
    \$\begingroup\$ @Arnauld 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 bold halt. \$\endgroup\$
    – user100690
    Jun 29, 2021 at 8:29
  • 3
    \$\begingroup\$ The spec is clear for programs. But functions are supposed to be reusable. For this challenge, it seems like a function would need to maintain the game state in static variables. If claim does not reset the game, then the function can only be used for a single game which is against the default rule. I guess that's fine, but it should probably be clarified. \$\endgroup\$
    – Arnauld
    Jun 29, 2021 at 8:35
  • 2
    \$\begingroup\$ 1 chance on 9 to gain 1 dollar / 8 chances to loose 2.. I'm not playing that game!!! \$\endgroup\$
    – Kaddath
    Jun 29, 2021 at 9:19
  • 4
    \$\begingroup\$ @Kaddath: if you bet on the range 1-9 you will always win. 1 - (rangeEnd - rangeBegin + 1) / 10 in this case is 0.1 - きっと勝つ! \$\endgroup\$
    – G B
    Jun 29, 2021 at 9:21

2 Answers 2

3
\$\begingroup\$

JavaScript (ES6), 110 bytes

A function taking either (command, value) or (command, min_value, max_value).

Returns [chosen_number, profit] for bet, 0 for retire, or the final balance for claim.

(c,x,y)=>c[3]?c[5]?b=0:t:[k=-~(Math.random(b=b*2||1)*9),-t+(t+=(k<x|k>(y||x)?-2:1)*(y?b+(~y+x)*b/10:b))]
b=t=0

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 3, 279 bytes

import random as o
d=m=0;t=1
s=eval(input())
for x in s:c,r=x[0],x[1:];exec({'bet':'if len(r)>1:r=range(r[0],r[1]+1);d=1\nh=o.randint(1,9);k=[t,t-t*(r[-1]-r[0]+1)/10][d];k=[-k,k][h in r]\nif not(d or(h in r)):k+=k\nprint(h,k);m+=k;t*=2','retire':'t=1','claim':'print(m);1/0'}[c])

Try it online!

Takes a 2D list as input, this is a full program which will prompt for input. Exits on zero division error on claim.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.