Marking a space on a Minesweeper grid

This is a HW question but I do have most of the code created just hungup on one detail. For the "Mark Space" program I am having the issue of not being able to replace the 'O' with an 'X' without the function thinking 'X' was already my choice in the firstplace. Ive included the exact prompt as to give a better idea of what I am trying to achieve. All my other functions seem to be working perfectly but this last one I cant get to work. If I remove a[row][col] = 'X'; from the "safe" portion of MarkSpace everything runs smoothly, if I do include it marks all attempts as "MARKED" even if I havent tried that space before

PROMPT FROM INSTRUCTOR:

SIS 113A Lab Assignment 17 - A Simple Game

This game is a simplified version of the mine sweep game. In this game you will randomly populate a grid with bombs. The user will be allowed to guess locations until they run into a bomb. Once they have blown up, you will show the grid and report how many spaces were guessed before they encountered the bomb. The object of the game is to guess as many free spaces as possible without hitting a bomb.

Specifics:

This game will use a 5 rows x 6 column 2D array of characters to represent the game board.
The board will be marked with an 'O' to indicate an open location.
The board will be marked with an '*' to indicate there is a bomb.
The board will be marked with an 'X' to indicate a space that has been selected

Functions:

The following functions are required and you *must* use these exact names and capitalization:

InitBoard - Populates the array with the 'O' character to represent an open space.
PrintBoard - Prints the board out 5 rows by 6 columns. Each column should be tab spaced.
ConvertToRowCol - This function takes a value between 1 and 30 and converts the value to row and column locations. You will need to use reference parameters to accomplish this.
PopulateBombs - Randomly populates 6 spaces with a bomb. You will use the '*' character to represent the bomb. Hint: This is trickier than you might think - make sure you have 6 bombs.
MarkSpace - This function is used to check and mark the board depending on the character in the location. It takes as parameters the 2D array that represents the board and the row column location to check. If the location has a 'O' in it you will place an X in the location to mark the spot and return SAFE. If the location has an 'X' in it you will return MARKED. If it has a '*' character you will return BOMB. To recap the function should return the following:

SAFE if there was no bomb and place an X in the location
MARKED if the space had previously been chosen.
BOMB if the space chosen contains the * character

You may create additional utility (helper) functions if needed but follow the same naming conventions.

BOMB, SAFE, and MARKED should be defined as constants in global scope and have the following values:

BOMB = 1
SAFE = 0;
MARKED = -1



Note: 1) Yes, global variables are bad and even global constants introduce function dependencies. 2) These should be your *only* global variables. 3) Yes, there is a better way but you don’t know how yet to make a variable that has more than function scope and less that program scope.

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
  
#include <iostream>
#include <ctime>

using namespace std;

void InitBoard(char a[5][6]);
void PrintBoard(char a[5][6]);
void ConvertToRowCol(int input, int &row, int &col);
int MarkSpace(char a[5][6], int row, int col);
void PopulateBombs(char a[5][6]);

const int BOMB = 1;
const int SAFE = 0;
const int MARKED = -1;

int main()
{

	char boardGame[5][6];
	int input = 0;
	int score = 0;
	int row = 0;
	int col = 0;
	int count = 0;
	InitBoard(boardGame);
	PopulateBombs(boardGame);


	do
	{
		cout << "Enter a value between 1 and 30 to check a space: ";
		cin >> input;
		ConvertToRowCol(input, row, col);
		cout << "You selected row " << row << " and col " << col << endl;
		MarkSpace(boardGame, row, col);

		if (MarkSpace(boardGame, row, col) == MARKED)
		{
			count--;
			cout << "Youve already guessed this space! That will cost you a point" << endl;

		}
		else if (MarkSpace(boardGame, row, col) == SAFE)
		{
			count++;

			cout << "Very good that is an open space, your count is now " << count << endl;
		}
	} while (MarkSpace(boardGame, row, col) != BOMB);


	PrintBoard(boardGame);

	cout << "Tough luck you hit a bomb, your final count was " << count << endl;

	return 0;
}
void InitBoard(char a[5][6])
{
	for (int row = 0; row < 5; row++)
		for (int col = 0; col < 6; col++)
		{
			a[row][col] = 'O';

		}

}
void PrintBoard(char a[5][6])
{
	for (int row = 0; row < 5; row++)
	{
		for (int col = 0; col < 6; col++)
		{
			cout << a[row][col] << "\t";
		}
		cout << endl;
	}
}
void ConvertToRowCol(int input, int &row, int &col)
{
	int rowConverter[31]{ 0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4 };
	int colConverter[31]{ 0,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5 };

	row = rowConverter[input];
	col = colConverter[input];

}
void PopulateBombs(char a[5][6])
{
	srand((unsigned)time(0));
	int bombCount = 0;

	do
	{
		for (int row = 0; row < 5; row++)
			for (int col = 0; col < 6; col++)
			{
				int randomBomb = rand() % 10 + 1;

				if (randomBomb > 7 && bombCount < 6)
				{
					a[row][col] = '*';
					bombCount++;
				}
			}
	} while (bombCount < 6);
	}
int MarkSpace(char a[5][6], int row, int col)
{
	

	if (a[row][col] == 'X')
	{


		return MARKED;
	}
	
	else if (a[row][col] == 'O')
	{
		a[row][col] = 'X';

		return SAFE;
	}
	else 
	{


		return BOMB;
	}

}
Last edited on
closed account (D80DSL3A)
The problem is the multiple calls to Markspace(). The 1st call at line 36 gets the 'O' changed to 'X' so it's already that way when you call it again on line 38. This function should be called only once per turn. You could save the value returned from the call on line 36 (why is it called there and return not saved? Practice call?). Use the saved value to test against in the if() conditions.
36
37
38
39
40
int status = MarkSpace(boardGame, row, col);

		if (status == MARKED)
		{
                 // and so on 

edit: You'll need to use status in the while on line 50 too, so declaring status within the do-while body won't work. It would be out of scope outside the {}. Declare status before the do loop.

One workaround:
1
2
3
4
5
6
7
do
{
    int status = MarkSpace(boardGame, row, col);
    if( status == BOMB) break;// exit do-while loop
    // isn't there a 2nd condition for game end? count = 30 - numBombs = 24 wins?

}while(true);
Last edited on
I'm still running into the exact same issue :/ it still comes up as a already MARKED spot even if I havent tried that spot before
closed account (D80DSL3A)
Please show the new code. main() function only will do. The MarkSpace() function looks like it should work as desired. It's gotta be the multiple calls per turn in main().
Thank you! such a silly mistake hahah I appreciate the help!! Its all good now
Topic archived. No new replies allowed.