needing some help overlapping two functions

I'm currently writing a minesweeper program and have come to a hault due to not being able to get my mine array to be printed into the grid.

any help or pointers would be appriciated


randomally generates 10 mines within the array
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
#include <iostream>
#include <conio.h>
#include <algorithm>
#include <ctime>


using namespace std;


int main()
{

int mField[8][8] = {{0,0,9,0,0,9,0,0},{0,0,9,0,0,9,0,0},{0,0,9,0,0,0,0,0},{0,0,9,0,0,0,0,0},{0,0,0,0,0,9,0,0,},{0,0,9,0,0,0,0,0},{0,0,9,0,0,0,0,0},{0,0,0,9,0,0,0,0}};
int x = 0;
	
	srand ((unsigned int)(time(0)));
	for(int x=0; x<8; x++){
	
		for(int j=0;j<8; j++){
			
			int values1=rand()%8;
			int values2=rand()%8;
			int temp =mField[x][j];
			mField[x][j]=mField[values1][values2];
			mField[values1][values2]=temp;		
		}
		
	
	}
	
	for(int x=0; x<8; x++)
	{
		for(int j=0;j<8;j++)
		{
			cout<<mField[x][j];	
		}
			cout<<endl;
	}
		_getch();
}

///////////////////////////////////////////////////////


prints a grid for the mines to go into 

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	int line = 0;
		cout << " 1   2   3   4   5   6   7\n";

		
		for (int a = 0; a <= 5; a++)
			
		{
			cout << line++;
			for (int b = 0; b <= 6; b++) cout << char(218) << char(196) << char(191) << " ";
			
				cout << '\n';
				cout << " ";
			for (int b = 0; b <= 6; b++) cout << char(179) << " " << char(179) << " ";
			
				cout << '\n';
				cout << " ";
			
			for (int b = 0; b <= 6; b++) cout << char(192) << char(196) << char(217) << " ";
			
				cout << '\n';
				
			}
		_getch();
		}


Last edited on
So what program is causing the problems?

Do you realize that there can be only one main() function per program?

The problem is that if I change them both to void functions and call them within the main section they just print under one another, rather than the arrays containing 0's and 9 being printed inside each box within the grid.

Yea they are currently in seperate c++ files to allow me to screenshot and use unit testing to them.
You really don't need to combine anything, but you do need to split some things up and write a function or two.

I know people pooh-pooh globals all the time, but this is a good case for having one: your mine field. Make that a global array.

Then you need a few functions:
 • a function to clear the array to no-mines
 • a function to randomly place N mines into the array
 • a function to tell you how many mines surround a particular element in the array
 • a function to print the array/minefield to the screen
...and so on.

This makes main() a lot easier to write and manage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
  srand(time(NULL));
  cout << "Welcome to MINE-DEATH BATTLEFIELD, grunt. Prepare to battle.\n";
  
  do {
    clear_minefield();
    set_mines( ask_number_of_mines_difficulty() );
    
    display_minefield();
    do {
      int x, y;
      ask_which_to_uncover( x, y );
      uncover( x, y );
      display_minefield();
    while (!is_mine( x, y ) && !is_game_won());

    if (is_game_won())
       ...

  } while (ask_play_again());

}

Etc. The use of these little functions allows you to deal with things in a much more convenient, human-like abstraction, and the little functions themselves do such small things it is easy to program just that.

Hope this helps.
cheers for the idea, but as it is a college/uni assignment if I use global variables I lose marks.

so everything needs to be in void functions and pass parameters for them. I'm just unsure how I go about getting the array inside of the actual grid. Do I need to integrate them into one loop?
That's not using global variables. Also, everything does not have to be void functions, returning values is necessary in many cases.

a global variable is something you declare above your main class and can freely manipulate between classes and throughout the class.

You should really attempt to use functions/comments as I'm completely lost on what you need or want done. I can help you more if you clarify a bit, I had to do something similar in one of my classes last year.
I know people pooh-pooh globals all the time, but this is a good case for having one: your mine field. Make that a global array.
Would make the minefield a global variable would it not?

At the minute I'm just trying to get an 8 by 8 grid with 10 mines to randomise within that grid.

The top code randomises the location of the 9's (which I will be using as mines) and the bottom code is just an output grid onto the screen
Last edited on
you can make a c++ safer and cleaner global. IMHO this is still a design flaw as it has most of the problems of globals (race conditions, who changed it last, multithreaded problems, etc) but something like this is a little better...

1
2
3
4
5
6
template <typename type> class glob
{//this class effectively makes a global of type. 
   public:
   static type t;	
};   
template <typename type> type  glob<type>::t;


remember also that classes pretty much use globals. class variables are global to all the routines in the class and visible everywhere in the scope. Just wrapping something in a class gives you that level of access and power but manages the risks to be inside the class. If you make it static, its shared across all instances, if not, its per-instance but shared to all the routines.

Last edited on
@jonnin
No need for the class since C++14's variable templates:
template <typename T> T global;

http://en.cppreference.com/w/cpp/language/variable_template
True!

the intent of mine was to encourage re-creating it everywhere you needed it, though.
eg

void foo()
{
glob<int> x;
}

void bar()
{
glob<int> x;
}

to avoid having to have
glob<int> x at the global scope, providing some protections (not much, but some). Anything that wants to use it has to declare it again.

I dunno if that really saves much in the grand scheme.
I never used it, it came out of playing with something else.


Last edited on
Ok so i've been messing around trying to improve the grid layout of the game, I've now put the grid into an array as well.

I'm now wanting to put the mines into the corrispoding grid square; screen shot will show what I mean https://ibb.co/mgygR6

code is as followed:
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
#include <iostream>
#include <conio.h>
#include <algorithm>
#include <ctime>

