我提供了如何区分您指定的标准的示例。我非常愿意回答具体问题,但由于这是一个家庭作业问题,我不想提供所有内容。希望这会有所帮助。
string[] values = buttonArray.Select(b => b.Text).ToArray();
//uses integer divison to separate out rows
string[][] rows = values
.Select((v, i) => new {index = i, value = v})
.GroupBy(anon => anon.index/size)
.Select(grp => grp.Select(anon => anon.value).ToArray())
.ToArray();
foreach(string[] row in rows)
{
//Pseudocode
var winner = CheckAllValuesArePopulatedAndTheSameAKAWinner(row);
if (winner exists)
return winner;
}
//uses modulus to separate out columns, basically a transpose
string[][] columns = values
.Select((v, i) => new { index = i, value = v })
.GroupBy(anon => anon.index % size)
.Select(grp => grp.Select(anon => anon.value).ToArray())
.ToArray();
string[] diagonal1 = values
.Select((v, i) => new {index = i, value = v})
.Where(anon => anon.index%(size + 1) == 0)
.Select(anon => anon.value) //take just the string value, drop the index
.ToArray();
string[] diagonal2 = values
.Select((v, i) => new { index = i, value = v })
.Where(anon => anon.index % (size - 1) == 0)
.Skip(1) //skip 0 index
.Take(size) //skip last index
.Select(anon => anon.value) //take just the string value, drop the index
.ToArray();
指数示例:
//given size and dimension
//run this code once per different size, not on each check (probably move variable to some outer scope)
Dictionary<string, int[]> winningSets = new Dictionary<string, int[]>();
int[] indices = Enumerable.Range(0, dimension).ToArray(); //make array of 0 to (dimension - 1)
for (int rowNum = 0; rowNum < size; ++rowNum) //0 based row index/num
{
int[] rowIndices = indices
.Where(i => i / size == rowNum)
.ToArray();
winningSets.Add(String.Format("Row Number {0}", rowNum + 1), rowIndices); //use 1 based row index/num for display
}
for (int colNum = 0; colNum < size; ++colNum) //0 based row index/num
{
int[] colIndices = indices
.Where(i => i % size == colNum)
.ToArray();
winningSets.Add(String.Format("Column Number {0}", colNum + 1), colIndices); //use 1 based column index/num for display
}
int[] diag1 = indices
.Where(i => i % (size + 1) == 0)
.ToArray();
winningSets.Add("Diag TR to BL", diag1);
int[] diag2 = indices
.Where(i => i % (size - 1) == 0)
.Skip(1) //skip 0 index
.Take(size) //skip last index
.ToArray();
winningSets.Add("Diag TL to Br", diag2);
//run this for each check
foreach(KeyValuePair<string, int[]> winningSet in winningSets)
{
//Pseudo
if (all of winningSet.Value indices are populated and same)
return winningSet.Name; //Name of winning set, ex will not return multiple winning set which is possible in this game
//this example does not show who won, but whoever had the last turn is the winner if one is found.
}
添加如何检查值数组的示例(表示行/列/对角线)
string CheckWinner(string[] setOfValues)
{
if (setOfValues.Any(individualValueFromSetOfValues => String.IsNullOrEmpty(individualValueFromSetOfValues))) // some values/cells are not filled in so no winner here
return null;
if (setOfValues.Distinct().Count() == 1) //all data is the same
return setOfValues.First(); //any value would work, we just checked that they're all the same
return null; // all values/cells are full but they are not all the same
}