Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

You're writing a program for a new automated roulette croupier to be installed in a major casino. Unfortunately, the last guy bailed out on the company and published buggy software. You're stuck with a thousand burned flash chips with only a few kilobytes of space to store a working program. Can you do it?

The task is to write a program which will accurately handle bets, wins and losses of a roulette game.

It will be run as a session. When it initialises, players place their bets which are sent through STDIN. Once all bets have been received, the input for where the ball landed is given. The program must then calculate the winnings for each of the players and the casino.

A single zero wheel is assumed. Assume dollars will be bet (not chips) and that the input is always valid. You only need to support the "outside bets" and numbered bets (lists of numbers, not named.) Payouts are described on the Wikipedia article.

Outside bet names: c1, c2, c3 (1st-3rd column), d1, d2, d3 (1st-3rd dozen), o (odd), e (even), r (red), b (black), 1-18, 19-36.

Example session (> indicates input, < indicates output):

> 7 800

Place a bet on the number "7" only, for $800.

> 8,9,10,11 50

Place a bet on the range "8,9,10,11", for $50.

> c1 2000

Place a bet on "1st column" (1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34) for $2000.

> ok
> 7

All players done. Pocketed in 7.

< 1 28800
< 2 0
< 3 6000

Payout details: #1 gets 36x original bet (35-to-1), #2 did not win and #3 gets 3x original bet (2-to-1)

< c -34750

Casino is down $34,750.

Deciding the winner

  • Code space is critical so the smaller the program the better. The smallest program implementing all of the described features will be the winner, unless bonus features are present, in which case the best overall program will be decided based on the combination of size and features supported.

  • The program must implement at least the bets described above, but support for other types, as well as number bets such as splits and baskets will be considered and may add bonus points.

share|improve this question
1  
If Player 2 did win, how much would he get? 9x? Could you give 5 numbers as input? –  beary605 Jan 26 '13 at 0:42
    
@beary605 The odd payouts are listed on the Wikipedia article I linked to -- just add one to get the amount paid. –  Thomas O Jan 26 '13 at 1:50
    
Are only numbers allowed to be combined by commas, or do we have to handle something like c1,c2 100? –  Keith Randall Jan 26 '13 at 17:45
    
@Keith Only numbers can be combined by commas. –  Thomas O Jan 26 '13 at 20:02
    
Shouldn't c in your example be -31950 = 800+50+2000-28800-6000? –  Keith Randall Jan 26 '13 at 21:56
show 2 more comments

2 Answers 2

Python, 443 440 chars

R=range
N=37
V=91447186090
M={'e':R(0,N,2),'o':R(1,N,2),'r':[i for i in R(N)if V>>i&1],'b':[i for i in R(1,N)if~V>>i&1],'1-18':R(1,19),'19-36':R(19,N)}
for i in R(1,4):M['c%d'%i]=R(i,N,3);M['d%d'%i]=R(i*12-11,i*12+1)
for i in R(1,N):M[`i`]=[i]
B=[]
r=raw_input()
while'ok'!=r:x,k=r.split();B+=[(sum((M[y]for y in x.split(',')),[]),int(k))];r=raw_input()
z=input()
c=0
i=1
for b,k in B:v=36/len(b)*k*(z in b);print i,v;c+=k-v;i+=1
print'c',c

Makes M, a map from bet names to a list of numbers that win that bet. Then it reads the bets in, storing each one as a pair (list of numbers, dollar amount) in the list B. Finally, reads in the spin z and computes the payouts.

share|improve this answer
    
Would the shorthand for repr(i) save chars on line 6? –  beary605 Jan 26 '13 at 22:30
    
@beary605: yup, saves 3 chars. –  Keith Randall Jan 26 '13 at 22:57
add comment

Python (548)

Wow, the biggest thing I've done in a while.

r=[];R=range
for i in[1,12,19,30]:r+=R(i,i+7+i%2*2,2)
a=[];b='01';c=''
while'ok'!=c:
 if'c'in c:c=R(int(c[1]),37,3)
 if'd'in c:c=R(1+(int(c[1])-1)*12,13+(int(c[1])-1)*12)
 if'r'in c:c=r
 if'b'in c:c=filter(lambda x:x not in r,R(1,36))
 if'o'in c:c=R(1,36,2)
 if'e'in c:c=R(2,36,2)
 if'-'in c:e=18*(c[1]>'-');b[0]=R(1+e,19+e)
 if"'"in`c`:c=map(int,b[0].split(','))
 a+=[[c,b[1]]];b=raw_input().split();c=b[0]
a=a[1:]
i=input()
z=[(i in p[0])*int(p[1])*(36/len(p[0])+1)-int(p[1])for p in a]
for j,k in enumerate(z):print j+1,max(k,0)
print"c",-sum(z)

Things that can probably be compressed:

  • generating the numbers for things like e, o, d2
  • the math formula for calculating z
share|improve this answer
    
[x for x in R(1,36)if x not in r] is two characters shorter than filter(lambda x:x not in r,R(1,36)) –  Strigoides Jan 26 '13 at 15:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.