使用LINQ在C#中主谋得分算法 [英] MasterMind scoring algorithm in C# using LINQ

查看:163
本文介绍了使用LINQ在C#中主谋得分算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种优雅的方式来计算,在C#中的主谋游戏猜测的成绩,最好使用LINQ。



在策划者的codemaker产生使用数字1到6,一个数字4位数的密码,可以使用一次以上。作为一个例子,这个秘密代码是:



  INT [] =秘{1,2,3,1}; 



密码破译者试图通过提出一个猜测破解密码。在这个例子中,猜测是:



  INT [] =猜测{1,1,2,2}; 



(代码和猜测现在存储在一个数组,但其他集合类型都还好过)。



codemaker然后在分数这个猜测,宣布黑人和白人的数量。黑色被授予从中是价值和位置正确猜测每个数字。白色被授予放错位置的每个正确的数字。在这个例子中,得分是(在位置1为1)1黑和2白人(为在2位的1和2和3)。



回到问题:我在寻找一个优雅的方式来计算的猜测C#中的成绩,最好使用LINQ。到目前为止,我想出了一个计算黑人数的语句:

  INT黑人= INT新[] { 0,1,2,3} .Count之间(ⅰ= GT;(猜[Ⅰ] ==秘密[I])); 



我要沿着白人的数量是匹配的总数线路进行(3 )减去黑人的数量。所以我尝试:

  INT白人= guess.Intersect(秘密).Count之间的() - 黑色; 



但是,唉,IEnumerable.Intersect()产生{1,2},而不是{1,1 ,2},因为它只着眼于不同的数字。因此,计算白人= 1,而不是2。



我不能想出计算白人,从除使用C样式嵌套循环的另一种方式。你可以吗?最好使用LINQ - 我喜欢的算法可以在代码中使用LINQ来表达的方式。执行速度是不是一个真正的问题。


解决方案

  VAR黑色=猜测
.ZIP(秘密,(G,S)=>克== S)
.Count之间(Z => Z);

变种白=猜测
.Intersect(秘密)
.SUM(C =>
System.Math.Min(
secret.Count( X => X = = C),
guess.Count(X => X == C))) - 黑色;



假设:

  INT [] =秘{1,2,3,1}; 
INT []猜测= {1,1,2,2};



然后:

 黑== 1安培;&安培;白== 2 


I'm looking for an elegant way to compute the score of a guess in the MasterMind game in C#, preferably using LINQ.

In MasterMind, the codemaker generates a secret code of 4 digits using the digits 1 through 6. A digit may be used more than once. As an example, the secret code is:

int[] secret = { 1, 2, 3, 1 };

The codebreaker tries to break the secret code by presenting a guess. In this example, the guess is:

int[] guess  = { 1, 1, 2, 2 };

(Both code and guess are now stored in an array, but other collection types are okay too).

The codemaker then "scores" this guess by announcing the number of "blacks" and "whites". A black is awarded for each digit from the guess which is correct in both value and position. A white is awarded for each correct digit placed in the wrong position. In this example, the score is 1 black (for the "1" in position 1) and 2 whites (for the "1" and "2" in positions 2 and 3).

Back to the question: I'm looking for an elegant way to compute the score of a guess in C#, preferably using LINQ. So far, I've come up with a statement that computes the number of blacks:

int blacks = new int[] { 0, 1, 2, 3 }.Count(i => (guess[i] == secret[i]));

I was going to proceed along the lines that the number of whites is the total number of matches (3) minus the number of blacks. So I tried:

int whites = guess.Intersect(secret).Count() - blacks;

But, alas, IEnumerable.Intersect() produces { 1, 2 } instead of { 1, 1, 2 }, because it looks at distinct digits only. So it computes whites = 1 instead of 2.

I cannot come up with another way of computing "whites", except from using "C" style nested loops. Can you? Preferably using LINQ - I like the way an algorithm can be expressed in code using LINQ. Execution speed is not really an issue.

解决方案

var black = guess
        .Zip(secret, (g, s) => g == s)
        .Count(z => z);

var white = guess
        .Intersect(secret)
        .Sum(c =>
            System.Math.Min(
                secret.Count(x => x == c),
                guess.Count(x => x == c))) - black;

Given:

int[] secret = { 1, 2, 3, 1 };
int[] guess  = { 1, 1, 2, 2 };

Then:

black == 1 && white == 2

这篇关于使用LINQ在C#中主谋得分算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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