0

我正在尝试制作以下 C# 代码的 C++ 等效代码,主要是因为我正在学习 C# 中的教程,而且我更喜欢使用 C++。我的思考过程可能是制作一个外部数组,然后将一个新数组分配到代表矩阵大小的每个索引中。这可以吗,还是有更好的方法可以在 C++ 中实现它?

// current C# code
List<int[,]> pieces;

pieces = new List<int[,]>();

/* I Piece */
pieces.Add(new int[4, 4] {
    {0, 0, 0, 0},
    {1, 1, 1, 1},
    {0, 0, 0, 0},
    {0, 0, 0, 0}
});

/* J Piece */
pieces.Add(new int[3, 3] {
    {0, 0, 1},
    {1, 1, 1},
    {0, 0, 0}
});

/* O Piece */
pieces.Add(new int[2, 2] {
    {1, 1},
    {1, 1}
});

/* S Piece */
pieces.Add(new int[3, 3] {
    {0, 1, 1},
    {1, 1, 0},
    {0, 0, 0}
});


/* T Piece */
pieces.Add(new int[3, 3] {
    {0, 1, 0},
    {1, 1, 1},
    {0, 0, 0}
});

/* Z Piece */
pieces.Add(new int[3, 3] {
    {1, 1, 0},
    {0, 1, 1},
    {0, 0, 0}
});

我的初始代码是在数组中创建一个矩阵。由于我不想改变游戏本身的“棋子”数量,所以固定数组应该没问题。所以这就是我写的等价物。显然,它不起作用,说它不能分配给 int 的实体:

int pieces [7];

//I piece
pieces [0] = new int [4][4];
pieces [0] = {
                {0, 0, 0, 0},
                {1, 1, 1, 1},
                {0, 0, 0, 0},
                {0, 0, 0, 0}
             }

/* And so on for each piece */
4

2 回答 2

3

对于这样一个简单的构造,IMO 一个向量的向量似乎有点麻烦。

我可能会假设所有东西都是 4x4,最大的尺寸,然后将碎片嵌入其中,在这种情况下,您将拥有

#include <vector>
struct Piece { int v[4][4]; };
std::vector<Piece> pieces { 
     // I piece
     { {
        {0,0,0,0},
        {1,1,1,1},
        {0,0,0,0},
        {0,0,0,0}
     } },
     // J piece
     { {
       {0, 0, 1},  // This is OK since extra element will be 0 initialised by default.
       {1, 1, 1},
       {0, 0, 0}
       // Dont need a row here either as it will be 0 initialised.
     } }
     //Others omitted for now..
  };

或者对于非 C++11 兼容的编译器:

#include <vector>
struct Piece { int v[4][4]; };
Piece raw_pieces[] = {
     { {
        {0,0,0,0},
        {1,1,1,1},
        {0,0,0,0},
        {0,0,0,0}
     } },
     { {
       {0, 0, 1},  // This is OK since extra element will be 0 initialised by default.
       {1, 1, 1},
       {0, 0, 0}
       // Dont need a row here either as it will be 0 initialised.
     } }
};


std::vector<Piece> pieces(raw_pieces, raw_pieces + sizeof(raw_pieces)/sizeof(Piece));
于 2013-03-13T07:01:26.793 回答
2

尝试std::list<std::vector<std::vector<int> > >或按照建议尝试 std::vector<std::vector<std::vector<int> > >

这是一个完整的示例vector<vector<vector<int>>>,但请记住,您必须像这样编译:g++ -std=c++0x t.cpp

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<vector<vector<int>>> A = {
      {
        {0, 0, 0, 0},
        {1, 1, 1, 1},
        {0, 0, 0, 0},
        {0, 0, 0, 0}
      },
      {
        {0, 0, 1},
        {1, 1, 1},
        {0, 0, 0}
      },
      {
        {1, 1},
        {1, 1}
      },
      {
        {0, 1, 1},
        {1, 1, 0},
        {0, 0, 0}
      },
      {
        {0, 1, 0},
        {1, 1, 1},
        {0, 0, 0}
      },
      {
        {1, 1, 0},
        {0, 1, 1},
        {0, 0, 0}
      }
    };


    for(int i=0;i<A.size();i++) {
      for(int j=0;j<A[i].size();j++) {
        for(int k=0;k<A[i][j].size();k++) {
          std::cout<<A[i][j][k];
        };
        std::cout<<std::endl;
      };
      std::cout<<std::endl;
    };
}

或者,如果您不喜欢一次性初始化所有内容,并且希望在运行时进行。你可以随时使用自带的push_back方法std::vector

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<vector<vector<int>>> A;
    A.push_back(vector<vector<int>>(
      {
        {0, 0, 0, 0},
        {1, 1, 1, 1},
        {0, 0, 0, 0},
        {0, 0, 0, 0}
      }
    ));


    A.push_back(vector<vector<int>>(
      {
        {0, 0, 1},
        {1, 1, 1},
        {0, 0, 0}
      }
    ));

    A.push_back(vector<vector<int>>(
      {
        {1, 1},
        {1, 1}
      }
    ));

    A.push_back(vector<vector<int>>(
      {
        {0, 1, 1},
        {1, 1, 0},
        {0, 0, 0}
      }
    ));


    A.push_back(vector<vector<int>>(
      {
        {0, 1, 0},
        {1, 1, 1},
        {0, 0, 0}
      }
    ));

    A.push_back(vector<vector<int>>(
      {
        {1, 1, 0},
        {0, 1, 1},
        {0, 0, 0}
      }
    ));


    for(int i=0;i<A.size();i++) {
      for(int j=0;j<A[i].size();j++) {
        for(int k=0;k<A[i][j].size();k++) {
          std::cout<<A[i][j][k];
        };
        std::cout<<std::endl;
      };
      std::cout<<std::endl;
    };
}
于 2013-03-13T06:32:08.763 回答