/// <summary> /// Filters the possible moves based and selects one with a weighted random function /// </summary> /// <param name="openingMoves"></param> /// <param name="player"></param> /// <param name="filter"></param> /// <returns></returns> public Move SelectMove(List <OpeningMove> openingMoves, Color player, OpeningBookFilter filter) { List <OpeningMove> validMoves = new List <OpeningMove>(); int total = openingMoves.Sum(x => x.Total); OpeningMove selected = null; if (player == Color.White) { validMoves = openingMoves .Where(x => x.BlackWinPercent <= filter.MaxBlackWinPercent) .Where(x => x.WhiteWinPercent >= filter.MinWhiteWinPercent) .Where(x => x.Total >= filter.MinNumberOfGames) .ToList(); selected = WeightedRandom(validMoves, x => x.WhiteWinPercent * filter.ImportantOfWinPercentage + x.Total / (double)total * filter.ImportanceOfGameCount); } else { validMoves = openingMoves .Where(x => x.WhiteWinPercent <= filter.MaxWhiteWinPercent) .Where(x => x.BlackWinPercent >= filter.MinBlackWinPercent) .Where(x => x.Total >= filter.MinNumberOfGames) .ToList(); selected = WeightedRandom(validMoves, x => x.BlackWinPercent * filter.ImportantOfWinPercentage + x.Total / (double)total * filter.ImportanceOfGameCount); } return(selected.Move); }
public List <OpeningMove> GetAvailableMoves(string currentLine) { var len = currentLine.Length; var a = OpeningLines.BinarySearch(x => x.Substring(0, len).CompareTo(currentLine)); var b = OpeningLines.BinarySearchMax(x => x.Substring(0, len).CompareTo(currentLine)); var lines = new List <string>(); for (int i = a; i <= b; i++) { lines.Add(OpeningLines[i]); } var moves = new Dictionary <Move, OpeningMove>(); foreach (var line in lines) { var result = GameEncoder.DecodeResult(line); var move = GameEncoder.DecodeMove(line, len); OpeningMove m = null; if (moves.ContainsKey(move)) { m = moves[move]; } else { m = new OpeningMove() { Move = move }; moves[move] = m; } m.Total++; switch (result) { case (GameResultsType.BlackWins): m.BlackWins++; break; case (GameResultsType.WhiteWins): m.WhiteWins++; break; case (GameResultsType.Tie): m.Tie++; break; } } return(moves.Select(x => x.Value).ToList()); }