diff options
author | emkael <emkael@tlen.pl> | 2016-05-23 16:56:31 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-05-23 16:56:31 +0200 |
commit | 7e722cf01fa63c968916095aa6b8997da8239c57 (patch) | |
tree | 63d82d0a1c107463874208407191ec98612fae60 | |
parent | 9b9a129f59bd76bab9314da38119facc0178a8f9 (diff) |
* calculating optimum defense for a contract
-rw-r--r-- | ParContract.cs | 55 |
1 files changed, 55 insertions, 0 deletions
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<int> defendersIndexes = new List<int>();
+ defendersIndexes.Add((declarerIndex + 1) & 3);
+ defendersIndexes.Add((declarerIndex + 3) & 3);
+ List<ParContract> possibleDefense = new List<ParContract>();
+ 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;
+ }
}
}
|