Minesweeper Recursion Issue

Hey guys, i'm working on a minesweeper project for school(due tonight unfortunately), and I've run into a problem. I use a recursive function called recurseCheck which i use to check for empty spaces around the space selected on the minesweeper grid. I think it is infinitely calling the function, and I really don't know why. Since this is due in about 3 hours if anyone could help me find any logic errors, I would really appreciate it. Thanks

This is the function
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
void recurseCheck (int row, int col, Grid theGrid)
{	
if (row>9 || row<0 || col>9 || col<0 || theGrid.getArrIsMine(row,col)==true || theGrid.getArrCovered(row,col)==false || theGrid.getArrFlagged(row,col)==true)
	return;
theGrid.setArrCovered(false,row,col);
theGrid.displayGrid();
if (theGrid.getArrMines(row, col)==0)
{
	if (theGrid.getArrIsMine(row-1,col-1)==false && theGrid.getArrFlagged(row-1,col-1)==false && theGrid.getArrCovered(row-1,col-1)==true && col>0 && row>0)
		recurseCheck(row-1,col-1,theGrid);
	if (theGrid.getArrIsMine(row-1,col)==false && theGrid.getArrFlagged(row-1,col)==false && theGrid.getArrCovered(row-1,col)==true && row>0)
		recurseCheck(row-1,col,theGrid);
	if (theGrid.getArrIsMine(row-1,col+1)==false && theGrid.getArrFlagged(row-1,col+1)==false && theGrid.getArrCovered(row-1,col+1)==true && row>0 && col<9)
		recurseCheck(row-1,col+1,theGrid);
	if (theGrid.getArrIsMine(row,col-1)==false && theGrid.getArrFlagged(row,col-1)==false && theGrid.getArrCovered(row,col-1)==true && col>0)
		recurseCheck(row,col-1,theGrid);
	if (theGrid.getArrIsMine(row,col+1)==false && theGrid.getArrFlagged(row,col+1)==false && theGrid.getArrCovered(row,col+1)==true && col<9)
		recurseCheck(row,col+1,theGrid);
	if (theGrid.getArrIsMine(row+1,col-1)==false && theGrid.getArrFlagged(row+1,col-1)==false && theGrid.getArrCovered(row+1,col-1)==true && row<9 && col>0)
		recurseCheck(row+1,col-1,theGrid);
	if (theGrid.getArrIsMine(row+1,col)==false && theGrid.getArrFlagged(row+1,col)==false && theGrid.getArrCovered(row+1,col)==true && row<9)
		recurseCheck(row+1,col,theGrid);
	if (theGrid.getArrIsMine(row+1,col+1)==false && theGrid.getArrFlagged(row+1,col+1)==false && theGrid.getArrCovered(row+1,col+1)==true && row<9 && col<9)
		recurseCheck(row+1,col+1,theGrid);	
}
	theGrid.displayGrid();
}

