private static Tuple <List <SubstitutionGroup>, List <Substitution> > BackgroundHandlerForOneTileAway(WindPivot dominantWind, WindPivot seatWind, List <TilePivot> handTileWithLast, List <TilePivot> availableTiles, List <TilePivot> forbiddenTiles) { var substitutionsAttemps = new List <Substitution>(); var rawResults = new List <SubstitutionGroup>(); foreach (TilePivot subbedTile in handTileWithLast.Distinct()) { foreach (TilePivot subTile in availableTiles.Distinct()) { if (subbedTile.Equals(subTile) || forbiddenTiles.Contains(subTile)) { continue; } substitutionsAttemps.Add(new Substitution(subbedTile, subTile, availableTiles)); List <TilePivot> handTilesWithSub = new List <TilePivot>(handTileWithLast); handTilesWithSub.Remove(subbedTile); var hand1 = new FullHandPivot(handTilesWithSub, dominantWind, seatWind, subTile); HandYakuListPivot tmpResults = hand1.ComputeHandYakus()?.FirstOrDefault(); if (tmpResults != null) { var subSeq = new SubstitutionSequence(); subSeq.AddSubstitution(new Substitution(subbedTile, subTile, availableTiles)); var finded = rawResults.FirstOrDefault(x => x.Yakus.Equals(tmpResults)); if (finded == null) { rawResults.Add(new SubstitutionGroup(tmpResults)); finded = rawResults.Last(); } finded.AddSubstitutionSequence(subSeq); } } } return(new Tuple <List <SubstitutionGroup>, List <Substitution> >(rawResults, substitutionsAttemps)); }
internal SubstitutionGroup(HandYakuListPivot handYakuListPivot) { _substitutionSequences = new List <SubstitutionSequence>(); Yakus = handYakuListPivot; }
private static void AsyncCompute(object sender, DoWorkEventArgs e) { var arguments = e.Argument as object[]; var handTiles = (List <TilePivot>)arguments[0]; var dominantWind = (WindPivot)arguments[1]; var seatWind = (WindPivot)arguments[2]; var latestTile = (TilePivot)arguments[3]; var draw = (DrawPivot)arguments[4]; Dictionary <int, List <SubstitutionGroup> > subsGroupByAwayIndex = new Dictionary <int, List <SubstitutionGroup> >(); // compute tile0 FullHandPivot hand0 = new FullHandPivot(handTiles, dominantWind, seatWind, latestTile); HandYakuListPivot handYakus = hand0.ComputeHandYakus()?.FirstOrDefault(); subsGroupByAwayIndex.Add(0, new List <SubstitutionGroup> { new SubstitutionGroup(handYakus) }); #region 1 tile away var handTileWithLast = new List <TilePivot>(handTiles) { latestTile }; List <TilePivot> availableTiles = draw.ComputeRemainingTiles(handTileWithLast); // gets yakus for each substitution combination var rawResultsArray = BackgroundHandlerForOneTileAway(dominantWind, seatWind, handTileWithLast, availableTiles, new List <TilePivot>()); var rawResults = rawResultsArray.Item1; subsGroupByAwayIndex.Add(1, rawResults.OrderByDescending(x => x.Probability).ToList()); // per discard solutions var discardprobabilities = new Dictionary <TilePivot, double>(); /* * TilePivot currentDiscard = null; * double probability = 0; * foreach (var r in rawResults.OrderBy(x => x.SubstitutionSequences.First().Substitutions.First().Subbed)) * { * if (currentDiscard != null && !r.Item1.Subbed.Equals(currentDiscard)) * { * discardprobabilities.Add(currentDiscard, Math.Round(probability * 100, 3)); * probability = 0; * } * currentDiscard = r.Item1.Subbed; * probability += availableTiles.Count(_ => _.Equals(currentDiscard)) / (double)availableTiles.Count; * } * if (currentDiscard != null) * { * discardprobabilities.Add(currentDiscard, Math.Round(probability * 100, 3)); * }*/ #endregion 1 tile away #region 2 tiles away // gets yakus for each substitution combination var rawResults2Away = new List <SubstitutionGroup>(); var firstRoundSubstitutionsList = rawResultsArray.Item2; foreach (var firstSubstitution in firstRoundSubstitutionsList) { var usedTiles = new List <TilePivot>(handTiles) { latestTile }; usedTiles.Add(firstSubstitution.Subber); var availableTilesAway2 = draw.ComputeRemainingTiles(usedTiles); usedTiles.Remove(firstSubstitution.Subbed); rawResultsArray = BackgroundHandlerForOneTileAway(dominantWind, seatWind, usedTiles, availableTilesAway2, new List <TilePivot> { firstSubstitution.Subbed }); var subGroupList = rawResultsArray.Item1; foreach (var subGroup in rawResultsArray.Item1) { subGroup.AddSubstitutionToEachSequences(firstSubstitution); rawResults2Away.Add(subGroup); } } var resultsAway2WithoutDuplicates = new List <SubstitutionGroup>(); foreach (var r in rawResults2Away) { var match = resultsAway2WithoutDuplicates.FirstOrDefault(ra => ra.Yakus.Equals(r.Yakus)); if (match == null) { resultsAway2WithoutDuplicates.Add(r); } else { foreach (var seq in r.SubstitutionSequences) { match.AddSubstitutionSequence(seq); } } } subsGroupByAwayIndex.Add(2, resultsAway2WithoutDuplicates.OrderByDescending(x => x.Probability).ToList()); #endregion 2 tiles away e.Result = new object[] { subsGroupByAwayIndex, discardprobabilities }; }