From dfe4c3b00198172c0f4bee6b26ce3962c69e4ccd Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 3 Jun 2016 14:02:45 +0200 Subject: * lowest from equal contracts as par contract --- src/ParScore.cs | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ParScore.cs b/src/ParScore.cs index 5032ea1..068be90 100644 --- a/src/ParScore.cs +++ b/src/ParScore.cs @@ -123,11 +123,13 @@ namespace BCDD return nsPlaying ? nsHighest.Validate() : ewHighest.Validate(); } ParContract highest = nsHighest.Higher(ewHighest) ? nsHighest : ewHighest; + ParContract otherSideHighest = nsHighest.Higher(ewHighest) ? ewHighest : nsHighest; nsPlaying = ('N'.Equals(highest.Declarer) || 'S'.Equals(highest.Declarer)); bool defenseVulnerability = this.determineVulnerability(vulnerability, nsPlaying ? 'E' : 'N'); ParContract highestDefense = highest.GetDefense(ddTable, defenseVulnerability); if (highestDefense != null) { + // Highest contract has profitable defense return highestDefense.Validate(); } int denominationIndex = Array.IndexOf(BCalcWrapper.DENOMINATIONS, highest.Denomination); @@ -151,13 +153,36 @@ namespace BCDD { ParContract contract = new ParContract(level, BCalcWrapper.DENOMINATIONS[i], BCalcWrapper.PLAYERS[player], false, 0); contract.Score = contract.CalculateScore(ddTable[player, i], vulnerable); - if (scoreSquared < contract.Score * highest.Score) + if (otherSideHighest.Higher(contract)) { - possibleOptimums.Add(contract.GetDefense(ddTable, defenseVulnerability) ?? contract); + // Contract is lower than other side's contract + break; } - else + if (highest.Score * contract.Score > 0) { - break; + // Contract makes + if (Math.Abs(contract.Score) >= Math.Abs(highest.Score)) + { + // Contract is profitable + ParContract defense = contract.GetDefense(ddTable, defenseVulnerability); + if (defense != null && (contract.Score * contract.Score > contract.Score * defense.Score)) + { + // Contract has defense + possibleOptimums.Add(defense); + // So lower contracts will too. + break; + } + else + { + // Contract does not have defense + possibleOptimums.Add(contract); + } + } + else + { + // Contract is not profitable + break; + } } level--; } @@ -165,10 +190,22 @@ namespace BCDD } foreach (ParContract contract in possibleOptimums) { - if (Math.Abs(contract.Score) > Math.Abs(highest.Score) || (contract.Score == highest.Score && contract.Higher(highest))) + if ((Math.Abs(contract.Score) > Math.Abs(highest.Score))) { + // Contract is more profitable highest = contract; } + else + { + if (contract.Score == highest.Score) + { + if (highest.Higher(contract)) + { + // Equally profitable, but lower + highest = contract; + } + } + } } return highest.Validate(); } -- cgit v1.2.3