创建和加载数据(对象)

时间:2016-11-28 17:07:30

标签: c# .net

我有一个相当昂贵的计算(每个30分钟)。实际上169相同。我想只运行一次并加载它们。

private List<Hands> Hands = new List<Hand>() {new Hand(...), new Hand(...)};

我的计划是运行程序并打印出所有新的Hand(...),然后我将其复制粘贴到程序中。

还有更好的方法吗?

我宁愿不使用数据库或外部XML。

 Debug.WriteLine("HoleCards holeCards = new HoleCards(new Card({0}), new Card({1}), {2}, {3}, {4}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20});",
                            holecards.Card1.CardInt, holecards.Card2.CardInt, holecards.Wins, holecards.Loses, holecards.Ties
                          , holecards.StrFlush, holecards.Quads, holecards.Boat, holecards.Flush, holecards.Straight, holecards.Trips, holecards.TwoPair, holecards.OnePair, holecards.High
                          , holecards.StrFlushLose, holecards.QuadsLose, holecards.BoatLose, holecards.FlushLose, holecards.StraightLose, holecards.TripsLose, holecards.TwoPairLose, holecards.OnePairLose, holecards.HighLose);

1 个答案:

答案 0 :(得分:1)

这不回答问题,但在评论中OP要求C#中的快速查找评估程序。您可以使用所谓的&#34;两加二评估者&#34;。它是一个很大的预先计算的查找表,可用于查找给定手的强度值。

以下是C#中的一些代码。

public static class Card {
    private static readonly Dictionary<char, int> _suites = new Dictionary<char, int> {
        {'c', 0}, {'d', 1}, {'h', 2}, {'s', 3}
    };

    private static readonly Dictionary<char, int> _ranks = new Dictionary<char, int> {
        {'2', 1}, {'3', 2}, {'4', 3}, {'5', 4}, {'6', 5}, {'7', 6}, {'8', 7}, {'9', 8}, {'T', 9}, {'J', 10}, {'Q', 11}, {'K', 12}, {'A', 13}
    };

    public static Dictionary<char, int> Ranks => _ranks;

    public static int FromString(string c) {
        if (c.Length != 2) throw new ArgumentException("c");
        char rank = Char.ToUpperInvariant(c[0]);
        char suit = Char.ToLowerInvariant(c[1]);
        return (_ranks[rank] - 1)*4 + _suites[suit] + 1;
    }
}

Card只是将一张卡片从字符串表示(&#34; As&#34; for a spades等)转换为我们使用的评估者所期望的int表示。

public class RankEvaluator
{
    readonly string[] _handTypes = new[] {
        "invalid hand",
        "high card",
        "one pair",
        "two pairs",
        "three of a kind",
        "straight",
        "flush",
        "full house",
        "four of a kind",
        "straight flush"
    };
    private RankEvaluator()
    {

    }
    private int[] _lut;
    private void Init()
    {
        _lut = InitIntl();
    }

    private static readonly Lazy<RankEvaluator> _instance = new Lazy<RankEvaluator>(() => {
        var r = new RankEvaluator();
        r.Init();
        return r;
    }, true);

    public static RankEvaluator Instance => _instance.Value;

    private int[] InitIntl()
    {
        var lut = new int[32487834];
        if (!File.Exists("HandRanks.dat")) throw new InvalidOperationException("HandRanks.dat not found");
        using (var reader = new BinaryReader(File.Open("HandRanks.dat", FileMode.Open, FileAccess.Read, FileShare.Read)))
        {
            var tempBuffer = reader.ReadBytes(32487834 * 4);
            Buffer.BlockCopy(tempBuffer, 0, lut, 0, 32487834 * 4);
        }
        return lut;
    }

    public int LookupHand(params int[] cards)
    {
        unchecked
        {
            int p = _lut[53 + cards[0]];
            p = _lut[p + cards[1]];
            p = _lut[p + cards[2]];
            p = _lut[p + cards[3]];
            p = _lut[p + cards[4]];
            p = _lut[p + cards[5]];
            return _lut[p + cards[6]];
        }
    }

    public string GetType(int value) {
        return _handTypes[value >> 12];
    }

    public int GetRank(int value) {
        return value & 0x00000fff;
    }
}   

此类将带有大量查找表的HandRanks.dat文件加载到内存中,并实现查找手部强度所需的简单算法。您可以获取dat文件here

像这样使用:

var hand1 = "As Ah";
var hand2 = "2s 2c";
var board = "5s 2d 8h 8s Ks";
var fullHand1 = (hand1 + " " + board).Split(' ').Select(Card.FromString).ToArray();
var fullHand2 = (hand2 + " " + board).Split(' ').Select(Card.FromString).ToArray();
var r1 = RankEvaluator.Instance.LookupHand(fullHand1);
var r2 = RankEvaluator.Instance.LookupHand(fullHand2);
var type1 = RankEvaluator.Instance.GetType(r1); // two pairs
var rank1 = RankEvaluator.Instance.GetRank(r1);
var type2 = RankEvaluator.Instance.GetType(r2); // full house
var rank2 = RankEvaluator.Instance.GetRank(r2);
Debug.Assert(r2 > r1); // full house wins

这只是基本的实现,但是从这开始你可以做很多事情(在翻牌前,翻牌,转牌,比较手与其他牌的范围之间评估单手与另一手的获胜变化等等)。< / p>