static void disp(string[,] mat, int flg )
for(int i=0;i< mat.GetLength(0);i++)
for(int j=0;j<mat.GetLength(1); j++)
Console.Write(mat[i,j]+" ");
if(j!= mat.GetLength(1)-1){
Console.WriteLine("___________");
Console.WriteLine("Player {0} select your box", flg);
static bool check_win(string[,] mat , int row, int col, int rows, int cols)
bool vert= (mat[row,col] == mat[(row+1)%rows, col ] && mat[row, col] == mat[(row+2)%rows, col] );
bool flat = (mat[row,col] == mat[row, (col+1)%cols] && mat[row, col] == mat[row, (col+2)%cols] ) ;
bool dia1 = (mat[row,col] == mat[(row +1)%rows, (col+1)%cols] && mat[row,col] == mat[(row +2)%rows, (col+2)%cols] );
bool dia2 = (mat[row,col] == mat[(row +1)%rows, (col-1+cols)%cols] && mat[row,col] == mat[(row +2)%rows, (col-2+cols)%cols] );
if(vert || flat || dia1 || dia2 )
public static void Main()
string [,] mat= new string[3,3]{{"1","2","3"}, {"4", "5","6"}, {"7","8","9"}};
int rows= mat.GetLength(0);
int cols= mat.GetLength(1);
bool valid = int.TryParse( Console.ReadLine(), out num);
Console.WriteLine("Enter only number");
else if ( num < 1 || num > 9)
Console.WriteLine("Enter number in between from 1 to 9 only");
Console.WriteLine("Enter non-selected numbers ");
int row= temp/cols, col= temp%cols;
bool win = check_win(mat, row, col, rows, cols);
Console.WriteLine("Player {0} WINS",flg);
Console.WriteLine("Game ended");
Console.WriteLine("Do you want to play again (Y/ N) ?");