# Goal

Given a deal result from Duplicate Contract Bridge, calculate the deal score.

Basically the same as iBug's question, which was deleted. I figured out this scoring system while trying to answer that question.

### Input

Input is a deal result, and whether or not the declaring side is vulnerable.

The deal result is a string in the following format:

[contract] [result]


The contract is defined as follows:

[tricks][suit][doubling]

• The tricks is a number 1-7 (both inclusive),
• The suitis one of C, H, S, D, or NT (Clubs, Hearts, Spades, Diamonds, and notrump)
• Suits are grouped as follows: notrump: NT, Major suits: H, S and Minor suits: C, D.
• doubling can be blank , X or XX (Undoubled, Doubled, Redoubled)

result is the resulting number of 'tricks' in relation to the contract. The value can be in the range [-13; +6], depending on the value (sign included).

The sum of contract number and result must be in the range[-6; 7], which means:

• For tricks= 1, result must be [-7; +6]
• For tricks= 2, result must be [-8; +5]
• ..
• For tricks= 7, result must be [-13; 0]

Examples of valid deal results:

1S 0, 4NT -3, 7NTXX -13, 3HX +3

Vulnerability can be input as a boolean or any truthy/falsy value.

### Output

Output is the scoring of the deal result.

The score is based on the number of tricks in a deal, and whether it reached the contract.

# Scoring

The score is a measure of the result compared to the contract.

If the contract is made, the score for each such deal consists of:

• Contract points, assigned to each trick bid and made
• Overtrick points, assigned for each trick taken over the contracted number of tricks
• A slam bonus for a small slam or grand slam contract
• A doubling bonus
• A game bonus

If the contract is defeated, the defenders receive

• Penalty points, assigned for every undertrick (negative value of result)

### Contract points:

Contract points are awarded for the tricks of the contract.

                                   Points per trick
Suit                      undoubled    doubled     redoubled
------------------------------------------------------------
Notrump
-first trick               40          80           160
-subsequent tricks         30          60           120
Major suits                 30          60           120
Minor suits                 20          40           80


### Overtrick points:

Overtrick points are awarded for the tricks made, more than the contract (result > 0)

                                Points per trick
Suit                      vulnerable    not vulnerable
If undoubled:
Notrump                     30             30
Major suits                 30             30
Minor suits                 20             20
Doubled (per trick)          200            100
Redoubled (per trick)        400            200


### Slam bonus

If a large contract is made, it earns a slam bonus:

• tricks = 6 earns a bonus of 500 if not vulnerable, and 750 if vulnerable
• tricks = 7 earns a bonus of 1000 if not vulnerable, and 1500 if vulnerable

### Doubled or redoubled bonus

If a doubled or redoubled contract is made, a bonus is earned:

• 50 points for a doubled contract
• 100 points for a redoubled contract

### Game bonus

A game bonus is awarded based on the contract points.

• Contract points below 100 earns a bonus of 50points.
• Contract points >= 100 earns a bonus of 300 if not vulnerable, and 500 if vulnerable

### Penalty

Each undertrick (negative result) gives a penalty point. No points are ganied for the contract, as it was failed.

                                      Points per undertrick
vulnerable                        not vulnerable
undoubled  doubled   redoubled     undoubled  doubled   redoubled
1st undertrick        100       200        400           100       100        200
2nd and 3rd, each     100       300        600           100       200        400
4th and subsequent    100       300        600           100       300        600


## Examples:

Deal result: 4DX +1, Vulnerable=true:

• Contract points = 4*40 = 160
• Overtrick points = 1*200 = 200
• Slam bonus = no bonus
• (re)doubled bonus = 50
• Game bonus = 500 (140>=100and vulnerable)

Result = 160+200+50+500 = 910

Deal result: 6NTXX 0, Vulnerable=false:

• Contract points = 4*40 = 160
• Overtrick points = 1*200 = 200
• Slam bonus = no bonus
• (re)doubled bonus = 50
• Game bonus = 500 (140>=100and vulnerable)

Result = 160+200+50+500 = 910

Deal result: 4SX -3, Vulnerable=false:

• Penalty = 1*100 + 2*200 = 500

Result = -500

Deal result: 7SNTXX -13, Vulnerable=true:

• Penalty = 1*400 + 2*600 + 10*600 = 7600

Result = -7600

# Example Input/Output:

Input is guaranteed to be valid.

1S 0 (non-vul)    # => 80
3NT +1 (non-vul)  # => 430
3NT +1 (vul)      # => 630
6CX +1 (vul)      # => 1740
4D -1 (vul)       # => -100
7NTXX -13 (vul)   # => -7600


Results can be checked against these tables.

I've also included a TIO with all results hardcoded in python, to check against.

• @PeterTaylor It is duplicate. I've edited that in, and updated the testcase – TFeld Aug 4 '18 at 6:56

# JavaScript (Node.js), 211204195 192 bytes

(r,v,[,c,s,d,t]=r.match(/(.)(.T?)(X*)(.*)/),l=d.length,m=s>'D',g=(c*(m+=2)+!!s[1])<<l)=>10*(t<0?10*(l*(v?3*t+1:-"13"[~t]||3*t+4)||t):g+(10*l*-~v||m)*t+5*l+25*(c>5&&(5-c)*~-~v)+5*(g>9?4*v+6:1))


Try it online! Link includes test cases. Edit: Saved 6 bytes by multiplying by 10 at the end. Saved 9 bytes thanks to @Arnauld. Explanation:

(r,v


Input the result as a string and the vulnerability as a boolean (or 1/0).

,[,c,s,d,t]=r.match(/(.)(.T?)(X*)(.*)/)


Split the string up into contract, suit, doubles and (over/under)tricks.

,l=d.length


Count the number of doubles.

,m=s>'D'


Track whether this is a major or minor suit.

g=(c*(m+=2)+!!s[1])<<l


Calculate the contract score. This is 3 per major suit or 2 per minor suit trick, plus 1 extra for no trump, and then doubled or redoubled as appropriate.

)=>10*


The score is always a multiple of 10. Subsequent comments take this multiplication into account.

(t<0


Do we have undertricks?

?10*


Undertricks are multiples of 100.

(l*(v?3*t+1:-"13"[~t]||3*t+4)


If they're doubled, then the potentially redoubled score depending on the vulnerability or number of undertricks.

||t)


But if they're not doubled, then just the number of undertricks.

:g


+(10*l*-~v||m)*t


Add on overtricks; per trick, if doubled then 100 potentially redoubled and doubled again if vulnerable, otherwise 30 for major or 20 for minor suits.

+5*l


Add on the potentially redoubled doubled game bonus.

+25*(c>5&&(5-c)*~-~v)


Add on the slam bonus if applicable.

+5*(g>9?4*v+6:1))


• @Arnauld Oh, of course, <<, I knew I was overlooking something but I just couldn't think of it. (I rearranged your answer slightly for ease of explanation.) – Neil Aug 5 '18 at 0:29

# Python 2, 230225221 220 bytes

I've posted this answer, as I found out the rules while creating this answer to the other question.

def f(D,V):v,s=D[:2];d=D.count('X');m=int(D.split()[1]);v=int(v);C=3-(s<'E');P=(v*C+(s=='N'))*2**d;W=-~V;return[-~W*25*max(0,v-5)+P+m*[C,10*W,20*W][d]+5*d+[5,30+20*V][P>9],((m-W)*3+7+~-V*max(0,m+3))*d*10or+W*m*5][m<0]*10


Try it online!

Saved:

• -4 bytes, thanks to Mr. Xcoder
• -1 byte, thanks to Jonathan Frech