i'm doing a Core for a Tic Tac Toe game.
I have my code with a dynamic size of the board (can be 2x2 to 10x10, if is 7x7 we need a 7 characters in a row to win , if is 4x4 we need 4 etc) , This code works for any matrix with any size and give me a bool state if someone won the game (they can use any character to mark on the matrix, like X or O). This code skips the empty strings ("") and zeros ("0").
The Matrix Validator Code (Where i send the matrix i wanna check)
public class MatrixValidator
{
private string[,] Matrix { get; set; }
private string[] Vector { get; set; }
public MatrixValidator(string[,] matrix)
{
this.Vector = new string[matrix.GetLength(0)];
this.Matrix = matrix;
}
public bool MatrixHasWinner()
{
return HorizontalValidator() || VerticalValidator() || MainDiagonalValidator() || SecondDiagonalValidator();
}
private bool HorizontalValidator()
{
bool Control = false;
for (int i = 0; i < this.Matrix.GetLength(1); i++)
{
Array.Clear(Vector, 0, Vector.Length);
for (int j = 0; j < this.Matrix.GetLength(0); j++)
{
this.Vector[j] = this.Matrix[i, j];
}
if (!ArrayValidator.HasBlank(this.Vector))
Control = ArrayValidator.HasSameValues(this.Vector);
}
return Control;
}
private bool VerticalValidator()
{
bool Control = false;
for (int j = 0; j < this.Matrix.GetLength(0); j++)
{
Array.Clear(Vector, 0, Vector.Length);
for (int i = 0; i < this.Matrix.GetLength(1); i++)
{
this.Vector[i] = this.Matrix[i, j];
}
if (!ArrayValidator.HasBlank(this.Vector))
Control = ArrayValidator.HasSameValues(this.Vector);
}
return Control;
}
private bool MainDiagonalValidator()
{
bool Control = false;
Array.Clear(Vector, 0, Vector.Length);
for (int i = 0; i < this.Matrix.GetLength(0); i++)
{
for (int j = 0; j < this.Matrix.GetLength(1); j++)
{
if (i == j)
this.Vector[j] = this.Matrix[i, j];
}
}
if (!ArrayValidator.HasBlank(this.Vector))
Control = ArrayValidator.HasSameValues(this.Vector);
return Control;
}
private bool SecondDiagonalValidator()
{
bool Control = false;
Array.Clear(Vector, 0, Vector.Length);
for (int i = 0; i < this.Matrix.GetLength(1); i++)
{
for (int j = 0; j < this.Matrix.GetLength(0); j++)
{
if (i + j == this.Matrix.GetLength(0) - 1)
this.Vector[i] = this.Matrix[i, j];
}
}
if (!ArrayValidator.HasBlank(this.Vector))
Control = ArrayValidator.HasSameValues(this.Vector);
return Control;
}
}
The Array Validator Code (Where i send the line, array i wanna check)
public static class ArrayValidator
{
public static bool HasBlank(string[] vector)
{
return (vector.Contains("") || vector.Contains("0"));
}
public static bool HasSameValues(string[] vector)
{
var v = vector.Distinct().Count();
if (v == 1)
return true;
else
return false;
}
}
Program.cs (The usage of my code)
static void Main(string[] args)
{
string[,] matrix = { { "X", "X", "X", "X" },
{ "O", "", "", "" },
{ "", "", "", "" },
{ "O", "", "O", "" } };
// X Wins
MatrixValidator matrixValidator = new MatrixValidator(matrix);
Console.WriteLine(matrixValidator.MatrixHasWinner());
Console.ReadKey();
}
What changes can i do to improve this code? Maybe it is not implemented with SOLID? I need help