2016-01-20 65 views
1

我正在尝试创建一个简单的扫雷游戏,并在创建板子时遇到了一些问题。我使用2D矢量来代替2D阵列,并且无法递增贴图值,以查看贴图旁边有多少个地雷。二维矢量增量错误

int Boardsize::createBoard() const { 
// vector < vector<Tile> > board; 
impl->board.resize(getLength(), vector<Tile>(getWidth(), Tile())); 
for (int i = 0; i < getMines(); i++) { 
    int v1 = rand() % getLength(); 
    int v2 = rand() % getWidth(); 
    if (impl->board[v1][v2].getMine() == true) i--; 
    else {impl->board[v1][v2].setMine(true); 
      if (v1 - 1 > -1) impl->board[v1-1][v2]++; 
      if (v1 + 1 < getLength()) impl->board[v1+1][v2]++; 
      if (v2 - 1 > -1) impl->board[v1][v2-1]++; 
      if (v2 + 1 < getWidth()) impl->board[v1][v2+1]++; 
      if ((v1 - 1 > -1) && (v2 - 1 > -1)) impl->board[v1-1][v2-1]++; 
      if ((v1 - 1 > -1) && (v2 + 1 < getWidth())) impl->board[v1-1][v2+1]++; 
      if ((v1 + 1 < getLength()) && (v2 - 1 > -1)) impl->board[v1+1][v2-1]++; 
      if ((v1 + 1 < getLength()) && (v2 + 1 < getWidth())) impl->board[v1+1][v2+1]++; 
     } 
    } 
} 

值的长度,宽度和地雷被设置的时间提前。我打算工作的方式是“检查getMine = true,如果是,那么游戏结束,如果不是,isRevealed设置为true,瓦片显示瓦片附近有多少个地雷,但是,我得到错误:。

error: no 'operator++(int)' declared for postfix '++' [-fpermissive]| 

我是否需要设置一个单独的功能来增加内容,我假设board.resize填充矢量全0的 我感谢帮助

这里是“瓷砖“文件的内容:

namespace Minesweeper { 
using namespace std; 

class Tile::Tilement { 
    int status; 
    bool mine; 
    int Adjmines; 
    friend class Tile; 
public: 
    Tilement() 
    : status(0), mine(false), Adjmines(0) 
    { 
    } 
}; 

Tile::Tile() { 
    cout << "Tile is being created" << endl; 
} 
Tile::~Tile() { 
    cout << "Tile is being deleted" << endl; 
} 

void Tile::setMine(int a) { 
    tint->mine = true; 
} 

void Tile::setStatus(int a) { 
    if ((a == 0) || (a == 1) || (a == 2)) { 
     tint->status = a; 
    } 
    else { 
     #ifdef DEBUG 
     cout << "Tile status invalid" << endl; 
     #endif // DEBUG 
     throw invalid_argument("Invalid tile status"); 
    } 
} 

//void Tile::setContent(char r) { 
// tint->content = r; 
//} 

int Tile::getStatus() const { 
    return tint->status; 
} 

char Tile::getAdjcount() const { 
    return tint->Adjmines; 
} 

char Tile::getMine() const { 
    return tint->mine; 
} 

int Tile::setAdjmines(int a) { 
    a = a++; 
} 

char Tile::getContent() const { 
    if (Tile::getMine() == true) { 
     return tint->mine; 
    } 
    else return tint->Adjmines; 
} 

编辑:我已经改变了增量mentations一下,让他们现在是这样的:

if (v1 - 1 > -1) impl->board[v1-1][v2].incAdjmines; (etc). 

而且incAdjmines功能如下:

int Tile::incAdjmines() { 
Adjmines = Adjmines + 1; 
} 

而且......嗯,代码编译,如果不出意外,但由于其他代码片段中的某些错误,我无法判断它是否正常工作。谢谢大家迄今为止的帮助。

+0

我们展示瓷砖请 –

+0

那么你确实是尝试“叫”'瓦::操作++(INT)'。如果增加一个'Tile'对象,你会发生什么? –

+0

您不使用任何名为“isRevealed”的东西。你打算写'impl-> board [v1-1] [v2] .isRevealed = true;'? – molbdnilo

回答

3

您正在致电++Tile对象似乎没有这个操作符过载。 您可以通过为Tile类重载此运算符来解决您的问题。或者直接告诉你想增加例如哪个变量:

impl->board[v1-1][v2].cout_of_things++