summaryrefslogtreecommitdiff
path: root/src/PBNBoard.cs
blob: 72f87a43b0b5f446bc1ead0becc347292b391145 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace BCDD
{
    class PBNField
    {
        public String Key;
        public String Value;
        public String RawField;

        public PBNField() { }

        public PBNField(String key, String value)
        {
            this.Key = key;
            this.Value = value;
            this.RawField = String.Format("[{0} \"{1}\"]", this.Key, this.Value);
        }

        public PBNField(String rawData)
        {
            this.RawField = rawData;
        }
    }

    class FieldNotFoundException : Exception
    {
        public FieldNotFoundException() : base() { }
        public FieldNotFoundException(String msg) : base(msg) { }
    }

    class PBNBoard
    {
        public List<PBNField> Fields;

        private bool? hasOptimumResultTable = null;
        private bool? hasAbility = null;

        private static Regex linePattern = new Regex(@"\[(.*) ""(.*)""\]");
        private static Regex abilityPattern = new Regex(@"\b([NESW]):([0-9A-D]{5})\b");
        private static Regex optimumResultTablePattern = new Regex(@"^([NESW])\s+([CDHSN])T?\s+(\d+)$");

        public PBNBoard(List<string> lines)
        {
            this.Fields = new List<PBNField>();
            foreach (String line in lines)
            {
                PBNField field = new PBNField();
                field.RawField = line;
                Match lineParse = PBNBoard.linePattern.Match(line);
                if (lineParse.Success)
                {
                    field.Key = lineParse.Groups[1].Value;
                    field.Value = lineParse.Groups[2].Value;
                }
                this.Fields.Add(field);
            }
        }

        public bool HasField(String key)
        {
            foreach (PBNField field in this.Fields)
            {
                if (key.Equals(field.Key))
                {
                    return true;
                }
            }
            return false;
        }

        public String GetField(String key)
        {
            foreach (PBNField field in this.Fields)
            {
                if (key.Equals(field.Key))
                {
                    return field.Value;
                }
            }
            throw new FieldNotFoundException(key + " field not found");
        }

        public void DeleteField(String key)
        {
            List<PBNField> toRemove = new List<PBNField>();
            foreach (PBNField field in this.Fields)
            {
                if (key.Equals(field.Key))
                {
                    toRemove.Add(field);
                }
            }
            foreach (PBNField remove in toRemove)
            {
                this.Fields.Remove(remove);
            }
        }

        public String GetLayout()
        {
            return this.GetField("Deal");
        }

        public String GetNumber()
        {
            return this.GetField("Board");
        }

        public String GetVulnerable()
        {
            return this.GetField("Vulnerable");
        }

        public String GetDealer()
        {
            return this.GetField("Dealer");
        }

        public MatchCollection ValidateAbility(String ability)
        {
            MatchCollection matches = PBNBoard.abilityPattern.Matches(ability);
            if (matches.Count != 4)
            {
                this.hasAbility = false;
                throw new DDTableInvalidException("Invalid Ability line: " + ability);
            }
            List<String> players = new List<String>();
            foreach (Match match in matches)
            {
                if (players.Contains(match.Groups[1].Value))
                {
                    this.hasAbility = false;
                    throw new DDTableInvalidException("Duplicate entry in Ability: " + match.Groups[0].Value);
                }
                else
                {
                    players.Add(match.Groups[1].Value);
                }
            }
            this.hasAbility = true;
            return matches;
        }

        public String GetAbility()
        {
            return this.GetField("Ability");
        }

        public void DeleteAbility()
        {
            this.DeleteField("Ability");
        }

        public void WriteAbility(int[,] ddTable)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 4; i++)
            {
                sb.Append(BCalcWrapper.PLAYERS[i]);
                sb.Append(':');
                for (int j = 4; j >= 0; j--)
                {
                    sb.Append((char)(ddTable[i, j] > 9 ? 'A' + ddTable[i, j] - 10 : ddTable[i, j] + '0'));
                }
                if (i < 3)
                {
                    sb.Append(' ');
                }
            }
            String abilityStr = sb.ToString();
            this.Fields.Add(new PBNField("Ability", abilityStr));
        }

        public String GetMinimax()
        {
            return this.GetField("Minimax");
        }

        public void DeleteMinimax()
        {
            this.DeleteField("Minimax");
        }

        public void WriteMinimax(ParContract contract)
        {
            this.Fields.Add(new PBNField("Minimax", String.Format("{0}{1}{2}{3}{4}", contract.Level, contract.Denomination, contract.Doubled ? "D" : "", contract.Declarer, contract.Score)));
        }

        public String GetOptimumScore()
        {
            return this.GetField("OptimumScore");
        }

        public void DeleteOptimumScore()
        {
            this.DeleteField("OptimumScore");
        }

        public void WriteOptimumScore(ParContract contract)
        {
            this.Fields.Add(new PBNField("OptimumScore", String.Format("NS {0}", contract.Score)));
        }

        public String GetOptimumResult()
        {
            return this.GetField("OptimumResult");
        }

        public List<Match> ValidateOptimumResultTable(List<String> table)
        {
            List<Match> matches = new List<Match>();
            List<String> duplicates = new List<String>();
            foreach (String line in table)
            {
                Match match = PBNBoard.optimumResultTablePattern.Match(line);
                if (!match.Success)
                {
                    this.hasOptimumResultTable = false;
                    throw new DDTableInvalidException("Invalid OptimumResultTable line: " + line);
                }
                String position = match.Groups[1].Value + " - " + match.Groups[2].Value;
                if (duplicates.Contains(position))
                {
                    this.hasOptimumResultTable = false;
                    throw new DDTableInvalidException("Duplicate OptimumResultTable line: " + line);
                }
                else
                {
                    duplicates.Add(position);
                }
                matches.Add(match);
            }
            this.hasOptimumResultTable = true;
            return matches;
        }

        public List<String> GetOptimumResultTable()
        {
            bool fieldFound = false;
            List<String> result = new List<String>();
            foreach (PBNField field in this.Fields)
            {
                if ("OptimumResultTable".Equals(field.Key))
                {
                    fieldFound = true;
                }
                else
                {
                    if (fieldFound)
                    {
                        if (field.Key == null)
                        {
                            result.Add(field.RawField);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            if (!fieldFound)
            {
                this.hasOptimumResultTable = false;
                throw new FieldNotFoundException("OptimumResultTable field not found");
            }
            return result;
        }

        public void DeleteOptimumResultTable()
        {
            bool fieldFound = false;
            List<PBNField> toRemove = new List<PBNField>();
            foreach (PBNField field in this.Fields)
            {
                if ("OptimumResultTable".Equals(field.Key))
                {
                    fieldFound = true;
                    toRemove.Add(field);
                }
                else
                {
                    if (fieldFound)
                    {
                        if (field.Key == null)
                        {
                            toRemove.Add(field);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            foreach (PBNField remove in toRemove)
            {
                this.Fields.Remove(remove);
            }
        }

        public void WriteOptimumResultTable(int[,] ddTable)
        {
            this.Fields.Add(new PBNField("OptimumResultTable", @"Declarer;Denomination\2R;Result\2R"));
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    this.Fields.Add(new PBNField(String.Format("{0} {1}{2} {3}", BCalcWrapper.PLAYERS[i], BCalcWrapper.DENOMINATIONS[j], (BCalcWrapper.DENOMINATIONS[j] == 'N') ? "T" : "", ddTable[i, j])));
                }
            }
        }

        public void SaveParContract(ParContract contract)
        {
            this.DeleteOptimumScore();
            this.WriteOptimumScore(contract); // we're not writing DDS custom fields, just parse them
            this.DeleteMinimax();
            this.WriteMinimax(contract);
        }

        public void SaveDDTable(int[,] ddTable)
        {
            if (this.hasOptimumResultTable == null)
            {
                try
                {
                    List<Match> optimumResultTable = this.ValidateOptimumResultTable(this.GetOptimumResultTable());
                    this.hasOptimumResultTable = true;
                }
                catch (FieldNotFoundException)
                {
                    this.hasOptimumResultTable = false;
                }
            }
            if (this.hasOptimumResultTable == false)
            {
                this.DeleteOptimumResultTable();
                this.WriteOptimumResultTable(ddTable);
            }
            if (this.hasAbility == null)
            {
                try
                {
                    MatchCollection ability = this.ValidateAbility(this.GetAbility());
                    this.hasAbility = true;
                }
                catch (FieldNotFoundException)
                {
                    this.hasAbility = false;
                }
            }
            if (this.hasAbility == false)
            {
                this.DeleteAbility();
                this.WriteAbility(ddTable);
            }
        }
    }
}