jeudi 28 septembre 2017

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 = 0;

//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.

Aucun commentaire:

Enregistrer un commentaire