code for minsweeper game

Hi, i have edited this code for a simple minesweeper game but i need to rearrange the code so that it is all in the
1
2
3
int main() 
{
}
section. please can you guide me on how i can do this. thanks

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

#include <iostream>
#include <string>
using namespace std;


int X,Y;
char name;



void display_board(char** board, int rows, int columns)
{    
    int linecharacters = 0;
    char vlign = ' ';
    char hlign = ' ';
    cout << "   ";
    linecharacters += 3;
    for (int i=0; i<columns; i++)
    {
        cout << i;
        if (i<=9) 
        {
            cout << " ";
            linecharacters++;
        }
        cout << " ";
        linecharacters += (i > 9) ? 3 : 2; 
    }
    cout << endl;

    string line = string(linecharacters, hlign);
	int r;
    for (r=0; r<rows; r++)
    {
        cout << line << endl;
        if (r<=9) { cout <<  r << " " << vlign; }
        else { cout << r << vlign; } 

        for (int c=0; c<columns; c++)
        {
            if (board[r][c] == 'e')
            { cout<<"O "<<vlign;}
            else if (board[r][c] == 'x')
            {cout<<"M "<<vlign;}
            else
            { cout<<"? "<<vlign;}
        }
        
        cout<<endl;
    }
}


void play_board(char** board, int rows, int columns, int mines)
{
    int guessed_mines = 0;
    int X, Y;
    char userchoice;
    
    do
    {
        display_board(board, rows, columns);

        cout<<"Enter X Co-ordinate"<<endl;
    	cin>>X;
    	cout<<"Enter Y Co-ordinate"<<endl;
    	cin>>Y;
    	
		
cout<<"Enter M to make move, B to mark as bomb"<<endl;
    	cin>>userchoice;
    	if (Y>rows||X>columns)
    	    cout << "You have entered the wrong Co-ordinates, Please Retry" << endl;

	    if (userchoice=='m' && board[X][Y] == 'b')
	    {
            cout << "You Hit a Mine" <<endl;
            board[X][Y] = 'x';
            guessed_mines++;
			
        }
	    else if (userchoice=='b' && board[X][Y] == 'b') 
	    {
            board[X][Y] = 'x';
            guessed_mines++;
			
        }         

        else
	    {
            if (board[X][Y] != 'x') board[X][Y] = 'e';
			
        }
    } while (guessed_mines < mines);

    if (guessed_mines == mines)
    {
        cout << "You won!" << endl;
    }
}


void set_mines(char** board, int columns, int rows, int mines)
{ 
    int minecounter = 0, X, Y;
	do
	{
		X = rand()%rows;
                Y = rand()%columns;
                if (board[X][Y] !='b')
		{
			board[X][Y] = 'b';
			minecounter++;
		}
	} while (minecounter < mines);
}


void beginner() { //Beginner

    int rows=6, columns=6, mines=5;

	cout << "Welcome to minesweeper" << endl;

	char** beginnerboard = new char *[rows];  
    for (int i=0;i<rows;i++)
    {
        beginnerboard[i] = new char[columns];
    }
	set_mines(beginnerboard, rows, columns, mines);
	
	
    
   play_board(beginnerboard, rows, columns, mines);
	    system ("pause");
    delete[] beginnerboard;
}



int main() 
{
	beginner();
    system("pause");
    return 0;
}
closed account (o3hC5Di1)
Hi there,

Why would you want it all into the main function?
Having it in separate functions is considered to be good style, much more readable and maintainable.
Did you get this code off the forums here somewhere by the way, it looks really familiar?

All the best,
NwN
hi, yeah i got the code off the forum and removed the bits i don't want.

i want to put it in the main function because i don't really understand how the separate function works. i think if i can put it into the main function i would then be able to understand the code abit better.
closed account (o3hC5Di1)
In that case, let's just break down how it works:

Main() calls beginner(), so the beginner() function is what we should look at first.
If I remember correctly the creator of this code used that name for the function because he wanted to expand it afterwards using intermediate(), advanced() etc.

Now then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void beginner() { //Beginner
    
    int rows=6, columns=6, mines=5;   //the dimensions of the minefield and the amount of mines

	cout << "Welcome to minesweeper" << endl;

	char** beginnerboard = new char *[rows];   //declaring the board as a 2D array (or matrix)
    for (int i=0;i<rows;i++)  //every row of the matrix contains a number of columns (or cells)
    {
        beginnerboard[i] = new char[columns];
    }
	set_mines(beginnerboard, rows, columns, mines);  //calls set mines, which places mines on the board

   play_board(beginnerboard, rows, columns, mines);  //calls play_board, which plays the game with the user
	    system ("pause");
    delete[] beginnerboard;  //have to do this or we end up with a memory leak
}


play_board() basically asks the user to input an X and Y coordinate and checks whether there is a mine there or not and it does so until a mine is trod on, or the user has found all other coordinates.
display_board() prints out the beginnerboard matrix, showing the positions the user has marked already.

If you have any further questions, just let us know which parts of the code you don't understand and we'll try to explain them to you.

All the best,
NwN
I sometimes do the same thing in reverse. Write it in main and later put it into functions.

read what NwN wrote, then this,
1. Backup your file (always)
2. go though the code everywhere you see a function, look for where it came from. use comments when there are lots of functions because if someone distrubs your concentration, you can get lost easy.

void beginner()
{
// called from main
code
play_board(beginnerboard, rows, columns, mines); // called from beginner

}

1
2
3
4
5
6
int main() 
{
    beginner(); // function
    system("pause");
    return 0;
}


3. cut and paste the code one function at a time into main, and test it.

You'll have to make a few changes, mostly to the variables, but if you do it in small steps, it should be a pretty good learning experience.
Topic archived. No new replies allowed.