关于2048游戏,建立二维数组的问题
列出一部分代码好了,加上说明太长了,也不方便大家读报错:Terminated due to signal: SEGMENTATION FAULT (11)
我在网上查了一下,有可能是数组越界的问题,但是函数产生的数组数字都是0到3之间的
头文件的声明就不写了
大致是这个样子
int main(int argc, char *argv[]) {
int Game[4][4];
initializeGame(Game);
startGameBuilding(Game);
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cout << fixed << setprecision(5) << Game[i][j];
}
}
}
void initializeGame(int (*Game)[4]) // 初始化数组
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
Game[i][j] = 0;
}
}
}
void startGameBuilding(int (*Game)[4]) // 随机选择两个不同的地方产生一个2
{
int a, b, c, d;
srand((unsigned)time(NULL));
a = rand() % 4;
b = rand() % 4;
c = rand() % 4;
d = rand() % 4;
checkStartGameBuilding(a, b, c, d);
Game[a][b] = 2;
Game[c][d] = 2;
}
void checkStartGameBuilding(int a, int b, int c, int d) // 检查随机产生的两组是否刚好在同一个地方
{
srand((unsigned)time(NULL));
if(a == c && b == d)
{
c = rand() % 4;
d = rand() % 4;
}
checkStartGameBuilding(a, b, c, d);
}
int Game[4][4];
initializeGame(Game);
startGameBuilding(Game);
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cout << fixed << setprecision(5) << Game[i][j];
}
}
}
void initializeGame(int (*Game)[4]) // 初始化数组
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
Game[i][j] = 0;
}
}
}
void startGameBuilding(int (*Game)[4]) // 随机选择两个不同的地方产生一个2
{
int a, b, c, d;
srand((unsigned)time(NULL));
a = rand() % 4;
b = rand() % 4;
c = rand() % 4;
d = rand() % 4;
checkStartGameBuilding(a, b, c, d);
Game[a][b] = 2;
Game[c][d] = 2;
}
void checkStartGameBuilding(int a, int b, int c, int d) // 检查随机产生的两组是否刚好在同一个地方
{
srand((unsigned)time(NULL));
if(a == c && b == d)
{
c = rand() % 4;
d = rand() % 4;
}
checkStartGameBuilding(a, b, c, d);
}