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);
}
}
}