This is the class Grid
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
Grid()
	{//Checking mines for inner 9x9 grid.
		for (int i=1; i<9;i++)
		{
			for (int j=1; j<9; j++)
			{
				if (arr[i-1][j-1].getIsMine()==true)
						arr[i][j].setMines(arr[i][j].getMines()+1);
				if (arr[i-1][j].getIsMine()==true)
						arr[i][j].setMines(arr[i][j].getMines()+1);
				if (arr[i-1][j+1].getIsMine()==true)
						arr[i][j].setMines(arr[i][j].getMines()+1);
				if (arr[i][j-1].getIsMine()==true)
						arr[i][j].setMines(arr[i][j].getMines()+1);
				if (arr[i][j+1].getIsMine()==true)					
						arr[i][j].setMines(arr[i][j].getMines()+1);
				if (arr[i+1][j-1].getIsMine()==true)
						arr[i][j].setMines(arr[i][j].getMines()+1);
				if (arr[i+1][j].getIsMine()==true)
						arr[i][j].setMines(arr[i][j].getMines()+1);
				if (arr[i+1][j+1].getIsMine()==true)
						arr[i][j].setMines(arr[i][j].getMines()+1);		
			}
		}
		//Checking for mines for left-most column excluding end points.
		for(int i=1; i<9; i++)
		{
				if (arr[i-1][0].getIsMine()==true)
					arr[i][0].setMines(arr[i][0].getMines()+1);
				if (arr[i-1][1].getIsMine()==true)
					arr[i][0].setMines(arr[i][0].getMines()+1);
				if (arr[i][1].getIsMine()==true)
					arr[i][0].setMines(arr[i][0].getMines()+1);
				if (arr[i+1][0].getIsMine()==true)
					arr[i][0].setMines(arr[i][0].getMines()+1);
				if (arr[i+1][1].getIsMine()==true)
					arr[i][0].setMines(arr[i][0].getMines()+1);
			
		}
		//same as above but for the furthest right column.
		for (int i=1; i<9; i++)
		{
				if (arr[i-1][9].getIsMine()==true)
					arr[i][9].setMines(arr[i][9].getMines()+1);
				if (arr[i-1][8].getIsMine()==true)
					arr[i][9].setMines(arr[i][9].getMines()+1);
				if (arr[i][8].getIsMine()==true)
					arr[i][9].setMines(arr[i][9].getMines()+1);
				if (arr[i+1][9].getIsMine()==true)
					arr[i][9].setMines(arr[i][9].getMines()+1);
				if (arr[i+1][8].getIsMine()==true)
					arr[i][9].setMines(arr[i][9].getMines()+1);
			
		}
		//same but for first row
		for (int i=1; i<9; i++)
		{
				if (arr[0][i-1].getIsMine()==true)
					arr[0][i].setMines(arr[0][i].getMines()+1);
				if (arr[0][i+1].getIsMine()==true)
					arr[0][i].setMines(arr[0][i].getMines()+1);
				if (arr[1][i-1].getIsMine()==true)
					arr[0][i].setMines(arr[0][i].getMines()+1);
				if (arr[1][i].getIsMine()==true)
					arr[0][i].setMines(arr[0][i].getMines()+1);
				if (arr[1][i+1].getIsMine()==true)
					arr[0][i].setMines(arr[0][i].getMines()+1);
			
		}
		//last row
		for (int i=1; i<9; i++)
		{
				if (arr[9][i-1].getIsMine()==true)
					arr[9][i].setMines(arr[9][i].getMines()+1);
				if (arr[9][i+1].getIsMine()==true)
					arr[9][i].setMines(arr[9][i].getMines()+1);
				if (arr[8][i].getIsMine()==true)
					arr[9][i].setMines(arr[9][i].getMines()+1);
				if (arr[8][i-1].getIsMine()==true)
					arr[9][i].setMines(arr[9][i].getMines()+1);
				if (arr[8][i+1].getIsMine()==true)
					arr[9][i].setMines(arr[9][i].getMines()+1);
			
		}
		//Upper left corner
		
			if (arr[0][1].getIsMine()==true)
				arr[0][0].setMines(arr[0][0].getMines()+1);
			if (arr[1][1].getIsMine()==true)
				arr[0][0].setMines(arr[0][0].getMines()+1);
			if (arr[1][0].getIsMine()==true)
				arr[0][0].setMines(arr[0][0].getMines()+1);	
		
		//Lower left corner	
		
			if (arr[9][1].getIsMine()==true)
				arr[9][0].setMines(arr[9][0].getMines()+1);
			if (arr[8][1].getIsMine()==true)
				arr[9][0].setMines(arr[9][0].getMines()+1);
			if (arr[8][0].getIsMine()==true)
				arr[9][0].setMines(arr[9][0].getMines()+1);
		
		//upper right corner
		
			if (arr[0][8].getIsMine()==true)
				arr[0][9].setMines(arr[0][9].getMines()+1);
			if (arr[1][8].getIsMine()==true)
				arr[0][9].setMines(arr[0][9].getMines()+1);
			if (arr[1][9].getIsMine()==true)
				arr[0][9].setMines(arr[0][9].getMines()+1);
		
		//lower right corner
		
			if (arr[8][9].getIsMine()==true)
				arr[9][9].setMines(arr[9][9].getMines()+1);
			if (arr[8][8].getIsMine()==true)
				arr[9][9].setMines(arr[9][9].getMines()+1);
			if (arr[9][8].getIsMine()==true)
				arr[9][9].setMines(arr[9][9].getMines()+1);
		
	}
};


This is the Cell class

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
class Cell
{
private:
	bool isMine, covered, flagged;
	int mines;
public:
	bool getFlagged ()
	{
		return flagged;
	}
	void setFlagged (bool flag)
	{
		flagged=flag;
	}
	int getMines()
	{
		return mines;
	}
	void setMines(int m)
	{
		mines=m;
	}
	bool getIsMine()
	{
		return isMine;
	}
	bool getCovered()
	{
		return covered;
	}
	void setCovered(bool cover)
	{
		covered=cover;
	}
	Cell()
	{
		int random=rand()%10+1;
		if (random==2)
			isMine=true;
		else
			isMine=false;
		mines=0;
		covered=true;
		flagged=false;
	}
};
There are a lot of conditions to check through here and I would think you would have more luck debugging the program and going through where you think the problems are line by line and seeing if they give the results you expect.

The only quick guess I can give you is, in the recurseCheck function you are passing the Grid object by value and then calling functions on it. However all of those values will only be given to the local copy of the object in the function not the one you passed. You will need to pass it by reference if you want to change this.

void recurseCheck (int row, int col, Grid &theGrid)

(This may be part of your design how you currently have it, it's just hard to tell).
James, you're the man! I can't believe that you figured this out so fast and with such little code! Thank you so much man, I probably went from a C to and A on this project because of you.
Glad I could help :D.
Topic archived. No new replies allowed.