From c7b42c905a9e3c90ff81b8d4dcdc0429ab2e991a Mon Sep 17 00:00:00 2001 From: emkael Date: Mon, 23 May 2016 16:50:45 +0200 Subject: * parsing JFR/DDS custom PBN tags to retrieve par contract and par score --- PBNBoard.cs | 43 ++++++++++++++++++++++++++++++++++++++++ ParScore.cs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Program.cs | 4 ++++ 3 files changed, 113 insertions(+) diff --git a/PBNBoard.cs b/PBNBoard.cs index 3c3030f..cf3ec63 100644 --- a/PBNBoard.cs +++ b/PBNBoard.cs @@ -173,6 +173,41 @@ namespace BCDD 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 ValidateOptimumResultTable(List table) { Regex linePattern = new Regex(@"^([NESW])\s+([CDHSN])T?\s+(\d+)$"); @@ -278,6 +313,14 @@ namespace BCDD } } + 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) diff --git a/ParScore.cs b/ParScore.cs index 2204145..5895823 100644 --- a/ParScore.cs +++ b/ParScore.cs @@ -15,11 +15,77 @@ namespace BCDD class ParScore { private PBNBoard board; + private static Regex pbnContractPattern = new Regex(@"(\d)([CDHSN])(X?)\s+([NESW])"); + private static Regex pbnScorePattern = new Regex(@"(NS|EW)\s+(-?\d})"); + private static Regex jfrContractPattern = new Regex(@"^(\d)([CDHSN])(D?)([NESW])(-?\d+)$"); public ParScore(PBNBoard board) { this.board = board; } + public ParContract GetPBNParContract() + { + String contractField = this.board.GetOptimumResult(); + if ("Pass".Equals(contractField)) + { + return new ParContract(); + } + Match contractMatch = ParScore.pbnContractPattern.Match(contractField); + if (!contractMatch.Success) + { + throw new ParScoreInvalidException("Invalid format for OptimumResult field: " + contractField); + } + String scoreField = this.board.GetOptimumScore(); + Match scoreMatch = ParScore.pbnScorePattern.Match(scoreField); + if (!scoreMatch.Success) + { + throw new ParScoreInvalidException("Invalid format for OptimumScore field: " + scoreField); + } + int score = Int16.Parse(scoreMatch.Groups[2].Value); + if ("EW".Equals(scoreMatch.Groups[1].Value)) + { + score = -score; + } + ParContract contract = new ParContract(Int16.Parse(contractMatch.Groups[1].Value), + contractMatch.Groups[2].Value[0], + contractMatch.Groups[4].Value[0], + "X".Equals(contractMatch.Groups[3].Value), + score); + return contract.Validate(); + } + + public ParContract GetJFRParContract() + { + String parString = this.board.GetMinimax(); + Match parMatch = ParScore.jfrContractPattern.Match(parString); + if (!parMatch.Success) + { + throw new ParScoreInvalidException("Invalid format for Minimax field: " + parString); + } + if ("0".Equals(parMatch.Groups[4].Value)) + { + return new ParContract(); // pass-out + } + ParContract contract = new ParContract(Int16.Parse(parMatch.Groups[1].Value), + parMatch.Groups[2].Value[0], + parMatch.Groups[4].Value[0], + "D".Equals(parMatch.Groups[3].Value), + Int16.Parse(parMatch.Groups[5].Value)); + return contract.Validate(); + } + + public ParContract GetParContract(int[,] ddTable) + { + try + { + return this.GetJFRParContract(); + } + catch (FieldNotFoundException) + { + return this.GetPBNParContract(); + } + } + } } diff --git a/Program.cs b/Program.cs index 3373004..5c7c912 100644 --- a/Program.cs +++ b/Program.cs @@ -37,8 +37,12 @@ namespace BCDD { Console.WriteLine("Board " + boardNo); DDTable.PrintTable(ddTable); + ParScore par = new ParScore(board); + ParContract contract = par.GetParContract(ddTable); + Console.WriteLine(contract); Console.WriteLine(); board.SaveDDTable(ddTable); + board.SaveParContract(contract); file.WriteBoard(board); } else -- cgit v1.2.3