dozingcat / CardsWithCats

Hearts and Spades card games
GNU General Public License v3.0
19 stars 2 forks source link

Issue with spades bid #17

Closed gsiems closed 1 year ago

gsiems commented 1 year ago

In the _estimatedTricksForSpades function of the spades_ai.dart file it appears that the calculated bid is too low at times.

I believe that adding the line previousTopRankIndex = ranks[i].index to the end of the numRanksToCheck loop fixes the issue:

double _estimatedTricksForSpades(List<Rank> ranks) {
  if (ranks.isEmpty) {
    return 0;
  }
  // Look at gaps for first 3 spades, assume 4th spade and beyond are good.
  int gaps = 0;
  int numRanksToCheck = min(3, ranks.length);
  int previousTopRankIndex = Rank.ace.index + 1;
  double gapPenalty = 0;
  for (int i = 0; i < numRanksToCheck; i++) {
    gaps += previousTopRankIndex - ranks[i].index - 1;
    gapPenalty += (gaps == 0)
        ? 0
        : (gaps == 1)
            ? 0.4
            : (gaps == 2)
                ? 0.8
                : 1;
    previousTopRankIndex = ranks[i].index;
  }
  return ranks.length - gapPenalty;
}

Update added missing semi-colon

dozingcat commented 1 year ago

Good find, thanks! Fixed in #18 and will be in the next release.