1

我正在制作一个 Windows Forms 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.Text 的值来检查 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[]的值,首先检查行,然后是列,然后是对角线。

表单没有按钮,因此当表单加载时,它会使用用于网格的按钮来填充它。它旨在与计算机对战,因此当您单击按钮时,.Text 变为“X”,然后计算机应该放下“O”等等。

4

1 回答 1

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
    }
于 2013-03-01T02:46:52.227 回答