英文:
What is causing my segfault when indexing this 2D vector?
问题
当我使用以下代码行索引二维向量 scoremap
时:int tile = this->scoremap[x][y];
,我遇到了段错误。有人可以解释一下我可能做错了什么吗?
#include "minesweeper.h"
using namespace std;
Minesweeper::Minesweeper() {
// Easy difficulty default values
map_size = 5;
mines = 8;
score = 0;
// Create displaymap & scoremap based on map_size
// displaymap is chars, initialized to question marks (?)
// scoremap is ints, initialized to zeros (0)
vector<vector<char>> displaymap(map_size, vector<char>(map_size, '?'));
vector<vector<int>> scoremap(map_size, vector<int>(map_size, 0));
};
// Generate mines / score tiles on the scoremap
void Minesweeper::generate_scoremap() {
// First, generate mines on map
int mine_count = this->mines;
// While we have mines to place
while (mine_count != 0) {
// Get a random position on the map
pair<int,int> position = random_position(this->map_size);
int x = position.first;
int y = position.second;
// If it's not already a mine (mines = -1, other tiles = how many mines are touching the tile)
int tile = this->scoremap[x][y];
if (tile != -1) {
tile = -1;
this->scoremap[x][y] = tile;
mine_count--;
}
else {
// find a new spot for this mine
}
}
// Next, set score of each tile that isn't a mine
// Direction pairs for checking adjacent tiles
vector<std::pair<int, int>> directions =
{
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
// Check each tile and set its score
for (int i = 0; i < this->map_size; i++) {
for (int j = 0; j < this->map_size; j++) {
int tilescore = 0;
// Check to make sure it isn't a mine
if (this->scoremap[i][j] != -1) {
// Check all adjacent cells for mines
for (const auto& dir : directions) {
int newi = i + dir.first;
int newj = j + dir.second;
// If the index is in the map
if (index_valid(newi,newj,this->map_size)) {
// and its a mine, increase tile score
if (this->scoremap[newi][newj] == -1) {
tilescore++;
}
}
}
// Set tile score on scoremap
this->scoremap[i][j] = tilescore;
tilescore = 0;
}
}
}
}
我尝试手动创建具有正确字符和正确大小的二维向量,似乎可以工作,但我希望根据 map_size
变量动态生成二维向量(这里在构造函数中自动设置,但稍后我希望它是从用户选择的值或从选择特定难度级别获取的)。
英文:
When I am indexing the 2D vector scoremap
with the line: int tile = this->scoremap[x][y];
, I am getting a segfault. Can anyone explain what I may be doing wrong?
#include "minesweeper.h"
using namespace std;
Minesweeper::Minesweeper() {
// Easy difficulty default values
map_size = 5;
mines = 8;
score = 0;
// Create displaymap & scoremap based on map_size
// displaymap is chars, initialized to quesiton marks (?)
// scoremap is ints, initliazed to zeros (0)
vector<vector<char>> displaymap(map_size, vector<char>(map_size, '?'));
vector<vector<int>> scoremap(map_size, vector<int>(map_size, 0));
};
// Generate mines / score tiles on the scoremap
void Minesweeper::generate_scoremap() {
// First, generate mines on map
int mine_count = this->mines;
// While we have mines to place
while (mine_count != 0) {
// Get a random position on the map
pair<int,int> position = random_position(this->map_size);
int x = position.first;
int y = position.second;
// If it's not already a mine (mines = -1, other tiles = how many mines are touching the tile)
int tile = this->scoremap[x][y];
if (tile != -1) {
tile = -1;
this->scoremap[x][y] = tile;
mine_count--;
}
else {
// find a new spot for this mine
}
}
// Next, set score of each tile that isn't a mine
// Direciton pairs for checking adjacent tiles
vector<std::pair<int, int>> directions =
{
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
// Check each tile and set its score
for (int i = 0; i < this->map_size; i++) {
for (int j = 0; j < this->map_size; j++) {
int tilescore = 0;
// Check to make sure it isn't a mine
if (this->scoremap[i][j] != -1) {
// Check all adjacent cells for mines
for (const auto& dir : directions) {
int newi = i + dir.first;
int newj = j + dir.second;
// If the index is in the map
if (index_valid(newi,newj,this->map_size)) {
// and its a mine, increase tile score
if (this->scoremap[newi][newj] == -1) {
tilescore++;
}
}
}
// Set tile score on scoremap
this->scoremap[i][j] = tilescore;
tilescore = 0;
}
}
}
}
I tried manually creating the 2D vector with the right characters and right size, and it seems to work, but I would like the 2D vector to be generated dynamically according to whatever the map_size
variable is (here it's set automatically in the constructor, but later I would like it to be a value from the user, or from picking a specific difficulty level).
答案1
得分: 2
Minesweeper
的构造函数正在创建本地变量,这些本地变量遮蔽了相同名称的成员变量。因此,实际上您没有将任何数据放入成员变量中,以便稍后索引到。
请尝试使用以下代码替代:
Minesweeper::Minesweeper()
// Easy difficulty default values
map_size = 5;
mines = 8;
score = 0;
// Create displaymap & scoremap based on map_size
// displaymap is chars, initialized to question marks (?)
// scoremap is ints, initialized to zeros (0)
displaymap.resize(map_size, vector<char>(map_size, '?'));
scoremap.resize(map_size, vector<int>(map_size, 0));
};
英文:
Minesweeper
's constructor is creating local variables that shadow the member variables of the same names. So, you are not actually putting any data into the member variables that you can then index into later.
Try this instead:
Minesweeper::Minesweeper()
// Easy difficulty default values
map_size = 5;
mines = 8;
score = 0;
// Create displaymap & scoremap based on map_size
// displaymap is chars, initialized to quesiton marks (?)
// scoremap is ints, initliazed to zeros (0)
displaymap.resize(map_size, vector<char>(map_size, '?'));
scoremap.resize(map_size, vector<int>(map_size, 0));
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论