random

Yes I know the title seems a bit awkward.

1
2
3
4
5
6
7
8
9
10
11
  #include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include <stdlib.h>
#include <random>
using namespace std;

	system("pause");
	return 0;
}
Last edited on
at least it is a bit interesting.

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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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_alias
constexpr std::size_t N = 10 ;
using mine_fld_type = char[N][N] ; // NxN characters

constexpr char mine = 'M' ;
constexpr char 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)

    static constexpr std::size_t min_mines = 10 ;
    static constexpr std::size_t max_mines = 16 ;
    // http://en.cppreference.com/w/cpp/language/static_assert
    static_assert( min_mines < max_mines && max_mines < N*N, "sanity check failed" ) ;

    // http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
    static 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/begin
    const auto 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( const auto& 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) ) ;
}

http://coliru.stacked-crooked.com/a/3f62a17916990d0a
http://rextester.com/TMXNV63587
Topic archived. No new replies allowed.