添加随机数以配对列表以进行比较

时间:2016-12-12 03:10:46

标签: c#

想知道我是否可以在这里得到一些帮助。我有一些代码可以创建2对数字。我将它们与.except进行比较。我的问题是如何分配我的代码中显示的数字,而不是如何将50个随机抛出添加到对中进行比较。

    public FrmDieRoller()
    {
        InitializeComponent();
    }

    public class Pair
    {
        public int FirstDie { get; set; }
        public int SecondDie { get; set; }
    }

    public class Pair2
    {
        public int ThirdDie { get; set; }
        public int FourthDie { get; set; }

    }

    public static List<Pair> ConvertToPairs(int[,] dieValues)
    {
        var query = from int item in dieValues select item;
        var p1Rolls = query.ToList();
        List<Pair> pairs = new List<Pair>(p1Rolls.Count);
        for (int i = 0; i <4  ; i += 2)
        {
            pairs.Add(new Pair() {FirstDie = p1Rolls[i], SecondDie =
            p1Rolls[i+ 1]});
        }
        return pairs;
    }

    public static List<Pair2> ConvertToPairs2(int[,] dieValues1)
    {
        var query = from int item1 in dieValues1 select item1;
        var p2Rolls = query.ToList();
        List<Pair2> pairs1 = new List<Pair2>(p2Rolls.Count);
        for (int j = 0; j < 4; j += 2)
        {
            pairs1.Add(new Pair2() {ThirdDie = p2Rolls[j], FourthDie = 
            p2Rolls[j + 1]});
        }
        return pairs1;
    }

    public void btnRoll_Click(object sender, EventArgs e)
    {   

        int[,] dieValue1 = {{1,2},{3,4}};
        int[,] dieValue2 = {{2,6},{5,1}};



        var p1Rolls = ConvertToPairs(dieValue1);
        var p2Rolls = ConvertToPairs(dieValue2);

        foreach (var item in p1Rolls.Except(p2Rolls))
        {

        lstRollDifference.Items.Add(string.Format("Player one has [{0},

        {1}] which Player two does not", item.FirstDie, item.SecondDie));


        }

2 个答案:

答案 0 :(得分:0)

这就是我要创建随机卷的方法:

private Random _random = new Random();

private int RollDie()
{
    return _random.Next(1, 7); // 7 is the exclusive max value - so real max is 6
}

private Pair RollPair()
{
    return new Pair()
    {
        FirstDie = this.RollDie(),
        SecondDie = this.RollDie()
    };
}

public void btnRoll_Click(object sender, EventArgs e)
{
    List<Pair> p1Rolls = Enumerable.Range(0, 50).Select(n => this.RollPair()).ToList();
    List<Pair> p2Rolls = Enumerable.Range(0, 50).Select(n => this.RollPair()).ToList();

    foreach (var item in p1Rolls.Except(p2Rolls))
    {
        lstRollDifference.Items.Add(string.Format("Player one has [{0}, {1}] which Player two does not", item.FirstDie, item.SecondDie));
    }
}

答案 1 :(得分:0)

您可以使用Random类生成卷,最简单的方法是将该代码合并到Pair类中,如下所示:

class Pair : IEquatable<Pair>
{
    public int FirstDie { get; set; }
    public int SecondDie { get; set; }

    public Pair(int d1, int d2)
    {
        FirstDie = d1;
        SecondDie = d2;
    }

    public bool Equals(Pair other)
    {
        return this.FirstDie.Equals(other.FirstDie) 
            && this.SecondDie.Equals(other.SecondDie);
    }

    public override int GetHashCode()
    {
        return FirstDie.GetHashCode() 
                ^ SecondDie.GetHashCode();
    }

    private static Random r = new Random();
    public static Pair RollNewPair()
    {
        // The minimum value is inclusive, the maximum value is exclusive
        return new Pair(r.Next(1, 7), r.Next(1, 7));
    }
}

(就像Enigmativity所说,如果没有实施Except并覆盖IEquatable<Pair>GetHashCode方法将无法正常工作。)

然后像这样使用它:

int numOfRolls = 20;

List<Pair> playerOneRolls = Enumerable.Range(0, numOfRolls).Select(n => Pair.RollNewPair()).ToList();
List<Pair> playerTwoRolls = Enumerable.Range(0, numOfRolls).Select(n => Pair.RollNewPair()).ToList();

foreach (var roll in playerOneRolls.Except(playerTwoRolls))
{
    // do whatever
}