简体   繁体   中英

Accessing rows/columns/diagonals of a 2d matrix C#

I'd like to access the rows, cols and the diagonals of a 2d matrix. How can I do this correctly?

I'm planning to use this in a function where I will access a particular row, column or diagonal and then exit the nested loop.

I've tried the following example:

Considering a 2d matrix of chars:

char[,] board = { { 'O', 'O', 'O' }, { 'X', 'X', 'X' }, { 'O', 'O', 'O' } };

for (int i = 0; i < board.GetLength(0); i++)
{
    for (int j = 0; j < board.GetLength(1); j++)
    {

        // for example the second row where all the values are X
        if (board[i+1, j] == 'X' && board[i+1, j+1] == 'X' && board[i+1, j+2] == 'X')
        {
            Console.WriteLine(board[i+1, j]);
            Console.WriteLine(board[i+1, j+1]);
            Console.WriteLine(board[i+1, j+2]);
        }
    }
}

To read from the array in the method you want, omit the for loops and just access the array directly...

// Only X's
Console.Write(board[1, 0]);
Console.Write(board[1, 1]);
Console.WriteLine(board[1, 2]);
// Diagonal
Console.Write(board[0, 0]);
Console.Write(board[1, 1]);
Console.WriteLine(board[2, 2]);

Use the for loops if you want to iterate through and display all of the values contained within the array. Remove the i+1 or 2 and j+1 or 2 as these will end up exceeding the array's constraints and cause an error

for (int i = 0; i < board.GetLength(0); i++)
{
    for (int j = 0; j < board.GetLength(1); j++)
    {
        Console.WriteLine(board[i, j]);
    }
}

To search for winners, you can do something as simple as checking all 8 combinations:

if ((board[0, 0] == charToCompare && board[0, 1] == charToCompare && board[0, 2] == charToCompare) || //Row 1
(board[1, 0] == charToCompare && board[1, 1] == charToCompare && board[1, 2] == charToCompare) || //Row 2
(board[2, 0] == charToCompare && board[2, 1] == charToCompare && board[2, 2] == charToCompare) || //Row 3
(board[0, 0] == charToCompare && board[1, 0] == charToCompare && board[2, 0] == charToCompare) || //Col 1
(board[1, 0] == charToCompare && board[1, 1] == charToCompare && board[1, 2] == charToCompare) || //Col 2
(board[2, 0] == charToCompare && board[2, 1] == charToCompare && board[2, 2] == charToCompare) || //Col 3
(board[0, 0] == charToCompare && board[1, 1] == charToCompare && board[2, 2] == charToCompare) || //Diagonal Top to Bottom
(board[2, 0] == charToCompare && board[1, 1] == charToCompare && board[0, 2] == charToCompare)) //Diagonal Bottom to Top
{
    Console.WriteLine("Winner");
}

Or to modify your original code with 1 for loop and the breaks, you can do like so. There are probably much better ways, but effectively you will need to implement a routine to check for the solutions you desire

char[,] board = {   { 'X', 'O', 'O' }
                ,   { 'X', 'X', 'O' }
                ,   { 'O', 'X', 'X' } };

char charToCompare = 'X';

for (int i = 0; i < 3; i++)
{
     if (board[i, 0] == charToCompare && board[i, 1] == charToCompare && board[i, 2] == charToCompare)
     {
         Console.WriteLine($"Winner Row {i}");
         break;
     }
     if (board[0, i] == charToCompare && board[1, i] == charToCompare && board[2, i] == charToCompare)
     {
         Console.WriteLine($"Winner Column {i}");
         break;
     }
}

if (board[0, 0] == charToCompare && board[1, 1] == charToCompare && board[2, 2] == charToCompare)
{
    Console.WriteLine("Winner diagonal top to bottom");
}

if (board[2, 0] == charToCompare && board[1, 1] == charToCompare && board[0, 2] == charToCompare)
{
    Console.WriteLine("Winner diagonal bottom to top");
}

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