I’m new in C#. My first attempt is a console application for the Noughts & Crosses game. I’m stuck in writing the condition for break if any player wins. Any comments will be appreciated.

//Tic Tac Toe

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TTT
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int  n = 0;
            const int ARRAY1_LENGHT = 10;
            int[] array1 = new int[ARRAY1_LENGHT];
            array1[n] = 0;
            char[] array2 = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };


            int[,] board = new int[3, 3];
            Console.SetCursorPosition(0, 0);



            Console.WriteLine();
            Console.WriteLine("   |   |   ");
            Console.WriteLine("   |   |   ");
            Console.WriteLine("  1|  2|  3");
            Console.WriteLine("------------ ");
            Console.WriteLine("   |   |   ");
            Console.WriteLine("  4|  5|  6");
            Console.WriteLine("------------ ");
            Console.WriteLine("   |   |   ");
            Console.WriteLine("   |   |   ");
            Console.WriteLine("  7|  8|  9");
            Console.WriteLine();
            Console.WriteLine();

            string input = "";

            while ( i <= 9  )
            {
                i++;
                Console.WriteLine(" Please enter a number from 1 to 9 : ");

                input = Console.ReadLine();
                n = int.Parse(input);

                if ( array1[n] == n )
                {
                    Console.WriteLine(" Please enter only number what hasn't been entered before : ");

                    input = Console.ReadLine();
                    n = int.Parse(input);    
                }

                array1[n] = n;

                if (n == 1)
                {
                    Console.SetCursorPosition(2,2);
                }
                if (n == 2)
                {
                    Console.SetCursorPosition(5,2);
                }
                if (n == 3)
                {
                    Console.SetCursorPosition(8, 2);
                }
                if (n == 4)
                {
                    Console.SetCursorPosition(2, 5);
                }
                if (n == 5)
                {
                    Console.SetCursorPosition(5, 5);
                }
                if (n == 6)
                {
                    Console.SetCursorPosition(8, 5);
                }
                if (n == 7)
                {
                    Console.SetCursorPosition(2, 8);
                }
                if (n == 8)
                {
                    Console.SetCursorPosition(5, 8);
                }
                if (n == 9)
                {
                    Console.SetCursorPosition(8, 8);
                }


                if (i % 2 == 0)
                {
                    Console.WriteLine("X");
                    array2[i] = 'X';
                }
                else
                {
                    Console.WriteLine("0");
                    array2[i] = '0';
                }

                
   if (array2[i] == array2[i + 1] && array2[i] == array2[i + 2] ||
       array2[i] == array2[i + 2] && array2[i] == array2[i + 4] ||
       array2[i] == array2[i + 3] && array2[i] == array2[i + 6] ||
       array2[i] == array2[i + 4] && array2[i] == array2[i + 8])
               
                    Console.SetCursorPosition(0, 15);
                    Console.WriteLine("WINNER IS ", array2[i]);
                    break;

               
            }
            Console.SetCursorPosition(0, 13); 
        }
    }
}

You're missing braces, for one. And you only have 4 win conditions -- tic tac toe has eight. Why are you using the variable i in the win condition? The win condition has nothing to do with the turn number.

Thank you for helping me out.
:)
Why I am using the variable i in the win condition?

If the win condition has nothing to do with the turn number.

Because
array2 stores X or O depending on who belongs the turn to :

if (i % 2 == 0)
                    {
                        Console.WriteLine("X");
                        array2[i] = 'X';
                    }
                    else
                    {
                        Console.WriteLine("0");
                        array2[i] = '0';
                    }

The line
Console.WriteLine("WINNER IS ", array2);
outputs who is winner – X or O.


The program compiles well as absolutely error-free. So if any braces were missing it would be pointed out immediately.

I implemented 8 winning conditions, but still these conditions don't work:

