繁体   English   中英

C#检查TicTacToe的win功能

[英]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;
}

因此,到目前为止,此功能仅此而已。 我很喜欢它的atm。 所以。 我用bool检查有人是否赢了。 真是赢,假是没有赢。

  • N =高度和视场。 它始终是3x3,但可以更改为变形文本框。
  • R =行
  • C =列

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

但是我在帖子的顶部声明了我的bugs atm,并想问一下:

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

先感谢您。 我希望我的问题得到正确的格式化。

快乐的编码。

问题的一部分是,在循环遍历c ,您需要将bw设置回true 因为break只会使您跳出for循环,所以总是会碰到该行。 这就是为什么不管行中有什么都变得true的原因。 另一个问题是,随着第一个循环重复进行, bw将继续被覆盖-您将只能返回最后一个值。

以下内容应该可以使用,并且可以扩展,并尽可能接近原始内容。 它不会告诉您赢了-如果您想让其他人也显示谁赢了,则需要返回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;
}
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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM