什么导致了我的segfault当我索引这个二维向量?

huangapple go评论78阅读模式
英文:

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-&gt;scoremap[x][y];, I am getting a segfault. Can anyone explain what I may be doing wrong?

#include &quot;minesweeper.h&quot;
using namespace std;
Minesweeper::Minesweeper() {
// Easy difficulty default values
map_size = 5;
mines = 8;
score = 0;
// Create displaymap &amp; scoremap based on map_size
// displaymap is chars, initialized to quesiton marks (?)
// scoremap is ints, initliazed to zeros (0)
vector&lt;vector&lt;char&gt;&gt; displaymap(map_size, vector&lt;char&gt;(map_size, &#39;?&#39;));
vector&lt;vector&lt;int&gt;&gt; scoremap(map_size, vector&lt;int&gt;(map_size, 0));
};
// Generate mines / score tiles on the scoremap
void Minesweeper::generate_scoremap() {
// First, generate mines on map
int mine_count = this-&gt;mines;
// While we have mines to place
while (mine_count != 0) {
// Get a random position on the map
pair&lt;int,int&gt; position = random_position(this-&gt;map_size);
int x = position.first;
int y = position.second;
// If it&#39;s not already a mine (mines = -1, other tiles = how many mines are touching the tile)
int tile = this-&gt;scoremap[x][y];
if (tile != -1) {
tile = -1;
this-&gt;scoremap[x][y] = tile;
mine_count--;
}
else {
// find a new spot for this mine
}
}
// Next, set score of each tile that isn&#39;t a mine
// Direciton pairs for checking adjacent tiles
vector&lt;std::pair&lt;int, int&gt;&gt; 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 &lt; this-&gt;map_size; i++) {
for (int j = 0; j &lt; this-&gt;map_size; j++) {
int tilescore = 0;
// Check to make sure it isn&#39;t a mine
if (this-&gt;scoremap[i][j] != -1) {
// Check all adjacent cells for mines
for (const auto&amp; 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-&gt;map_size)) {
// and its a mine, increase tile score
if (this-&gt;scoremap[newi][newj] == -1) {
tilescore++;
}
}
}
// Set tile score on scoremap
this-&gt;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 &amp; scoremap based on map_size
// displaymap is chars, initialized to quesiton marks (?)
// scoremap is ints, initliazed to zeros (0)
displaymap.resize(map_size, vector&lt;char&gt;(map_size, &#39;?&#39;));
scoremap.resize(map_size, vector&lt;int&gt;(map_size, 0));
};

huangapple
  • 本文由 发表于 2023年5月25日 01:26:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定