C# 检查 Windows 是否适用于 Tic Tac Toe

C# Check if win function for TicTacToe

我正在尝试创建一个函数来检查是否有胜利(水平)。 所以我在纸上想到了这一点,但无法让它完全发挥作用。我的代码此时有 2 个缺陷。

  1. 它只检查第一行。之后就不起作用了。
  2. 第一行的内容。让我们说在点 0,1[X] 0,2[O] 0,3[X] 它会重新运行有一个 "true"

这是代码。

//Public Var
int n = 3;

//How I test if function is True and when. (every time an O or X is placed i do this:)

if (checkwinnner() == true)
{
    MessageBox.Show("Someone won.");
}

//Function
public bool checkwinnner() 
{  
    bool bw = true;
    for (int r = 0; r < n; r++)
    {
        bw = true;
        if (bar[r, 0].Text != "")
        {
            for (int c = 1; c < n; c++)
            {
                if (bar[r, 0].Text != bar[r, c].Text)
                {
                    bw = false; break;
                }
            }
            bw = true;
        }
        else 
        { 
            bw = false; 
        }
    }
    return bw;
}

到目前为止,这就是此功能的全部内容。我正在忙着取款机。所以。我用 bool 来检查是否有人赢了。 true 是赢 false 是还没有赢。

所以我首先放入一个for循环来循环每一行。 然后我检查文本是否不为空。因为如果它是空的,它不能在 3x3 字段中水平连续 3 个。之后我需要为每一列做一个f​​or循环。并检查第 1 列中的文本是否等于 2 和 3。

但是我在 post 的顶部说明了我的 bugs atm 并想问:

有关我可以解决或做错的任何提示。我想使用这段代码,而不是一些 if 语句来检查 if((0,1 && 0,2 && 0,3) == X || Y) 之类的按钮。因为该字段可以是 4x4 和 5x5 到。

提前谢谢你。我希望我的问题格式正确。

编码愉快。

public bool checkwinnner() 
{  
    int size = n;

    //check rows
    bool okay = true;
    for (int i = 0; i < size; i++)
    {
        bool rowOkay = true;
        //start at 1, compare with previous
        for (int j=1; j<size;j++)
        {
           //this cell is blank or doesn't match it's neighbor
           if (bar[i,j].Text == "" || bar[i, j-1].Text != bar[i,j].Text)
           {
               rowOkay = false;
               j=size;
           }
        }
        if (rowOkay) return true;
    }

    //TODO (per Question OP): Implement columns and diagonals

    return false;
}

您也可以考虑更改此告诉您 赢了("X"、"O" 或“”)。否则,您将不得不再次返回游戏板来弄清楚。

public string checkwinnner() 
{  
    int size = n;

    //check rows
    bool okay = true;
    for (int i = 0; i < size; i++)
    {
        bool rowOkay = true;
        //start at 1, compare with previous
        for (int j=1; j<size;j++)
        {
           //this cell is blank doesn't match it's neighbor
           if (bar[i,j].Text == "" || bar[i, j-1].Text != bar[i,j].Text)
           {
               rowOkay = false;
               j=size;
           }
        }
        if (rowOkay) return bar[i,0].Text;
    }

    //TODO (per Question OP): Implement columns and diagonals

    return "";
}

var winner = checkwinnner();
if (winner != "")
{
    MessageBox.Show(winner + " won.");
}

对于列,只需反转 i 和 j:bar[i,j] 变为 bar[j,i]。对于对角线,从左上角到右下角的对角线非常简单 (bar[i,i])。另一条对角线需要一些思考,但由于这看起来像是一道练习题,我希望你先自己试一试。

如果你不喜欢循环,我个人会使用 LINQ

string[][] grid = new string[3][];

grid[0] = new string[3] { "X", "O", "X" };
grid[1] = new string[3] { "", "", "" };
grid[2] = new string[3] { "X", "X", "X" };

bool test = grid.Any(r => !r.Contains("") && r.Distinct().Count() == 1);

基本上,您要查看值是否都相同(但不为空)。

更新,增加垂直测试。

int n = 3;

string[][] grid = new string[3][];

grid[0] = new string[3] { "X", "O", "X" };
grid[1] = new string[3] { "", "", "X" };
grid[2] = new string[3] { "X", "X", "X" };

bool test = grid.Any(r => !r.Contains("") && r.Distinct().Count() == 1);

// for each column, get the distinct elements from it
for(int i = 0; i < n; i++)
{
    bool vertTest = grid.Select(r => r[i]).Any(c=> !c.Contains("") && c.Distinct().Count() == 1);
}

部分问题是在循环 c 之后,您将 bw 设置回 true。该行将始终被命中,因为 break 只会使您脱离 for 循环。这就是为什么无论您在行中有什么,您都会得到 true 的原因。另一个问题是 bw 将在第一个循环重复时继续被覆盖 - 你将只能 return 最后一个值。

以下应该有效,可扩展,并尽可能接近您的原始版本。它不会告诉你赢了——你需要return除了bool之外的一些类型,如果你想让它也显示谁赢了。

public bool checkwinnner() 
{  
    bool bw = true;
    for (int r = 0; r < n; r++)
    {
        bw = true;
        if (bar[r, 0].Text != "")
        {
            for (int c = 1; c < n; c++)
            {
                if (bar[r, 0].Text != bar[r, c].Text)
                {
                    bw = false;
                }
            }
            //bw will remain true if and only if every cell in the row has the same symbol
            if (bw)
            {
                //if bw is true then someone wins, so return true so the method terminates
                return true;
            }
        }
    }
    //if we haven't already returned true, there is no winning row so return false
    return false;
}