首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Tic Tac脚趾游戏实战

C# Tic Tac脚趾游戏实战
EN

Code Review用户
提问于 2020-06-05 23:05:46
回答 1查看 417关注 0票数 2

我在做Tic Tac脚趾游戏的核心游戏。

我的代码有一个动态大小的板(可以是2x2到10x10,如果是7x7,我们需要连续7个字符,如果是4x4,我们需要4等等),这个代码对任意大小的矩阵都有效,如果有人赢了游戏,给我一个bool状态(他们可以使用任何字符在矩阵上标记,比如X或O)。此代码跳过空字符串("")和零("0")。

矩阵校验器代码(在这里,我要检查矩阵)

代码语言:javascript
复制
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代码(在这里,我要检查行、数组)

代码语言:javascript
复制
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 (我的代码的用法)

代码语言:javascript
复制
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();
        }

我能做什么改变来改进这段代码?也许它不是用实心实现的?我需要帮助

EN

回答 1

Code Review用户

回答已采纳

发布于 2020-06-06 02:06:35

这里有一些关于这个特定实现的一些温和建议:

  • 这并不是真正的“验证器”,因为它不验证任何东西。也许可以考虑一下“评估者”或者其他类似的东西。
  • 当您接收到矩阵时,您可以存储this.Matrix.GetLength(0)的值并使用它,而不是多次调用它。因为您知道矩阵总是NxN,所以您知道第一维长度总是匹配于第二维长度(换句话说,this.Matrix.GetLength(1) == this.Matrix.GetLength(0))。
  • 您没有检查矩阵是否实际上是NxN矩阵。如果用户通过了一个4x3呢?
  • 你做了相当多的复制,却没有很大的好处。如果一行、列或对角线中的每一项都是相同的,那么您就知道这是一场胜利。所以,看看一个字符是否与它旁边的那个匹配。如果没有,那你就知道不会有胜利。如果是匹配的,检查下一个。您不需要将其复制到另一个数组中,而do则是不同的。如果这样做,您将大大减少代码行和复杂性。也许是这样的:if (matrix[i,j] != matrix[i + 1,j]) return false;显然需要一些边界检查,等等。
  • 除了MatrixHasWinner之外,您的代码中没有任何东西表明这是为了玩toe游戏。您使用的是非常通用的名称(矩阵、向量),而不是为其分配任何上下文。基质可能是GameBoard。向量可以是行或列(不过,正如我前面所说,您并不真正需要它)。
票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/243451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档