using namespace std; 


//Beginner game void functions 
void draw_gridBeginner(){

int x,y;
char grid[8][8]= {{255}};
char topl[33]={201,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,187};
char midl[33]={204,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,185};
char bottoml[33]={200,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,188};
  
  cout<<endl;
  cout<<topl<<endl;
  for(x=0;x<8;x++)
   {
   	for(y=0;y<8;y++)
   	 {
   	    if(y==0)
   	     cout<<char(186);
		cout<<char(32)<<grid[x][y];
		if (grid[x][y] <8)
		cout<<char(32)<<char(186);
	    else
	    cout<<char(186);
     }
     cout<<endl;
    if (x<8)
     cout<<midl<<endl;
   }
  
  }

void mineBeginner() // randomises 10 mines (9) into an array
{

int mField[8][8] = {{0,0,9,0,0,9,0,0},{0,0,9,0,0,9,0,0},{0,0,9,0,0,0,0,0},{0,0,9,0,0,0,0,0},{0,0,0,0,0,9,0,0,},{0,0,9,0,0,0,0,0},{0,0,9,0,0,0,0,0},{0,0,0,9,0,0,0,0}};
int x = 0;
	
	srand ((unsigned int)(time(0)));
	for(int x=0; x<8; x++){
	
		for(int j=0;j<8; j++){
			
			int values1=rand()%8;
			int values2=rand()%8;
			int temp =mField[x][j];
			mField[x][j]=mField[values1][values2];
			mField[values1][values2]=temp;		
		}
		
	
	}
	
	for(int x=0; x<8; x++)
	{
		for(int j=0;j<8;j++)
		{
			cout<<mField[x][j];	
		}
			cout<<endl;
	}
	
}

// End Beginner game void functions 

int main(){

		draw_gridBeginner();
		

	
	return 0;
}


I'm still unsure how I'm meant to get the mines to sit within the grid.
Last edited on
Get out a piece of graph paper and draw what you expect it to look like, both as the user sees it and as the data underneath exists.

Write a function to draw it to the display properly.


If prof will take marks for using an obvious global, cheat and wrap globals into a struct that you pass around as argument to the functions. Or just put everything (except main()) into a class.
Managed with the help of a friend to get it searching the array within the same function. I've now added a loop to play over if the player hits a mine.

Two new questions.

1. I have one slight problem with the replay though, for some reason it misses the first line to input the column number and goes straight to the Row number.

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
void mineBeginner() // randomises 10 mines (9) into an array
{

int mField[8][8] = {{0,0,9,0,0,9,0,0},{0,0,9,0,0,9,0,0},{0,0,9,0,0,0,0,0},{0,0,9,0,0,0,0,0},{0,0,0,0,0,9,0,0,},{0,0,9,0,0,0,0,0},{0,0,9,0,0,0,0,0},{0,0,0,9,0,0,0,0}};
int x = 0;
bool alive = true;
string answer;

	srand ((unsigned int)(time(0)));
	for(int x=0; x<8; x++){
	
		for(int j=0;j<8; j++){
			
			int values1=rand()%8;
			int values2=rand()%8;
			int temp =mField[x][j];
			mField[x][j]=mField[values1][values2];
			mField[values1][values2]=temp;		
		}
		
	
	}
	
	for(int x=0; x<8; x++)
	{
		for(int j=0;j<8;j++)
		{
			cout<<mField[x][j];	
		}
			cout<<endl;
	}

	
	while(alive){
        string Column;
        string Row;
        cout << "Enter Column number: "<<endl;
        getline (cin, Column);
        cout << "Enter Row number: "<<endl;
        getline (cin, Row);
        cout << "[" << Column << "][" << Row << "]" << "\n";
        int xValue = atoi(Row.c_str());
        int yValue = atoi(Column.c_str());
        cout << mField[xValue][yValue] << "\n";
       
        
		 if(mField[xValue][yValue] == 9){
		 	alive = false;
        		system("CLS");
        		cout<<"			BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOM"<<endl;
        		cout<<"						  GAME OVER"<<endl;
        		cout <<"					  Play again? Type Yes or No"<<endl;
        		cin>>answer;
        		
        		if(answer == "Yes"  ){
        			
        			system("CLS");
        			draw_gridBeginner();
        			mineBeginner();
				}
        		
        		if(answer == "No" ){
        			
        			break;
				}
				
			}
    }
}


---------------------------------------------------

Second is my grid. An array starts from 0,0 meaning my top left hand grid is going to be 0,0 which then messes up my whole board as 0.0 should be nothing.

Also I'm not sure If I need to create a new grid to pring 1-8 on the left hand side of the grid, Or it's just my inability to work out where I need to i++ in the current for loop.

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
//Beginner game void functions 
		void draw_gridBeginner(){
	
			int x,y;
			int a = 1;
			cout<<"  0   1   2   3   4   5   6   7   ";
			char grid[8][8]= {{255}};
			char topl[34]={ 201,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,187};
			char midl[34]={ 204,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,185};
			char bottoml[34]={ 200,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,188};
			
			cout<<endl;
		
		  	cout<<topl<<endl;
		  	
		  	for(x=0;x<8;x++)
		   		{
		   			for(y=0;y<8;y++)
		   	 			{
		   	    			if(y==0)
		   	     			cout<<char(186);
							cout<<char(32)<<grid[x][y];
							if (grid[x][y] < 8)
							cout<<char(32)<<char(186);
						    else
						    cout<<char(32);
						    a++;
		     			}
		     			cout<<endl;
		    		if (x<8)
		     			cout<<midl<<char(32)<<endl;
		   		}
		  
		  }

Last edited on
Topic archived. No new replies allowed.