我正在尝试做类似的事情:
#include <iostream>
#include <vector>
#include <ctime>
class Clickomania
{
public:
Clickomania();
std::vector<std::vector<int> > board;
};
Clickomania::Clickomania()
: board(12, std::vector<int>(8,0)) <<<<<<<
{
srand((unsigned)time(0));
for(int i = 0; i < 12; i++)
{
for(int j = 0; j < 8; j++)
{
int color = (rand() % 6) + 1;
board[i][j] = color;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,显然我不能用这种方式初始化矢量的"板"矢量.
如何创建二维矢量类型的公共成员并正确初始化它?
如果我有一个4x4游戏板,我在我的程序中表示为16d的1d整数数组.
如何获得任何给定索引上方,下方,左侧和右侧的方块索引?
所以,例如:
A = { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 }
Run Code Online (Sandbox Code Playgroud)
代表这个董事会
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Run Code Online (Sandbox Code Playgroud)
让我们说我目前在董事会中的索引#8(值= 7).如何得到4(值= 3),5(值= 6),10(值= 11)的索引,并意识到没有正方形,因为它位于电路板的右边缘.
我知道我需要使用一些模数学,但我没有想出正确的方法来获得相邻正方形的索引.
我在想......
if ((i % 4) + 1 < 3) right = i + 1;
if ((i % 4) - 1 > 0) left = i - 1;
if ((i % 4) + 4 < …Run Code Online (Sandbox Code Playgroud) 我在Puzzle.h中有以下内容
class Puzzle
{
private:
vector<int> puzzle;
public:
Puzzle() : puzzle (16) {}
bool isSolved();
void shuffle(vector<int>& );
};
Run Code Online (Sandbox Code Playgroud)
然后我的Puzzle.cpp看起来像:
Puzzle::Puzzle()
{
// Initialize the puzzle (0,1,2,3,...,14,15)
for(int i = 0; i <= puzzle.size(); i++)
{
puzzle[i] = i;
}
}
// ... other methods
Run Code Online (Sandbox Code Playgroud)
我在头文件中使用了错误的初始化程序列表吗?我想定义一个int的向量并将其大小初始化为16.我应该怎么做?
G ++输出:
Puzzle.cpp:16: error: expected unqualified-id before ')' token
Puzzle.cpp: In constructor `Puzzle::Puzzle()':
Puzzle.cpp:16: error: expected `)' at end of input
Puzzle.cpp:16: error: expected `{' at end of input
Puzzle.cpp: At global scope:
Puzzle.cpp:24: …Run Code Online (Sandbox Code Playgroud)