1

I'm trying to create a simple minesweeper game, and have some issues with creating the board. I am using a 2d vector in lieu of a 2d array and am having trouble incrementing the tiles value to see how many mines are adjacent to the tile.

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]++;
        }
    }
}

Values length, width and mines are set ahead of time. The way I intend it to work is "Check if getMine = true, if yes then game over. If no, isRevealed is set to true and the tile shows how many mines are adjacent to the tile. However, I'm getting the error:

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

Do I need to set a seperate function to increment the content? I am assuming that the board.resize fills the vector full of 0's. I appreciate the help.

Here is the "Tile" file's contents:

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;
}

EDIT: I've changed the incrementations a bit so that they're now like this:

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

And the incAdjmines function looks like this:

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

And...well, the code compiled, if nothing else, but due to some errors in another fragment of code, I can't tell if it works correctly. Thank you all for your help so far.

Anstane
  • 177
  • 9

1 Answers1

3

You are calling ++ on Tile object which seems with no overload for this operator. You can solve your problem by overloading this operator for Tile class. Or by directly telling which variable you want to increase for example:

impl->board[v1-1][v2].cout_of_things++
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160