/// <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); } }
/// <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> /// Worker method for a single deal. /// </summary> /// <param name="deal">Deal in BCalc's "NESW" format, with deal number prepended.</param> private void analyzeDeal(String deal) { try { this.form.addStatusLine(deal); BCalcWrapper solver = new BCalcWrapper(deal); foreach (KeyValuePair <int, List <int> > row in this.contracts) { try { solver.setTrump(row.Key); } catch (Exception ex) { this.form.addStatusLine(Form1.GetResourceManager().GetString("Form1_error", Form1.GetCulture()) + ": " + ex.Message); } foreach (int entry in row.Value) { try { BCalcResult result = solver.run(entry); if (!this.abort) { String line = "#" + result.dealNo + ", " + result.declarer + " " + Form1.GetResourceManager().GetString("Accumulator_playsIn", Form1.GetCulture()) + " " + result.trumpSuit + ", " + Form1.GetResourceManager().GetString("Accumulator_tricks", Form1.GetCulture()) + ": " + result.tricks; this.form.addStatusLine(line); this.outputFile.WriteLine(line); this.update(result); this.form.setResult(this.getString()); } } catch (Exception ex) { this.form.addStatusLine(Form1.GetResourceManager().GetString("Form1_error", Form1.GetCulture()) + ": " + ex.Message); } } } solver.destroy(); } catch (Exception ex) { this.outputFile.WriteLine(ex.Message); this.form.addStatusLine(Form1.GetResourceManager().GetString("Form1_error", Form1.GetCulture()) + ": " + ex.Message); } }
/// <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> /// Releases the solver instaces. /// </summary> public void destroy() { BCalcWrapper.bcalcDDS_delete(this.solver); }