summaryrefslogtreecommitdiff
path: root/Analizator9000/Analizator9000/Contract.cs
blob: 82dd7b82009921142d44ee1d1bcceca586fb3f81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;

namespace Analizator9000
{
    /// <summary>
    /// Tuple describing contract parameters.
    /// </summary>
    public class Contract: IEquatable<Contract>
    {
        /// <summary>
        /// Denomination: non-trumps
        /// </summary>
        public const int DENOMINATION_NONTRUMP = 4;
        /// <summary>
        /// Denomination: spades
        /// </summary>
        public const int DENOMINATION_SPADES = 3;
        /// <summary>
        /// Denomination: hearts
        /// </summary>
        public const int DENOMINATION_HEARTS = 2;
        /// <summary>
        /// Denomination: diamonds
        /// </summary>
        public const int DENOMINATION_DIAMONDS = 1;
        /// <summary>
        /// Denomination: clubs
        /// </summary>
        public const int DENOMINATION_CLUBS = 0;
        /// <summary>
        /// Declarer: North
        /// </summary>
        public const int DECLARER_NORTH = 0;
        /// <summary>
        /// Declarer: East
        /// </summary>
        public const int DECLARER_EAST = 1;
        /// <summary>
        /// Declarer: South
        /// </summary>
        public const int DECLARER_SOUTH = 2;
        /// <summary>
        /// Declarer: West
        /// </summary>
        public const int DECLARER_WEST = 3;
        /// <summary>
        /// Vulnerability: none
        /// </summary>
        public const int VULNERABLE_NONE = 0;
        /// <summary>
        /// Vulnerability: all
        /// </summary>
        public const int VULNERABLE_BOTH = 1;
        /// <summary>
        /// Vulnerability: NS
        /// </summary>
        public const int VULNERABLE_NS = 2;
        /// <summary>
        /// Vulnerability: EW
        /// </summary>
        public const int VULNERABLE_EW = 3;
        /// <summary>
        /// Level of the contract.
        /// </summary>
        public int Level;
        /// <summary>
        /// Trump denomination, in numeric format.
        /// </summary>
        /// <see cref="BCalcWrapper"/>
        public int Denomination;
        /// <summary>
        /// Contract without double
        /// </summary>
        public const int MODIFIERS_NONE = 0;
        /// <summary>
        /// Contract doubled
        /// </summary>
        public const int MODIFIERS_DOUBLE = 1;
        /// <summary>
        /// Contract redoubled
        /// </summary>
        public const int MODIFIERS_REDOUBLE = 2;
        /// <summary>
        /// Modifiers - double or redouble.
        /// </summary>
        public int Modifiers;
        /// <summary>
        /// Declaring player, in numeric format.
        /// </summary>
        /// <see cref="BCalcWrapper"/>
        public int Declarer;
        /// <summary>
        /// Number of occurrences of the contract within board traveller.
        /// </summary>
        public int Frequency;
        /// <summary>
        /// Position of the contract in boards traveller table.
        /// </summary>
        public int TableRow;

        /// <summary>
        /// Constructor for contract without full information (just general trick-taking in a denomination).
        /// </summary>
        /// <param name="denom">Trump denomination.</param>
        /// <param name="decl">Declaring player.</param>
        public Contract(int denom, int decl)
        {
            this.Denomination = denom;
            this.Declarer = decl;
        }

        /// <summary>
        /// Constructor with full contract information.
        /// </summary>
        /// <param name="level">Contract level.</param>
        /// <param name="denom">Trump denomination.</param>
        /// <param name="decl">Declaring player.</param>
        /// <param name="modifiers">Modifiers: 1 = X, 2 = XX</param>
        /// <param name="frequency">Traveller frequency</param>
        /// <param name="tableRow">Traveller row index</param>
        public Contract(int level, int denom, int decl, int modifiers = 0, int frequency = 0, int tableRow = 0)
        {
            this.Frequency = frequency;
            this.Level = level;
            this.Denomination = denom;
            this.Declarer = decl;
            this.Modifiers = modifiers;
            this.TableRow = tableRow;
        }

        /// <summary>
        /// IEquatable method for comparing (checking equality) of two tuples.
        /// </summary>
        /// <param name="other">Tuple to compare to.</param>
        /// <returns>TRUE if both tuple components are equal.</returns>
        public bool Equals(Contract other)
        {
            return this.Denomination == other.Denomination && this.Declarer == other.Declarer &&
                this.Level == other.Level && this.Modifiers == other.Modifiers && this.Frequency == other.Frequency && this.TableRow == other.TableRow;
        }

