Search code examples
c#for-loopif-statementbooleantic-tac-toe

C# Check if win function for TicTacToe


I am trying to create an function to check if there is an win (horizontally). So I thought this out on paper but could not get it to fully work. My code has 2 flaws at this point.

  1. It only checks first row. and does not work after that.
  2. Whatever is in the first row. lets say in point 0,1[X] 0,2[O] 0,3[X] it wil retrun that there is an "true"

Here is the code.

//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;
}

So This is all there is to this function as of yet. Im buisy with it atm. So. I used an bool to check if someone won. true is win false is no win yet.

  • N = height and with of field. its 3x3 always but can be changed trought textbox.
  • R = row
  • C = column

so I first put in an for loop to loop every row. Then I check if text is not empty. becouse if its empty it cant be 3 in a row in a 3x3 field horizontally. After that i need to do a for loop for every column. and check if text in columns 1 is equal to 2 and 3.

However I stated my buggs atm at the top of the post and would like to ask:

Any tips on what I can fix or am doing wrong. I would like to use this code and not some if statement that checks the buttons like if((0,1 && 0,2 && 0,3) == X || Y) or something like that. becouse the field can be 4x4 and 5x5 to.

Thank you in advance. And I hope my question is formatted correcly.

Happy coding.


Solution

  • Part of the problem is that after you loop over c, you're then setting bw back to true. That line will always be hit because the break will only break you out of the for loop. This is why you're getting true regardless of what you have in the row. The other problem is that bw will continue to get overwritten as the first loop repeats - you'll only ever be able to return the last value.

    The following should work, is scalable, and stays as close to your original as possible. It doesn't tell you who won - you'd need to return some type other than a bool if you wanted this to also show who won.

    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;
    }