none
How to check if some elements of 2D array are equal in c# sharp RRS feed

  • Question

  • Given the array:

    string[,] arr = new string[n,n].
    

    How do you check if the elements are equal for each row, for each column and for the two diagonals?

    Thank you in advance: it is a kind of tic tac toe: Write a console application that receives as input date N moves of X and 0 as coordinates. (0, 0) is the top left corner and (2, 2) is the bottom right corner. On the first line is the number of moves N, and from line two there are moves, one on each line. The first move is of the player with X, followed by the move of the player with 0, then again X and so on.

    The application will analyze the received moves and will display the winner: X, 0 or draw if there is no winner.

    using System;

    namespace TicTacToe { class Program { static void Main() { int numberOfMoves = Convert.ToInt32(Console.ReadLine()); const int size = 3; string[,] boardGame = new string[size, size]; for (int i = 0; i < numberOfMoves; i++) { string strCoordinates = Console.ReadLine(); string[] lineCoordinates = strCoordinates.Split(' '); int coordinateX = Convert.ToInt32(lineCoordinates[0]); int coordinateY = Convert.ToInt32(lineCoordinates[1]); const int value = 2; boardGame[coordinateX, coordinateY] = i % value == 0 ? "X" : "0"; } CheckElements(boardGame); Console.Read(); } static void CheckRows(int x, int y) { string[,] boardGame = new string[3, 3]; int cols = boardGame.GetLength(1); const int value = 2; for (int i = 0; i < cols; i++) { if ((boardGame[0, 0] == boardGame[0, 1] && boardGame[0, 1] == boardGame[0, value]) || (boardGame[1, 0] == boardGame[1, 1] && boardGame[1, 1] == boardGame[1, value])) { Console.WriteLine(boardGame[0, 0]); } if ((boardGame[1, 0] == boardGame[1, 1] && boardGame[1, 1] == boardGame[1, value]) || (boardGame[value, 0] == boardGame[value, 1] && boardGame[value, 1] == boardGame[value, value])) { Console.Write(boardGame[0, 0]); } } Console.WriteLine(boardGame[x, y]); } static void CheckColumns(int x, int y) { string[,] boardGame = new string[3, 3]; int rows = boardGame.GetLength(0); const int value = 2; for (int i = 0; i < rows; i++) { if ((boardGame[0, 0] == boardGame[1, 0] && boardGame[1, 0] == boardGame[value, 0]) || (boardGame[0, 1] == boardGame[1, 1] && boardGame[1, 1] == boardGame[value, 1])) { Console.WriteLine(boardGame[0, 0]); } if ((boardGame[0, 1] == boardGame[1, 1] && boardGame[1, 1] == boardGame[value, 1]) || (boardGame[0, value] == boardGame[1, value] && boardGame[1, value] == boardGame[value, value])) { Console.WriteLine(boardGame[0, 1]); } } Console.WriteLine(boardGame[x, y]); } static void CheckDiagonals(int x, int y) { string[,] boardGame = new string[3, 3]; int m = boardGame.Length; const int value = 2; for (int i = 0; i < m; i++) { m--; for (int j = 0; j < m; j++) { if (boardGame[0, 0] == boardGame[1, 1] && boardGame[1, 1] == boardGame[value, value]) { Console.WriteLine(boardGame[0, 0]); } } } Console.WriteLine(boardGame[x, y]); } } }

    Can you guys help me with that?

    Tuesday, October 20, 2020 1:41 PM

All replies

  • Hi Simona.M, thank you asking your question.  But I should tell you following two messages.

    1. This forum is for Microsoft Small Basic programming language.  Details are described in PLEASE READ BEFORE POSTING.  So you need to find an appropriate community to ask your question about C#.

    2. MSDN forums will close soon.  New user/developer community is Microsoft Q&A.  Please read How to: Write a quality question before posting to Microsoft Q&A.

    Nonki Takahashi


    Tuesday, October 20, 2020 10:13 PM
    Moderator