代码之家  ›  专栏  ›  技术社区  ›  Narek

在C语言中初始化List<List<Coordinate>>的优雅方式#

  •  1
  • Narek  · 技术社区  · 8 年前

    我想在C#中的3x3 2D矩阵上查看tic tac toe的胜利。我的结构如下:

    public struct GridCoordinates {
        public int row;
        public int column;
    }
    

    为了检查电路板的所有行、列和对角线,我可以执行以下操作。我可以创建一个预先计算好的 List<List<GridCoordinates>> 其中,内部列表是3个坐标的集合,每个坐标代表一行、一列或一条对角线。但当我想到这需要多长时间 列表<列表<网格坐标>> 将使用 new s、 我开始觉得在C#中应该有更好的方法。请建议如何优雅地填充预先计算的坐标。

    我的BG来自C++,我可以做这样的事情:

    #include <iostream>
    #include <vector> using namespace std;
    
    struct GridCoordinates {
        int i;
        int j; 
     };
    
    vector<vector<GridCoordinates>> vec{
        {{0,0}, {0,1}, {0, 2}}
        };
    
    int main() {   std::cout << vec[0][2].j; }
    

    优雅,对吧?

    2 回复  |  直到 8 年前
        1
  •  0
  •   Matthew Watson    8 年前

    可以将(X,Y)元组的二维数组声明为类中的字段,如下所示:

    static readonly (int X, int Y)[,] winningLines =
    {
        {(0, 0), (0, 1), (0, 2)},
        {(0, 0), (1, 1), (2, 2)},
        {(0, 0), (1, 0), (2, 0)},
        {(0, 1), (1, 1), (2, 1)},
        {(0, 2), (1, 2), (2, 2)},
        {(1, 0), (1, 1), (1, 2)},
        {(2, 0), (1, 2), (2, 2)},
        {(0, 2), (1, 1), (2, 0)}
    };
    

    这使得声明数组相当简洁。然而,访问它仍然相当冗长,例如:

    using System;
    
    namespace Demo
    {
        class Program
        {
            static void Main()
            {
                Console.WriteLine(isWinFor('X')); // false
                Console.WriteLine(isWinFor('O')); // false
    
                board[0, 0] = 'X';
                board[1, 1] = 'X';
                board[2, 2] = 'X';
    
                Console.WriteLine(isWinFor('X')); // true
                Console.WriteLine(isWinFor('O')); // false
            }
    
            static bool isWinFor(char player)
            {
                for (int line = 0; line < winningLines.GetUpperBound(0); ++line)
                {
                    bool won = true;
    
                    for (int coord = 0; coord < 3; ++coord)
                    {
                        var p = winningLines[line, coord];
    
                        if (board[p.X, p.Y] != player)
                            won = false;
                    }
    
                    if (won)
                        return true;
                }
    
                return false;
            }
    
            static readonly char[,] board = new char[3,3];
    
            static readonly (int X, int Y)[,] winningLines =
            {
                {(0, 0), (0, 1), (0, 2)},
                {(0, 0), (1, 1), (2, 2)},
                {(0, 0), (1, 0), (2, 0)},
                {(0, 1), (1, 1), (2, 1)},
                {(0, 2), (1, 2), (2, 2)},
                {(1, 0), (1, 1), (1, 2)},
                {(2, 0), (1, 2), (2, 2)},
                {(0, 2), (1, 1), (2, 0)}
            };
        }
    }
    

    (注意:元组支持需要c#7.0或更高版本。)

        2
  •  0
  •   Dharani Kumar    8 年前

    结构应该是GridCoordinate(单数)(我相信这些东西),

    以下是我要做的保持清洁

    有一门叫做坐标的课

    public class Coordinate
    {
         int row;
         int column;
         bool status;
    }
    

    有一排坐标

    public class Row
    {
         int length;
         List<Coordinate> coOrds;
    }
    

    最后有一个叫做Grid的类

    public class Grid    
    {
         int cols;
         int rows;    
         List<Row> rows;
    }
    

    现在,根据动态需要的列和行添加行。

    这样一来,它就可以扩展且干净。。