unsigned int Game::coordstofieldindex(CoordinateSet pos) { unsigned int idx = 0; SizeVectorIt i; CoordinateSetIt j; for (i = this->dimensions.begin(), j = pos.begin(); i != this->dimensions.end() && j != pos.end(); ++i, ++j) { assert(*j < *i); idx *= *i; idx += *j; } return idx; }
void Game::pressborders() { CoordinateSetList::const_iterator i; for (i = coordbegin(); i != coordend(); ++i) { CoordinateSet c = *i; CoordinateSetIt x; SizeVectorIt s; bool isborder = false; for (x = c.begin(), s = dimensions.begin(); x != c.end() && s != dimensions.end(); ++x, ++s) { if (*x == 0 || *x+1 == *s) { isborder = true; break; } } if (isborder) { Tile *tile = getTile(c); if (!tile->amIDeadNow()) amIDeadNow(c); } } }
void Game::deploythemines(int mines) { std::vector<unsigned int> minelist; std::vector<unsigned int>::iterator minelistit, minelistit2; minelist.reserve(mines); while (mines-- > 0 && this->tilecount > minelist.size()) { unsigned int loc = ((unsigned int) rand()) % (this->tilecount-minelist.size()); std::sort(minelist.begin(), minelist.end()); for (minelistit = minelist.begin(); minelistit != minelist.end(); ++minelistit) { if (loc >= *minelistit) ++loc; } assert(loc < this->tilecount); if (opts.noborderbombs) { CoordinateSet c = fieldindextocoords(loc); SizeVectorIt i; CoordinateSetIt j; bool border = false; for (i = dimensions.begin(), j = c.begin(); i != dimensions.end() && j != c.end(); ++i, ++j) { if (*j == 0 || *j+1 == *i) { border = true; break; } } if (border) { ++mines; continue; } } minelist.push_back(loc); } for (minelistit = minelist.begin(); minelistit != minelist.end(); ++minelistit) { this->tiles[*minelistit]->setBlarg(true); PTileSet neighbours = this->neighbourhood(this->fieldindextocoords(*minelistit)); PTileSetIt i; for (i = neighbours.begin(); i != neighbours.end(); ++i) { assert(*i != NULL); (*i)->addNeighbouringBomb(); } } }