summaryrefslogtreecommitdiff
path: root/src/ParContract.cs
blob: d767f0e169219df3cc99ece4e57f580cd41eae95 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCDD
{
    class ParContract
    {
        public int Level = 0;
        public char Denomination;
        public char Declarer;
        public bool Doubled = false;
        public int Score = 0;

        public ParContract() { }

        public ParContract(int level, char denom, char declarer, bool doubled, int score)
        {
            this.Level = level;
            this.Denomination = denom;
            this.Declarer = declarer;
            this.Doubled = doubled;
            this.Score = score;
        }

        public ParContract Validate()
        {
            if (this.Score == 0)
            {
                return this;
            }
            if (this.Level < 1 || this.Level > 7)
            {
                throw new ParScoreInvalidException("Invalid par contract level: " + this.Level.ToString());
            }
            if (!"CDHSN".Contains(this.Denomination))
            {
                throw new ParScoreInvalidException("Invalid par contract denomination: " + this.Denomination);
            }
            if (!"NESW".Contains(this.Declarer))
            {
                throw new ParScoreInvalidException("Invalid par contract declarer: " + this.Declarer);
            }
            return this;
        }

        override public String ToString()
        {
            if (this.Score == 0)
            {
                return "PASS";
            }
            String contract = this.Level.ToString() + this.Denomination;
            String risk = this.Doubled ? "x" : "";
            String declarer = " " + this.Declarer;
            String result = " " + this.Score.ToString("+#;-#;0");
            return contract + risk + declarer + result;
        }

        public override bool Equals(object other)
        {
            ParContract obj = (ParContract)(other);
            return this.Level == obj.Level && this.Denomination == obj.Denomination && this.Score == obj.Score;
        }

        public override int GetHashCode()
        {
            return this.Score + this.Level + 10000 * this.Denomination;
        }

        public int CalculateScore(int tricks, bool vulnerable = false)
        {
            if (this.Level == 0)
            {
                return 0;
            }
            int score = 0;
            if (this.Level + 6 > tricks)
            {
                int undertricks = this.Level + 6 - tricks;
                if (this.Doubled)
                {
                    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);
                }
                else
                {
                    score = vulnerable ? -100 : -50;
                    score *= undertricks;
                }
            }
            else
            {
                int parTricks = this.Level;
                do
                {
                    if (this.Denomination == 'N' && parTricks == 1) // first non-trump trick: 40
                    {
                        score += 40;
                    }
                    else // other tricks
                    {
                        switch (this.Denomination)
                        {
                            case 'N':
                            case 'S':
                            case 'H':
                                score += 30;
                                break;
                            case 'D':
                            case 'C':
                                score += 20;
                                break;
                        }
                    }
                    parTricks--;
                }
                while (parTricks > 0);
                if (this.Doubled)
                {
                    score *= 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;
                }
                if (this.Doubled)
                {
                    score += 50;
                }
                int overtricks = tricks - this.Level - 6;
                score += this.Doubled
                    ? (vulnerable ? 200 : 100) * overtricks // (re-)double overtricks: 100/200/200/400
                    : overtricks * ((this.Denomination == 'C' || this.Denomination == 'D') ? 20 : 30); // undoubled overtricks
            }
            if (this.Declarer == 'E' || this.Declarer == 'W')
            {
                score = -score;
            }
            return score;
        }

        public bool Higher(ParContract obj)
        {
            return (this.Level > obj.Level
                || (this.Level == obj.Level
                && Array.IndexOf(BCalcWrapper.DENOMINATIONS, this.Denomination) > Array.IndexOf(BCalcWrapper.DENOMINATIONS, obj.Denomination)));
        }

        public ParContract GetDefense(int[,] ddTable, bool vulnerable)
        {
            int declarerIndex = Array.IndexOf(BCalcWrapper.PLAYERS, this.Declarer);
            int denominationIndex = Array.IndexOf(BCalcWrapper.DENOMINATIONS, this.Denomination);
            if (this.Level != 0 && this.Level + 6 <= ddTable[declarerIndex, denominationIndex])
            {
                List<int> defendersIndexes = new List<int>();
                defendersIndexes.Add((declarerIndex + 1) & 3);
                defendersIndexes.Add((declarerIndex + 3) & 3);
                List<ParContract> possibleDefense = new List<ParContract>();
                int scoreSquared = this.Score * this.Score;
                for (int i = 0; i < 5; i++)
                {
                    int level = this.Level;
                    if (i <= denominationIndex)
                    {
                        level++;
                    }
                    if (level <= 7)
                    {
                        foreach (int defender in defendersIndexes)
                        {
                            if (level + 6 > ddTable[defender, i])
                            {
                                ParContract defense = new ParContract(level, BCalcWrapper.DENOMINATIONS[i], BCalcWrapper.PLAYERS[defender], true, 0);
                                defense.Score = defense.CalculateScore(ddTable[defender, i], vulnerable);
                                if (scoreSquared > this.Score * defense.Score)
                                {
                                    possibleDefense.Add(defense);
                                }
                            }
                        }
                    }
                }
                if (possibleDefense.Count > 0)
                {
                    possibleDefense.Sort((x, y) => Math.Abs(x.Score - this.Score).CompareTo(Math.Abs(y.Score - this.Score)));
                    ParContract optimumDefense = possibleDefense.Last();
                    possibleDefense = possibleDefense.FindAll(x => x.Score == optimumDefense.Score);
                    foreach (ParContract defense in possibleDefense)
                    {
                        if (defense.Higher(optimumDefense))
                        {
                            optimumDefense = defense;
                        }
                    }
                    return optimumDefense;
                }
            }
            return null;
        }
    }
}