我正在制作一个Windows窗体Tic-Tac-Toe程序,其网格大小由App.config文件中的值设置。因此,如果值为9,则网格为常规3 x 3,如果为25,则为5 x 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.Text的值来检查数组中的win模式,我该如何迭代它?
例如,在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[]
的值,首先检查行,然后检查列,然后检查对角线。
表单没有按钮,因此当表单加载时,它会使用用于网格的按钮填充它。这是为了对抗计算机,所以当你点击一个按钮时,.Text变为“X”,然后计算机应该放下一个“O”,依此类推。
答案 0 :(得分:2)
我提供了如何分离您指定的标准的示例。我非常愿意回答具体问题,但由于这是一个家庭作业问题,我不想提供一切。希望这会有所帮助。
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
}