我在做Tic Tac脚趾游戏的核心游戏。
我的代码有一个动态大小的板(可以是2x2到10x10,如果是7x7,我们需要连续7个字符,如果是4x4,我们需要4等等),这个代码对任意大小的矩阵都有效,如果有人赢了游戏,给我一个bool状态(他们可以使用任何字符在矩阵上标记,比如X或O)。此代码跳过空字符串("")和零("0")。
矩阵校验器代码(在这里,我要检查矩阵)
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;
}
}数组Validator代码(在这里,我要检查行、数组)
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 (我的代码的用法)
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();
}我能做什么改变来改进这段代码?也许它不是用实心实现的?我需要帮助
发布于 2020-06-06 02:06:35
这里有一些关于这个特定实现的一些温和建议:
this.Matrix.GetLength(0)的值并使用它,而不是多次调用它。因为您知道矩阵总是NxN,所以您知道第一维长度总是匹配于第二维长度(换句话说,this.Matrix.GetLength(1) == this.Matrix.GetLength(0))。if (matrix[i,j] != matrix[i + 1,j]) return false;显然需要一些边界检查,等等。MatrixHasWinner之外,您的代码中没有任何东西表明这是为了玩toe游戏。您使用的是非常通用的名称(矩阵、向量),而不是为其分配任何上下文。基质可能是GameBoard。向量可以是行或列(不过,正如我前面所说,您并不真正需要它)。https://codereview.stackexchange.com/questions/243451
复制相似问题