从Vectores的向量创建一个板

Create a board from a vector of vectores

本文关键字:一个 Vectores 向量 创建      更新时间:2023-10-16

我遇到了一些问题,创建了向量的向量

我需要

之类的东西
  a b c d e f g h i j 
A . . . . . . . . . .
B . . . . . . . . . .
C . . . . . . . . . .
D . . . . . . . . . .
E . . . . . . . . . .
F . . . . . . . . . . 
G . . . . . . . . . .
H . . . . . . . . . .
I . . . . . . . . . .
J . . . . . . . . . .

大小可以更改(取决于我已经在构造函数上"提取"的一些TXT信息)。我尝试了这样的事情:

 void Board::display() const{
//string filename;
Board b1("configp1.txt");
int upC = 65, lowC = 97; //ascii code for low a and upper A
int icc = 0; //int char coluna
cout << b1.numLines << endl;
cout << b1.numColumns << endl;
cout << "lol" << endl;
for(int i = 0; i < b1.numLines; i++){
    for (int j = 0; j < b1.numColumns; j++){
        board[i][j] == '.';
    }
}

while (icc < b1.numLines){ //o int char coluna vai aumentando consoante as dimensoes do tabuleiro || e.g Se tivermos um tab 10x10
    cout << (char)lowC << " ";                      //teremos um range de 65-75 [a-j] para as colunas e um range 97 -107 [A-J] para as linhas
    icc++;
    lowC++;
}
cout << endl;
icc = 0;
for (int y = 0; y<b1.numLines; y++)
{
    cout << (char)upC << " ";
    for (int x = 0; x < b1.numColumns; x++){
        cout << board[y][x] << " ";
    }
    cout << endl;
    upC++;
}

董事会构造函数:

Board::Board(const string &filename){

string nome;
unsigned int size;
char simb;
unsigned int cor;
char ori;
PositionChar position;

string tmp;
ifstream config;
config.open(filename.c_str()); //abre o ficheiro config onde estao as informacoes do tabuleiro
if (config.is_open()) {
    config >> tmp >> numLines >> tmp >> numColumns;
    while (!config.eof()) {
        config >> simb >> tmp >> position.lin >> position.col >> tmp >> ori >> tmp >> size >> tmp >> cor;
        if(!config.fail()){
            ships.push_back(Ship(simb, position, ori, size, cor));
        }

    }
}
else{
    cout << "Ficheiro de config invalido" << endl;
    exit(1);
}
config.close();

}

董事会课程:

    class Board {
public:
    Board(const string &filename);
    int putShip(const Ship &s);
    void moveShips();
    //bool checkLimits()
    //bool attack(const Bomb &b);
    void display()const;
    void show()const;
    int getLines();
    int getColumns();
private:
    int numLines, numColumns;
    vector<Ship> ships;
    vector <vector <int> > board;
};

,但根本什么也没发生。

有人可以帮我吗?

最好的问候

您的板定义为向量的向量: vector <vector <int> > board;

但是在构造函数中,您不初始化它。您也没有在显示功能中。所以它是空的。在这种情况下,尝试访问board[i][j]将超出范围。

解决方案:

我建议在构造函数中建议,一旦知道尺寸,就可以用:

调整您的向量大小
    board.resize(numLines, vector<int> (numColumns,'.'));

其他问题:

请注意,您打算修改board时,以下代码不应在display() const中:

for(int i = 0; i < b1.numLines; i++){
    for (int j = 0; j < b1.numColumns; j++){
        board[i][j] == '.';    /// <=======OOOPS !  Won't change anything this is comparison
    }
}

这可能仅是因为您写了==,因此将board[i][k]'.'进行比较而不更改。这可以解释为什么什么都没有发生(显示0不会产生任何可见的东西)。

请注意,Board::display()中还有另一个潜在问题。您可以在其中创建局部变量Board b1,以便使用b1.numLinesb1.numColumns。这很容易出错:您应该摆脱这种无用的b1,直接参考numLinesnumColumns,它们都是构件变量,具有确保在其自己的构造函数中为对象设置的值。