roughly (you will want to clean this up and integrate the idea into your code)
num_mines = rand()%6 +10;
vector<bool> minez(100); //100 is board locations,use a const/variable/something else here.
for(i = 0 i < minez.size(), i++) //all the board locations, in your case, 100
{
if(i < num_mines)
minez[i] = true;
else
minez[i] = false;
}
randomshuffle (minez );
for(i = 0 i < minez.size(), i++) //all the board locations, in your case, 100
{
if (minez[i] == true)
board[i/10][i%10] is a mine //this expression will change for other board sizes.
}
there are many other ways to do this, including doing it without the temporary vector.
#include <iostream>
#include <algorithm>
#include <random>
#include <ctime> // required only for GNU/MinGW
#include <iterator>
// using a type alias for the array makes the code simpler
// http://en.cppreference.com/w/cpp/language/type_aliasconstexpr std::size_t N = 10 ;
using mine_fld_type = char[N][N] ; // NxN characters
constexprchar mine = 'M' ;
constexprchar not_mine = '.' ;
mine_fld_type& init( mine_fld_type& fld ) // pass the NxN array by reference
{
// http://en.cppreference.com/w/cpp/numeric/random
// note: it is a good idea to get familiar with the C++ random number facilities
static std::mt19937 rng( std::random_device{}() ) ;
// static std::mt19937 rng( std::time(nullptr) ) ; // GNU/MinGW (fix for a poor implementation)
staticconstexpr std::size_t min_mines = 10 ;
staticconstexpr std::size_t max_mines = 16 ;
// http://en.cppreference.com/w/cpp/language/static_assertstatic_assert( min_mines < max_mines && max_mines < N*N, "sanity check failed" ) ;
// http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distributionstatic std::uniform_int_distribution<std::size_t> distrib( min_mines, max_mines ) ;
const std::size_t num_mines = distrib(rng) ; // generate random number of mines
// note: the following code works because an array contains contiguously allocated objects
// char[N][N] : N contiguously allocated objects of type char[N]
// making, in all, NxN contiguously allocated objects of type char
// place mines in the first num_mines positions of the fld
// http://en.cppreference.com/w/cpp/algorithm/fill_n
// http://en.cppreference.com/w/cpp/iterator/beginconstauto iter = std::fill_n( std::begin( fld[0] ), num_mines, mine ) ;
// and no mines in the remaining N*N - num_mines positions of the fld
std::fill_n( iter, N*N - num_mines, not_mine ) ;
// shuffle the N*N characters in the fld to bring the mines into random positions
// http://en.cppreference.com/w/cpp/iterator/end
// http://en.cppreference.com/w/cpp/algorithm/random_shuffle
// note: avoid the deprecated (and now removed) std::random_shuffle
std::shuffle( std::begin( fld[0] ), std::end( fld[N-1] ), rng ) ;
return fld ;
}
void print( const mine_fld_type& fld )
{
// http://www.stroustrup.com/C++11FAQ.html#auto
// note: good idea to let the compiler figure out what the type is (it won't make a mistake)
// http://www.stroustrup.com/C++11FAQ.html#for
// note: good idea to favour range-based loops over classical for loops
for( constauto& row : fld ) // for each row in fld
{
for( char c : row ) std::cout << c << ' ' ; // print each char in row
std::cout << '\n' ;
}
}
int main()
{
mine_fld_type fld ;
print( init(fld) ) ;
}