我有两个需要比较的列表:
var list = new Guess(guesses, 0 , 0);// see class for constructor
和
theGame.Target = guesses;
var list2 = theGame.Target;
我需要做以下事情:
GuessTheDigits
正确设置RightDigitsInTheRightPosition
的数量
GuessTheDigits:
public ActionResult GuessTheDigits(List<int> guesses)
{
GuessingGame theGame = this.Session["GameState"] as GuessingGame;
// pass to the method
theGame.GuessTheHiddenDigits(guesses);
//set the lists
var list = new Guess();
theGame.Target = guesses;
var list2 = theGame.Target;
//need to compare lists here
// Redirect to action
return RedirectToAction("Index", theGame.Guesses);
}
课程如下:
猜类
public class Guess
{
public Guess()
: this(new List<int>() { 1, 2, 3 }, 0, 0 )
{
}
public Guess(List<int> guesses, int rdrp, int rdwp)
{
this.Digits = guesses;
this.RightDigitRightPosition = rdrp;
this.RightDigitWrongPosition = rdwp;
}
public Guess(List<int> guesses, int rdrp)
: this(guesses, rdrp, 0)
{
}
public Guess(List<int> guesses)
: this(guesses, 0, 0)
{
}
public List<int> Digits { get; set; }
public int RightDigitRightPosition { get; set; }
public int RightDigitWrongPosition { get; set; }
}
之前尝试过的代码:
var check = list2.Where(s => !theGame.Guesses.Contains(list));
foreach (var i in check)
{
list.RightDigitRightPosition = i;
}
答案 0 :(得分:1)
假设theGame.Guesses
是List<Guess>
,那么我认为(因为它并不完全清楚)您所追求的代码是
var samePositions = myGame.Guesses.Where(x => list.Digits.IndexOf(x.RightDigitRightPosition) == x.Digits.IndexOf(x.RightDigitRightPosition)));
这会提取所有RightDigitRightPosition
值且与list
位于同一位置的猜测。
答案 1 :(得分:0)
您可以使用IndexOf
查找值的位置:
int i = this.Digits.IndexOf(value);
目前还不清楚如何设置 RightDigitRightPosition
和RightDigitWrongPosition
以确切知道如何做到这一点。