Need it to be a bool instead of Void.

I have a minesweeper program. What is happening is that there is a 5x5 array of * like so,
*****
*****
*****
*****
*****
And it is using the loop to assign how ever many mines the user enters to a a random spot on the array.I need the assignMines function to be a bool. I currently have it as a void. I'm not good with bool. I do know that it returns true or false. And also, sometimes it will only put 1 less amount of mines because the random spot it generates already has a mine so it just places it over it. So I also need help on where and what type of loop I should use.

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
enum values {INITIAL = '*', MINE = '@'};
const int row = 5;
const int column = 5;

void assignMines(values array[row][column]);

int main()
{
    srand(time(NULL));
    
    int minecount;
    
    cout << "Enter number of mines to place on board (5 - 10):";
    cin >> minecount;

    values array[row][column];

    for (int i=0;i<minecount;i++)
    {
        assignMines(array);
        
    }
    return 0;
}
void assignMines(values array[row][column])
{
        array[rand()%5][rand()%5] = MINE;
    
}
Last edited on
I didn't include all the code. Only the part that I thought would be needed.
1. Use std::map<size_t, std::pair<size_t,size_t>> to map natural numbers to the 2D array positions
2. std::iota a std::vector<size_t> with the #elements of the 2D array
3. std::shuffle this vector and read off it's first n elements – where n is the number of mines – this will ensure that each mine is placed at a unique spot
4. wrap this code into a function returning bool though I'm not sure what the return value is signifying – if the function is written properly it should always return true
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <tuple>
#include <chrono>
#include <random>

constexpr auto SIZE = 5;

int main()
{
    std::map<size_t, std::pair<size_t, size_t>> numToMatrix{};
    size_t k{};
    for (size_t i = 0; i < SIZE ; ++i)
    {
        for (size_t j = 0; j < SIZE; ++j)
        {
            numToMatrix[k] = std::make_pair(i, j);
            ++k;
        }
    }

    std::vector<size_t> data((std::pow(SIZE,2)) + 1 );
    std::iota(data.begin(), data.end(), 0);
    auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
    std::shuffle (data.begin(), data.end(), std::default_random_engine(seed));

    std::cout << "How many mines? \n";
    size_t choice;
    std::cin >> choice;
    char mineSweeperArray [SIZE ][SIZE ];
    for (size_t i = 0; i < SIZE; ++i)
    {
        for (size_t j = 0; j < SIZE; ++j)
        {
            mineSweeperArray[i][j] = '*';
        }
    }

    for (size_t i = 0; i < choice; ++i)
    {
        mineSweeperArray[numToMatrix.find(data[i]) -> second.first][numToMatrix.find(data[i])->second.second] = '@';
    }
    for (size_t i = 0; i < SIZE; ++i)
    {
        for (size_t j = 0; j < SIZE; ++j)
        {
            std::cout << mineSweeperArray[i][j] << " ";
        }
        std::cout << "\n";
    }
}

one final point – instead of a 2D C-style array, a std::vector<std::vector<char>> would also be a better choice
I think the assignMines function is OK as void. It is the minefield array that needs to be of bool type - each point is either mined (true) or not (false).


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
70
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

const char INITIAL = '.';
const char MINE    = '@';
const int  ROW     =  5 ;
const int  COL     =  5 ;

bool mineArray[ROW][COL];         // Minefield represented by bool (true or false) array

void initialise();
void assignMines( int num );
void printMines();

//======================================================================

int main()
{
   srand(time(NULL));
   
   int minecount;
   cout << "Enter number of mines to place on board (5 - 10):";
   cin >> minecount;

   initialise();
   assignMines( minecount );
   printMines();
}

//======================================================================

void initialise()
{
   for ( int i = 0; i < ROW; i++ )
   {
      for ( int j = 0; j < COL; j++ ) mineArray[i][j] = false;
   }
}

//======================================================================

void assignMines( int num )
{
   int count = 0;
   while ( count < num )
   {
      int i = rand()%ROW;
      int j = rand()%COL;
      if ( !mineArray[i][j] )     // If not already mined ...
      {
         mineArray[i][j] = true;  // ... then add a mine
         count++;
      }
   }
}

//======================================================================

void printMines()
{
   for ( int i = 0; i < ROW; i++ )
   {
      for ( int j = 0; j < COL; j++ ) cout << ( mineArray[i][j] ? MINE : INITIAL );
      cout << endl;
   }
}

//====================================================================== 


Enter number of mines to place on board (5 - 10): 5
....@
.@...
.....
...@.
@@...

Last edited on
The function is supposed to be a bool. Here is the part that says so.
In the code I attached,
values array[row][column]; is correct


You will then assign the number of mines between 5 and 10, inclusively, specified by the user to randomly generated locations on the board using a bool function, passing in the two-dimensional array and the size. This function will generate a random row- column position and then attempt to place a mine at that row-column position on the board. Since the row-column position is randomly generated, if the function attempts to place a mine on a square that already contains a mine, the method will return false, indicating that the assignment was not successful. If the square, however, does not already contain a mine, then the function will assign a mine to that location by assigning that row-column position with the enumerated type representing the mine and return true, indicative that the assignment was successful. Note that this function attempts to place only one (1) mine at a randomly generated location on the board, so a loop should be implemented in the calling function to achieve this functionality.
Last edited on
Your instructions are alluding to a function that attempts successfully, or unsuccessfully place one mine at a time.

Taking lastchance's suggested code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//  New function to attempt to place one mine
bool place_one_mine ()
{  int i = rand()%ROW;
    int j = rand()%COL;
    if (mineArray[i][j])     // already mined ...
        return false;          // Can't place a mine there
    mineArray[i][j] = true;  // ... then add a mine
    return true;  // Successful
}

void assignMines( int num )
{  int count = 0;
   while (count < num)
   {  if (place_one_mine ())
         count++;
   }
}
Topic archived. No new replies allowed.