创建和加载数据(对象) [英] Create and load data (objects)

查看:54
本文介绍了创建和加载数据(对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计算非常昂贵(每次30分钟).实际上是169个.我只想运行一次并加载它们.

I have a fairly expensive calculation (30 minutes each). Actually 169 of the same. I want to just run them once and load them.

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

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

My plan is to just run the program and have it print out all the new Hand(...) and then I will copy paste it into the program.

是否有更好的方法来解决此问题?

Is there a better way to go about this?

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

I would rather not use a database or external 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);

推荐答案

这不能回答问题,但是在注释中,OP要求使用C#进行快速查找.您可以使用所谓的二加二评估器".这是一个很大的预先计算的查找表,可用于查找给定手的强度值.

This does not answer question, but in comments OP asked for fast lookup evaluator in C#. You can use so called "two plus two evaluator". It's a big precomputed lookup table which you can use to lookup strength value of a given hand.

这是C#中的一些代码.

Here is some code in 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;
    }
}

卡只是将卡从字符串表示形式(黑桃等表示为"As")转换为我们使用的评估程序期望的int表示形式.

Card just converts a card from string representation ("As" for A spades and so on) to int representation expected by evaluator we use.

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文件加载到内存中,并实现查找手部力量所需的简单算法.您可以在此处获得数据文件.

This class loads HandRanks.dat file with huge lookup table into memory and implements simple algorithm necessary to lookup hand strength. You can get dat file 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

这只是基本的实现,但是从这开始,您可以做很多事情(评估翻牌前,翻牌,转牌,比较一手对另一手的范围等方面的赢局变化).

That's just basic implementation but starting from this you can do many things (evaluate change of winning of one hand vs another on preflop, flop, turn, compare hand vs range of other hands and so on).

这篇关于创建和加载数据(对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