简体   繁体   中英

Diagonal and Up/Down Tic Tac Toe Algorithm C# Wpf

So I have programmed the horizontal win for Tic Tac Toe, however I haven't been successful with the others.

Here is the code for the horizontal win.

 private void CheckWinner(int row, int col)
    {
        if (aIntNaughtsCrosses[row, 0] == aIntNaughtsCrosses[row, 1])
        {
            if (aIntNaughtsCrosses[row, 0] == aIntNaughtsCrosses[row, 2])
            {
                MessageBox.Show("Yay");
            }

         }

aIntNaughtsCrosses is the array.

 else if(aIntNaughtsCrosses[col, 0] == aIntNaughtsCrosses[row, 0])
        {
            if(aIntNaughtsCrosses[col, 0] == aIntNaughtsCrosses[row, 0])
            {
                if(aIntNaughtsCrosses[col, 0] == aIntNaughtsCrosses[row, 1])
                {
                    if(aIntNaughtsCrosses[col, 0] == aIntNaughtsCrosses[row, 2])
                    {
                        MessageBox.Show("yay");
                    }
                }
            }


        }

That is the code I planned for the vertical/down win. With it also being applied to column 1 and 2.

You got it right for horizontal, keeping same row and making the column change.
Now, you have to keep the same column and make the row change to check for vertical:

if (aIntNaughtsCrosses[row, 0] == aIntNaughtsCrosses[row, 1] && // horizontal
    aIntNaughtsCrosses[row, 0] == aIntNaughtsCrosses[row, 2] || // horizontal
    aIntNaughtsCrosses[0, col] == aIntNaughtsCrosses[1, col] && // vertical
    aIntNaughtsCrosses[0, col] == aIntNaughtsCrosses[2, col])   // vertical
{
    MessageBox.Show("Yay");
}

This means:

 IF (row,0) equals (row,1) equals (row,2) OR // horizontal (0,col) equals (1,col) equals (2,col) THEN // vertical DISPLAY "Yay" 

For diagonals, note that there are only 2 diagonals so you could do (shortening aIntNaughtsCrosses to arr ):

if (col == row &&     // top-left to bottom-right
    arr[0, 0] == arr[1, 1] &&
    arr[0, 0] == arr[2, 2] ||
    col == 2 - row && // top-right to bottom-left
    arr[0, 2] == arr[1, 1] &&
    arr[0, 2] == arr[2, 0])
{
    MessageBox.Show("Yay");
}

This means:

 IF (row,col) is in diag1 AND (0,0) equals (1,1) equals (2,2) OR // diag1 (row,col) is in diag2 AND (0,2) equals (1,1) equals (2,0) THEN // diag2 DISPLAY "Yay" 

In summary, the following should work:

if (arr[row, 0] == arr[row, 1] && // horizontal
    arr[row, 0] == arr[row, 2] ||
    arr[0, col] == arr[1, col] && // vertical
    arr[0, col] == arr[2, col] ||
    col == row &&     // top-left to bottom-right
    arr[0, 0] == arr[1, 1] &&
    arr[0, 0] == arr[2, 2] ||
    col == 2 - row && // top-right to bottom-left
    arr[0, 2] == arr[1, 1] &&
    arr[0, 2] == arr[2, 0])
{
    MessageBox.Show("Yay");
}

This means:

 IF (row,0) equals (row,1) equals (row,2) OR // horizontal (0,col) equals (1,col) equals (2,col) OR // vertical (row,col) in diag1 AND (0,0) equals (1,1) equals (2,2) OR // diag1 (row,col) in diag2 AND (0,2) equals (1,1) equals (2,0) THEN // diag2 DISPLAY "Yay" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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