//Noughts & Crosses

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TTT
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int n = 0;
            string input = "";
            bool exitGame = false;
            const int ARRAY1_LENGHT = 10;
            int[] array1 = new int[ARRAY1_LENGHT];
            array1[n] = 0;
            char[] array2 = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

            int[,] board = new int[3, 3];
            Console.SetCursorPosition(0, 0);

            Console.WriteLine();
            Console.WriteLine("   |   |   ");
            Console.WriteLine("   |   |   ");
            Console.WriteLine("  1|  2|  3");
            Console.WriteLine("------------ ");
            Console.WriteLine("   |   |   ");
            Console.WriteLine("  4|  5|  6");
            Console.WriteLine("------------ ");
            Console.WriteLine("   |   |   ");
            Console.WriteLine("   |   |   ");
            Console.WriteLine("  7|  8|  9");
            Console.WriteLine();
            Console.WriteLine();

            if (
                        array2[1] == array2[2] && array2[2] == array2[3] ||
                        array2[4] == array2[5] && array2[4] == array2[6] ||
                        array2[7] == array2[8] && array2[7] == array2[9] ||

                        array2[1] == array2[4] && array2[4] == array2[7] ||
                        array2[2] == array2[5] && array2[5] == array2[8] ||
                        array2[3] == array2[6] && array2[6] == array2[9] ||

                        array2[3] == array2[5] && array2[5] == array2[7] ||
                        array2[1] == array2[5] && array2[5] == array2[9]
                        )
            {
                exitGame = true;
            }

            while (!exitGame)
            {

                for (i = 0; i <= 9; i++)
                {
                    Console.SetCursorPosition(0, 13);
                    Console.WriteLine(" Please enter a number from 1 to 9 : ");

                    input = Console.ReadLine();
                    n = int.Parse(input);

                    if (array1[n] == n)
                    {
                        Console.WriteLine(" Please enter only number what hasn't been entered before : ");

                        input = Console.ReadLine();
                        n = int.Parse(input);
                    }

                    array1[n] = n;

                    if (n == 1)
                    {
                        Console.SetCursorPosition(2, 2);
                    }
                    if (n == 2)
                    {
                        Console.SetCursorPosition(5, 2);
                    }
                    if (n == 3)
                    {
                        Console.SetCursorPosition(8, 2);
                    }
                    if (n == 4)
                    {
                        Console.SetCursorPosition(2, 5);
                    }
                    if (n == 5)
                    {
                        Console.SetCursorPosition(5, 5);
                    }
                    if (n == 6)
                    {
                        Console.SetCursorPosition(8, 5);
                    }
                    if (n == 7)
                    {
                        Console.SetCursorPosition(2, 8);
                    }
                    if (n == 8)
                    {
                        Console.SetCursorPosition(5, 8);
                    }
                    if (n == 9)
                    {
                        Console.SetCursorPosition(8, 8);
                    }


                    if (i % 2 == 0)
                    {
                        Console.WriteLine("X");
                        array2[i] = 'X';
                    }
                    else
                    {
                        Console.WriteLine("0");
                        array2[i] = '0';
                    }


                    if (exitGame == true)
                    {
                        Console.SetCursorPosition(0, 15);
                        Console.WriteLine("WINNER IS ", array2[i]);
                        break;
                    }
                }
            }
        }
    }
}

So there's your problem. If array2 gets stored 'X' when i is even and 'O' when i is odd, it will just contain { 'X', 'O', 'X', 'O', 'X', 'O', 'X', ... }

You should be using the turn number to figure out whether to write an X or an O, and you should be using the user's input to figure out what square to write the X or O to.

Right...code explanation how this works. The second array was initialized in the declaration line by placing list of 1-9 numbers:
char[] array2 = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
If the square is empty, the related to the square element of the second array equals to the number of the square: array2[n] = n+1.
If move has been made to the square, the element of the second array related to the square holds corresponding nought or cross:

if (i % 2 == 0)
                    {
                        Console.WriteLine("X");
                        array2[i] = 'X';
                    }
                    else
                    {
                        Console.WriteLine("0");
                        array2[i] = '0';
                    }

This helps to create 8 victory conditions because of stored noughts and crosses:

if (
    (array2[0] == array2[2] && array2[0] == array2[1]) ||
    (array2[0] == array2[4] && array2[0] == array2[8]) ||
    (array2[0] == array2[3] && array2[0] == array2[6]) ||
    (array2[3] == array2[4] && array2[3] == array2[5]) ||
    (array2[6] == array2[7] && array2[6] == array2[8]) ||
    (array2[2] == array2[4] && array2[2] == array2[6]) ||
    (array2[2] == array2[5] && array2[2] == array2[8]) ||
    (array2[1] == array2[4] && array2[1] == array2[7])
   )
    {
                        exitGame = true;
    }

Every element of the first array was initialized to 0 ->array1[n] =0 and with every move it gets the value of the square-> array1[n]=n.
With every move it helps checking - does square is empty or not:

if (array1[n] == n)
    {
    Console.WriteLine(" Please enter only number what hasn't been entered before : ");
     }
      else
          {
           validInput = true;
          }

I can't see any problem with arrays.

Hello, cause&effect.
Just to add to Rashakil Fol's words: your array2 is specific view of your gaming board. That mean, that every value in it depends not only on who belongs the turn to, but also on a position, where to store 'X' or 'O'. You already use that to check the condition (e.g. (array2[0] == array2[2] && array2[0] == array2[1]) ... mean, that you're checking if the 'X'/'O' are placed in 1,2 and 3 fields).
But you don't use that to store data into array:

if (i % 2 == 0)
{  
     array2[i] = 'X';

Take that into account :)

There is nothing wrong in storing data into array as long as this helps with a victory check.

"What if
Michelangelo had said, ‘I don’t do ceilings,’ and walked away
from painting the Sistine Chapel?...”

Ok, let's see ... your array2 in any case has value { 'X', 'O', 'X', 'O', 'X', 'O', 'X', 'O', 'X' } (even if users during the game entering the 'X'/'O' in other order). To make it more obvious (same data in table view):
X | O | X
----------
O | X | O
----------
X | O | X

And you check THIS table for matching your conditions (not entered by user).

Thank you, Antenka. My mistake was the index value - instead of i (number of moves) I must use n (position of square). Now my code gives correct output:

if (i % 2 == 0)
                    {
                        Console.WriteLine("X");
                        array2[n-1] = 'X';
                    }
                    else
                    {
                        Console.WriteLine("0");
                        array2[n-1] = '0';
                    }
This question has already been answered. Start a new discussion instead.