Random numbers in a array

Pages: 12
so in a minesweeper game, I am trying to set the mines on the array to be random, but this function just crashes the compiler

now minesweeper[row][col] is set as minesweeper[8][8] and -1 is the number on the array for a mine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void mine_set()
{ 
   int minesetter = 0;
do
	{
		row=rand()%row;
		col=rand()%col;
		if (minesweeper[row][col] !=-1)
			{
			minesweeper[row][col]=space;
			mines++;
			}
	}
		while (minesetter<mines); 
}
Crashes the compiler? You mean it gives you a compiler error? In that case it would be very helpful if you posted it.

If it crashes at runtime I would suspect row and col to be set incorrectly. Make sure they have the same value as the dimensions of the game board.

If mines start out below zero the loop will never run.
If mines start out above zero the loop will never stop.
Are you sure you want to increment mines inside the loop?
If it's a compiler error, I suspect that row and col are supposed to be const int variables.
The compiler runs the game, but when I select to play that board, it freezes

I'll try setting them as const ints next
Lines 6/7 should look something like this:
1
2
		row=rand()%8; // Note: The maximum allowed number
		col=rand()%8; // Note: The maximum allowed number 
set as const ints, same issue, can get into the main menu, freezes on selecting it, if I take out the Random function though, it works fine, so obviously there's something wrong there
Well, minesetter never changes, it remains 0. mines might increase. So as Peter87 said you'll have an infinite loop that freezes your program
oh so minesetter needs to be a global int? or am I missing something there?
oh so minesetter needs to be a global int?
Nope, global variable should be only the last resort. Pass it as a parameter:
1
2
void mine_set(int minesetter)
{

mines is a global variable. I would be better to make it local and pass it as a parameter as well. Global variables tend to have surprising values...

This minesetter<mines doesn't make too much sense when mines only increases
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void mine_set(int minesetter, int mines=10)
{ 
   
do
	{
		row=rand()%8;
		col=rand()%8;
		if (minesweeper[row][col] !=mine)
			{
			minesweeper[row][col]=mine;
			mines--;		
			}
		
	}
		while (minesetter<mines);
	
}


so like this?
so now I have that parameter set up


I get this error

C2660 " mine_set : function does not take 0 arguments"

what does that even mean?
When a function requires parameter you need to provide parameter. Without more context it's impossible to say what the problem is.
In my last post, I was suggesting that row and col were const int and that you were trying to change them in your code, which is illegal. Sorry if I wasn't clear.

Anyway, from the fact that they are not declared in your function, they must be global variables. Are they used in loop conditions when you populate your array?

If so, they really should be const int and this part

1
2
3
4
5
		row=rand()%8;
		col=rand()%8;
		if (minesweeper[row][col] !=mine)
			{
			minesweeper[row][col]=mine;

should be changed to

1
2
3
4
5
		int r=rand()%row;
		int c=rand()%col;
		if (minesweeper[r][c] !=mine)
			{
			minesweeper[r][c]=mine;
now when I've changed it I get

First-chance exception at 0x00D1982E in Project5.exe: 0xC0000094: Integer division by zero.

If there is a handler for this exception, the program may be safely continued.
You need to post your whole code.
ok, here it is (I cut out the other difficulties for now)

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<iostream>
#include<iomanip>
#include <stdio.h>      
#include <stdlib.h>     
#include <time.h>
#include <ctime>
#include<fstream>
using namespace std;
unsigned int Time = time(0);


int minesweeper[8][8];
						
int minesweeperI[16][16];
int minesweeperE[24][24];
char game[8][8];
char gameI[16][16];
char gameE[24][24];
int minesetter; 
int mines=0;
int guesses=0;
int row, col;
char square = 178, mine = 153,flag=214, rowline=179,space=' ';
char userchoice;
char play_again;


void initialise()
{
	int row,col;
	for (row=0;row<8;row++)
		for(col=0;col<8;col++)
			game[row][col]=square;


void mine_set()
{ 
   
do
	{
		int r=rand()%row;
		int c=rand()%col;
		if (minesweeper[r][c] !=mine)
			{
			minesweeper[r][c]=mine;
			mines--;		
			}
		
	}
		while (mines<10);
	
}



void drawGridB()
{    
	cout<<setw(18)<<"|0|1|2|3|4|5|6|7|"<<endl;
	
	
   for (row=0;row<8;row++)
	{
		cout<<setw(2)<<"------------------"<<endl;
		cout<<row<<rowline;
		for(col=0;col<8;col++)
			
		{
			
			cout<<game[row][col]<<rowline;
		}
			
		cout<<endl;
			
		}	
   cout<<setw(2)<<"------------------"<<endl<<endl;
}
	

void playB()//this is for the beginners game
{	
	 int score = 0;
	 guesses=0; 
	 initialise();
	 mine_set();

	do
	{ 
		
		drawGridB();		
		cout<<endl<<setw(6)<<"Score = "<<score<<endl<<endl;
		cout<<setw(6)<<"Guesses = "<<guesses<<endl<<endl;
		cout << "Please enter the Vertical number" << endl;
		cin >> col;
		cout << "Now enter the Horizontal number" << endl;
		cin >> row;

			if (row>=8||col>=8)
			{
				cout << "You have entered an invalid choice." << endl;
			}
			else
			{
				cout << "Do you want to dig(d) or flag for a bomb here(f)?" << endl;
				cin >> userchoice;

			if (game[row][col]=space)
				{
					cout<<"You have entered a invalid choice"<<endl;
				}
			//else if (game[row][col]=flag);
			//	{
			//		cout<<"You will now Dig at these coordinates"<<endl;
			//		game[row][col]=space;
			//	}

			if (userchoice == 'd' && minesweeper[row][col]==-1||guesses==65)
			{
				cout << "BOOM!!" << endl;
				cout << "You have hit a mine" << endl;
				cout<<"Score = "<<score<<endl;
				cout<<"Guesses ="<<guesses<<endl;
				cout << "Please Play again" << endl;
				game[row][col]=mine;
			}
			else if (userchoice == 'd' && minesweeper[row][col] == 0)
					{
						game[row][col]=' ';
						guesses++;
						score++;
						cout<<userchoice<<endl;
						
					}
			else if(userchoice=='d'&&minesweeper[row][col]==1)
					{
						game[row][col]='1';
						score++;
						guesses++;
						cout<<userchoice<<endl;;
						cout<<endl<<setw(6)<<"Score = "<<score<<endl<<endl;
						cout<<setw(6)<<"Guesses = "<<guesses<<endl<<endl;
					}
			else if (userchoice=='d'&&minesweeper[row][col]==2)
					{
						game[row][col]='2';
						guesses++;
						score++;
						cout<<userchoice<<endl;
						cout<<endl<<setw(6)<<"Score = "<<score<<endl<<endl;
						cout<<setw(6)<<"Guesses = "<<guesses<<endl<<endl;
					}
			else if (userchoice=='d'&&minesweeper[row][col]==3)
					{
						game[row][col]='3';
						guesses++;
						score++;
						cout<<userchoice<<endl;
						cout<<endl<<setw(6)<<"Score = "<<score<<endl<<endl;
						cout<<setw(6)<<"Guesses = "<<guesses<<endl<<endl;
					}
				else if (userchoice == 'f')
						{
							game[row][col]=flag;
							guesses++;
							
						}
			drawGridB();
		}
  }
  while (guesses<64);


		/*if(guesses==mines)
			cout<<"Congratulations- You Win!!!!"<<endl<<endl;
		cout<<"would you like to play again? Yes or No"<<endl;
		cin>>play_again;
		if ("yes")
			playB();
		else if ("No")
			playB();*/
}


				cin>>option;
				cout<<endl;
				cin.ignore();
				switch(option)
				{
				case '1':playB();
						break;
				




void main()//start of the program, fully compiled
{
		displaymenu();
		srand(time(NULL));
		system("pause");
	
}
Your code is messed up. It looks like mine_set() is being defined inside initialise() because there is no ending }, and lines 183-189? Copy paste error?
yea, I musta missed that part when I copied it, its there in the original code

183-189 is part of my main menu, thats the part that starts the game, I left the rest out as it's not relevant, but wanted to include that part as it starts after selecting option 1
Last edited on
On line 41/42 you use the global variables form line 22. But since they permanently change (like on line 93/95, 61/65) you cannot predict what the value is.
So, how can I rectify that? Change the names of them? Set them as local variables?
Pages: 12