在Array中迭代. text值
本文关键字:text 迭代 Array | 更新日期: 2023-09-27 18:01:35
我正在制作一个Windows窗体井字棋程序,该程序具有由App.config文件中的值设置的网格大小。因此,如果值为9,则网格是常规的3 × 3,如果值为25,则是5 × 5,等等。
我有一个Gameboard类,使实际的按钮数组:
public Gameboard(int numberofButtons) //Constructor method that is referencing the App.config for the dimensions value to make the board
{
buttonArray = new Button[numberofButtons]; //creating an array the size of numberofButtons which is the dimensions value from App.config
Font font = new Font("Times New Roman", 36.0f); //creates an instance of the Font class
int sqrtY = (int) Math.Sqrt(numberofButtons);
int z = 0; //Counter for array
//Create the buttons for the form
for (int x = 0; x < sqrtY; x++)
{
for (int y = 0; y < sqrtY; y++)
{
buttonArray[z] = new Button();
buttonArray[z].Font = font;
buttonArray[z].Size = new System.Drawing.Size(100, 100);
buttonArray[z].Location = new System.Drawing.Point(100*y, 100*x);
buttonArray[z].Click += new EventHandler(button_click);
z++;
}
}
}
如果我想通过简单地检查buttonArray的值来检查数组中的获胜模式。buttonArray中位置的文本,我如何遍历它?
例如,在4x4网格中,水平胜线将是
0,1,2,3
4,5,6,7
8,9,10,11
12,13,14,15
x=0, x<sqrt(dimension), x++
buttonArray[x].Text == "X" <-- to test
我意识到这是非常糟糕的编码和混乱,但我试图做到这与一个单一的维度数组,因为这是老师要求的。我想检查buttonArray[]
的值,首先检查行,然后检查列,然后检查对角线。
表单没有按钮,所以当表单加载时,它会填充用于网格的按钮。这是为了对抗电脑,所以当你点击一个按钮,文本变成"X",然后电脑应该放下一个"O",等等。
我提供了如何分离您指定的条件的示例。我非常愿意回答具体的问题,但由于这是一个家庭作业问题,我不想提供所有内容。希望这对你有帮助。
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
}