        /// <summary>
        /// Method used in object comparisons, taking the Equals() method into account.
        /// <see cref="Equals"/>
        /// </summary>
        /// <returns>Object hash</returns>
        public override int GetHashCode()
        {
            int hash = this.Denomination;
            hash *= 10;
            hash += this.Declarer;
            hash *= 10;
            hash += this.Level;
            hash *= 10;
            hash += this.Modifiers;
            hash *= 10;
            hash += this.Frequency;
            hash *= 10;
            hash += this.TableRow;
            return hash;
        }

        /// <summary>
        /// Calculates score for contract result
        /// </summary>
        /// <param name="result">BCalc output result</param>
        /// <param name="vulnerability">Vulnerability for the deal</param>
        /// <returns>Score for NS pair</returns>
        public int getScore(BCalcResult result, int vulnerability)
        {
            // All PASS.
            if (this.Level == 0)
            {
                return 0;
            }
            if (BCalcWrapper.table[this.Declarer] != result.declarer)
            {
                throw new Exception("Declarer mismatch!");
            }
            // Determining vulnerability of the contract
            bool vulnerable = (vulnerability == Contract.VULNERABLE_BOTH)
                || (vulnerability == Contract.VULNERABLE_EW && this.Declarer == Contract.DECLARER_EAST)
                || (vulnerability == Contract.VULNERABLE_EW && this.Declarer == Contract.DECLARER_WEST)
                || (vulnerability == Contract.VULNERABLE_NS && this.Declarer == Contract.DECLARER_NORTH)
                || (vulnerability == Contract.VULNERABLE_NS && this.Declarer == Contract.DECLARER_SOUTH);
            int score = 0;
            // Contract not made
            if (this.Level + 6 > result.tricks)
            {
                int undertricks = this.Level + 6 - result.tricks;
                if (this.Modifiers < 1) // undoubled undertricks
                {
                    score = vulnerable ? -100 : -50;
                    score *= undertricks;
                }
                else // (re-)doubled undertricks
                {
                    do
                    {
                        if (undertricks == 1) // first undertrick: 100 non-vul, 200 vul
                        {
                            score -= vulnerable ? 200 : 100;
                        }
                        else
                        {
                            if (undertricks <= 3 && !vulnerable) // second non-vul undertrick: 200
                            {
                                score -= 200;
                            }
                            else // further undertricks: 300
                            {
                                score -= 300;
                            }
                        }
                        undertricks--;
                    }
                    while (undertricks > 0);
                    score *= this.Modifiers; // redouble doubles the score
                }
            }
            else // Contract made, yay!
            {
                int parTricks = this.Level;
                do
                {
                    if (this.Denomination == Contract.DENOMINATION_NONTRUMP && parTricks == 1) // first non-trump trick: 40
                    {
                        score += 40;
                    }
                    else // other tricks
                    {
                        switch (this.Denomination)
                        {
                            case Contract.DENOMINATION_NONTRUMP:
                            case Contract.DENOMINATION_SPADES:
                            case Contract.DENOMINATION_HEARTS:
                                score += 30;
                                break;
                            case Contract.DENOMINATION_DIAMONDS:
                            case Contract.DENOMINATION_CLUBS:
                                score += 20;
                                break;
                        }
                    }
                    parTricks--;
                }
                while (parTricks > 0);
                if (this.Modifiers > 0) // (re-)doubled tricks
                {
                    score *= this.Modifiers * 2;
                }
                score += (score >= 100) ? (vulnerable ? 500 : 300) : 50; // game premium
                if (this.Level == 7) // grand slam premium
                {
                    score += vulnerable ? 1500 : 1000;
                }
                else if (this.Level == 6) // small slam premium
                {
                    score += vulnerable ? 750 : 500;
                }
                score += this.Modifiers * 50; // failed (re-)double premium
                int overtricks = result.tricks - this.Level - 6;
                score += this.Modifiers > 0
                    ? (vulnerable ? 200 : 100) * this.Modifiers * overtricks // (re-)double overtricks: 100/200/200/400
                    : overtricks * ((this.Denomination == Contract.DENOMINATION_CLUBS || this.Denomination == Contract.DENOMINATION_DIAMONDS) ? 20 : 30); // undoubled overtricks
            }
            // If EW played the board, the score is shifted to the other side
            if (this.Declarer == Contract.DECLARER_WEST || this.Declarer == Contract.DECLARER_EAST)
            {
                score = -score;
            }
            return score;
        }
    }
}