summaryrefslogtreecommitdiff
path: root/Analizator9000/Analizator9000/BCalcWrapper.cs
blob: 6c9df76c5b6b00548abf8e98cd8ed65443db8134 (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
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace Analizator9000
{
    /// <summary>
    /// Structure holding single deal analysis result.
    /// </summary>
    public struct BCalcResult
    {
        /// <summary>
        /// Trump denomination (N/S/H/D/C).
        /// </summary>
        public char trumpSuit;
        /// <summary>
        /// Declaring player (N/E/S/W).
        /// </summary>
        public char declarer;
        /// <summary>
        /// Number of tricks taken by the declaring side.
        /// </summary>
        public int tricks;
        /// <summary>
        /// Deal number, parsed out from input string.
        /// </summary>
        public long dealNo;
        /// <summary>
        /// Constructor for result structure.
        /// </summary>
        /// <param name="a">Trump denomination (N/S/H/D/C).</param>
        /// <param name="b">Declaring player (N/E/S/W).</param>
        /// <param name="c">Number of tricks taken by the declaring side.</param>
        /// <param name="d">Deal number, parsed out from input string.</param>
        public BCalcResult(char a, char b, int c, long d)
        {
            trumpSuit = a;
            declarer = b;
            tricks = c;
            dealNo = d;
        }
    };
    /// <summary>
    /// Wrapper class for libbcalcDDS.ddl.
    /// </summary>
    class BCalcWrapper
    {
        /// <summary>
        /// Allocates new instance of BCalc solver.
        /// </summary>
        /// <param name="format">Deal format string. See the original documentation for details.</param>
        /// <param name="hands">Card distribution.</param>
        /// <param name="trump">Trump denomination, in numeric format. See the original documentation for details.</param>
        /// <param name="leader">Player on lead, in numeric format. See the original documentation for details.</param>
        /// <returns>Pointer to solver instance.</returns>
        /// <remarks>Original documentation: http://bcalc.w8.pl/API_C/bcalcdds_8h.html#ab636045f65412652246b769e8e95ed6f</remarks>
        [DllImport(@"bin\libbcalcdds.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr bcalcDDS_new(IntPtr format, IntPtr hands, Int32 trump, Int32 leader);

        /// <summary>
        /// Conducts DD analysis.
        /// </summary>
        /// <param name="solver">Pointer to solver instance.</param>
        /// <returns>Number of tricks to take by leading side.</returns>
        /// <remarks>Original documentation: http://bcalc.w8.pl/API_C/bcalcdds_8h.html#a369ce661d027bef3f717967e42bf8b33</remarks>
        [DllImport(@"bin\libbcalcdds.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern Int32 bcalcDDS_getTricksToTake(IntPtr solver);

        /// <summary>
        /// Checks for solver errors.
        /// </summary>
        /// <param name="solver">Pointer to solver instance.</param>
        /// <returns>Error string or NULL if no error accured.</returns>
        /// <remarks>Original documentation: http://bcalc.w8.pl/API_C/bcalcdds_8h.html#a89cdec200cde91331d40f0900dc0fb46</remarks>
        [DllImport(@"bin\libbcalcdds.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr bcalcDDS_getLastError(IntPtr solver);

        /// <summary>
        /// Frees allocated solver instance and cleans up after it.
        /// </summary>
        /// <param name="solver">Pointer to solver instance.</param>
        /// <remarks>Original documentation: http://bcalc.w8.pl/API_C/bcalcdds_8h.html#a4a68da83bc7da4663e2257429539912d</remarks>
        [DllImport(@"bin\libbcalcdds.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void bcalcDDS_delete(IntPtr solver);

        /// <summary>
        /// Sets trump denomination and resets the analysis.
        /// </summary>
        /// <param name="solver">Pointer to solver instance.</param>
        /// <param name="trump">Trump denomination, in numeric format. See the original documentation for details.</param>
        /// <remarks>Original documentation: http://bcalc.w8.pl/API_C/bcalcdds_8h.html#a88fba3432e66efa5979bbc9e1f044164</remarks>
        [DllImport(@"bin\libbcalcdds.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void bcalcDDS_setTrumpAndReset(IntPtr solver, Int32 trump);

        /// <summary>
        /// Sets leading player and resets the analysis.
        /// </summary>
        /// <param name="solver">Pointer to solver instance.</param>
        /// <param name="player">Player on lead, in numeric format. See the original documentation for details.</param>
        /// <remarks>Original documentation: http://bcalc.w8.pl/API_C/bcalcdds_8h.html#a616031c1e1d856c4aac14390693adb4c</remarks>
        [DllImport(@"bin\libbcalcdds.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void bcalcDDS_setPlayerOnLeadAndReset(IntPtr solver, Int32 player);

        /// <summary>
        /// Translation table between player characters and numeric values.
        /// </summary>
        public static String table = "NESW";
        /// <summary>
        /// Translation table between denomination characters and numeric values.
        /// </summary>
        public static String denominations = "CDHSN";

        /// <summary>
        /// Pointer to solver instance.
        /// </summary>
        private IntPtr solver;
        /// <summary>
        /// Deal distribution.
        /// </summary>
        private String deal;
        /// <summary>
        /// Internal number of deal being analyzed.
        /// </summary>
        private long dealNo;
        /// <summary>
        /// Trump suit, in numeric format.
        /// </summary>
        private int trumps;
        
        /// <summary>
        /// Constructor of class instance for single deal analysis.
        /// </summary>
        /// <param name="deal">Deal distribution prefixed with deal number.</param>
        public BCalcWrapper(String deal)
        {
            try
            {
                Match dealMatch = Regex.Match(deal, @"^(\d+): (.*)$");
                if (!dealMatch.Success) {
                    throw new Exception();
                }
                this.deal = dealMatch.Groups[2].Value;
                this.dealNo = Convert.ToInt64(dealMatch.Groups[1].Value);
            }
            catch (Exception)
            {
                throw new Exception(Form1.GetResourceManager().GetString("BCalcWrapper_dealLoadError", Form1.GetCulture()) + ": " + deal);
            }
            this.solver = BCalcWrapper.bcalcDDS_new(Marshal.StringToHGlobalAnsi(BCalcWrapper.table), Marshal.StringToHGlobalAnsi(this.deal), 0, 0);
            this.errorCheck();
        }

        /// <summary>
        /// Sets the trump denomination.
        /// </summary>
        /// <param name="trumpSuit">Trump denomination, in numeric format.</param>
        public void setTrump(int trumpSuit)
        {
            if (trumpSuit < 0)
            {
                throw new Exception(Form1.GetResourceManager().GetString("BCalcWrapper_trumpError", Form1.GetCulture()) + ": " + trumpSuit);
            }
            BCalcWrapper.bcalcDDS_setTrumpAndReset(solver, trumpSuit);
            this.errorCheck();
            this.trumps = trumpSuit;
        }

        /// <summary>
        /// Runs the single contract analysis.
        /// </summary>
        /// <param name="declarer">Declaring player, in numeric format.</param>
        /// <returns>Result structur for the contract.</returns>
        //TODO substitute from actual number of tricks to analyze, rather than 13, maybe.
        public BCalcResult run(int declarer)
        {
            if (declarer < 0)
            {
                throw new Exception(Form1.GetResourceManager().GetString("BCalcWrapper_declarerError", Form1.GetCulture()) + ": " + declarer);
            }
            int l = (declarer + 1) % 4;
            BCalcWrapper.bcalcDDS_setPlayerOnLeadAndReset(solver, l);
            this.errorCheck();
            int result = BCalcWrapper.bcalcDDS_getTricksToTake(this.solver);
            this.errorCheck();
            return new BCalcResult(BCalcWrapper.denominations[this.trumps], BCalcWrapper.table[declarer], 13 - result, this.dealNo);
        }

        /// <summary>
        /// Releases the solver instaces.
        /// </summary>
        public void destroy() {
            BCalcWrapper.bcalcDDS_delete(this.solver);
        }

        /// <summary>
        /// Checks for errors, throws exception if any occured.
        /// </summary>
        private void errorCheck()
        {
            IntPtr error = BCalcWrapper.bcalcDDS_getLastError(this.solver);
            String eStr = Marshal.PtrToStringAnsi(error);
            if (eStr != null)
            {
                throw new Exception(eStr);
            }
        }

    }
}