Minesweeper

Hi, I am making a minesweeper game, and have made the bombs and other things. But I have a problem with getting the numbers of bombs next to it. With this code it does not check the tiles above it and I can not figure out why.
Here is my code:

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
int miner = 0;
	if (row - 1 >= 0){
		if (isTileMine(row - 1, col - 1)) {
			miner++;
		}
		if (isTileMine(row - 1, col)) {
			miner++;
		}
		if (isTileMine(row - 1, col + 1)) {
			miner++;
		}
	}
	if (isTileMine(row, col - 1)) {
		miner++;
	}
	if (isTileMine(row, col)) {
		miner++;
	}
	if (isTileMine(row, col + 1)) {
		miner++;
	}
	if (row + 1 < row){
		if (isTileMine(row + 1, col - 1)) {
			miner++;
		}
		if (isTileMine(row + 1, col)) {
			miner++;
		}
		if (isTileMine(row + 1, col + 1)) {
			miner++;
		}
	}
	return miner;
You need to rethink your logic on line 22:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

// Let's say row = 2:

if( row + 1 < row )

// plug in values:

if( 2 + 1 < 2 )

is:

if ( 3 < 2 )

// This will never be true!
Last edited on
1. You check to see if the row is out of bounds, but you never check if the column is out of bounds.

2. Line 22 always evaluates to false.

Lines 16-18 are unnecessary because that is the cell you're on, not its neighbor.

You should use embedded for or while loops for this.

1
2
3
4
5
6
7
8
9
10
for(row - 1 to row + 1)
{
    for(col - 1 to col + 1)
    {
        if(row is valid and col is valid)
        {
            miner + 1
        }
    }
}


When you place each bomb, update the number of bombs next to the cell in each cell next to the bomb. Assuming there are any blank spaces, this will make the processing take less time.
Topic archived. No new replies allowed.