From 7e722cf01fa63c968916095aa6b8997da8239c57 Mon Sep 17 00:00:00 2001 From: emkael Date: Mon, 23 May 2016 16:56:31 +0200 Subject: * calculating optimum defense for a contract --- ParContract.cs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/ParContract.cs b/ParContract.cs index 5b77f50..9a6c500 100644 --- a/ParContract.cs +++ b/ParContract.cs @@ -164,5 +164,60 @@ namespace BCDD return score; } + public bool Higher(ParContract obj) + { + return (this.Level > obj.Level + || (this.Level == obj.Level + && Array.IndexOf(BCalcWrapper.DENOMINATIONS, this.Denomination) > Array.IndexOf(BCalcWrapper.DENOMINATIONS, obj.Denomination))); + } + + public ParContract GetDefense(int[,] ddTable, bool vulnerable) + { + if (this.Level != 0) + { + int declarerIndex = Array.IndexOf(BCalcWrapper.PLAYERS, this.Declarer); + List defendersIndexes = new List(); + defendersIndexes.Add((declarerIndex + 1) & 3); + defendersIndexes.Add((declarerIndex + 3) & 3); + List possibleDefense = new List(); + int scoreSquared = this.Score * this.Score; + int denominationIndex = Array.IndexOf(BCalcWrapper.DENOMINATIONS, this.Denomination); + for (int i = 0; i < 5; i++) + { + int level = this.Level; + if (i <= denominationIndex) + { + level++; + } + foreach (int defender in defendersIndexes) + { + if (level + 6 > ddTable[defender, i]) + { + ParContract defense = new ParContract(level, BCalcWrapper.DENOMINATIONS[i], BCalcWrapper.PLAYERS[defender], true, 0); + defense.Score = defense.CalculateScore(ddTable[defender, i], vulnerable); + if (scoreSquared > this.Score * defense.Score) + { + possibleDefense.Add(defense); + } + } + } + } + if (possibleDefense.Count > 0) + { + possibleDefense.Sort((x, y) => Math.Abs(x.Score - this.Score).CompareTo(Math.Abs(y.Score - this.Score))); + ParContract optimumDefense = possibleDefense.Last(); + possibleDefense = possibleDefense.FindAll(x => x.Score == optimumDefense.Score); + foreach (ParContract defense in possibleDefense) + { + if (defense.Higher(optimumDefense)) + { + optimumDefense = defense; + } + } + return optimumDefense; + } + } + return null; + } } } -- cgit v1.2.3