OK so heres the scoop: I am building a minesweeper style program for my c++ class, I’m using a 2D array to create the board,and a second 2d array to store guesses. For each value in the board array, I run a random number gen and assign it a value between 0 and 99. Each value in the array is considered a bomb if its value is greater than 85. The program accepts user input and determines if it is a bomb. If it is not the corresponding bool array (isGuessed) position is changed to true. This bool array is then sent to the reDraw function to be redrawn. All false bools are displayed as ‘?’ and all true are displayed as ‘X’. However, when I reDraw, it changes multiple locations in the array to an X with only one guess. My thought is that the board is not drawn in connection with the array positions. Could someone help. Just to be clear, I’m trying to understand why the function reDraw is not working properly. Here is ALL my code.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//Function Prototypes
void reDraw (bool guessed [] [10], int rows, int columns);
void newBoard (int rows, int columns);
int main(int argc, const char * argv[])
{
//Declare Variables
const int ROWS = 10;
const int COLUMNS = 10;
int board [ROWS][COLUMNS];
bool wasGuessed [10] [10];
bool playAgain = true;
char newGame;
int rowGuess, columnGuess, numOfBombs = 0, score = 0;
bool bomb = false;
srand((unsigned) time(0));
// Welcome User, Give Direction
cout<<"\n\n\t\tWelcome To Matt's Minesweeper\n\n";
while (playAgain) {
//function to randomly populate array elements
for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS; column++) {
board [row] [column] = rand() % 100;
if (board [row] [column] >= 85) { //counter for bombs
numOfBombs++;
}
}
}
// Create a new Display Board
newBoard(10, 10);
//Begin Game
cout<<"\nTotal Bombs In This Game: "<<numOfBombs;
// Process Guesses
do {
cout<<"\nPlease Input your ROW & COLUMN Guess coordinate (i.e. 3 2): ";
cin>>rowGuess>>columnGuess;
if (board [rowGuess] [columnGuess] >= 85) {
bomb = true;
cout<<"\n\n\t\tXXXXXX BOMB HIT XXXXXXX\n\t\t\tGame Over.\n\n";
cout<<"Your Score Score: "<<score;
cout<<"\n\n Play Again (y/n):";
cin>>newGame;
switch (newGame) {
case 'y':
cout<<"\n\n";
playAgain = true;
break;
default:
playAgain = false;
break;
}
} else {
wasGuessed [rowGuess] [columnGuess] = true;
score++;
reDraw(wasGuessed, 10, 10);
}
} while (!bomb);
}
return 0;
}
void reDraw (bool guessed [] [10], int rows, int columns)
{
// Format row and column headers
cout<<" 0 1 2 3 4 5 6 7 8 9\n";
cout<<"0";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if ((j+1) % 10 == 0)
if ((i+1) == 10) {
if (guessed [i] [j])
cout<<"X "<<" \n";
else
cout<<"? "<<" \n";
}else{
if (guessed [i] [j])
cout<<"X "<<" \n"<<i+1;
else
cout<<"? "<<" \n"<<i+1;
}
if ((j+1) % 10 != 0)
if (guessed [j] [i])
cout<<" X"<<" ";
else
cout<<" ?"<<" ";
}
}
}
void newBoard (int rows, int columns)
{
cout<<" 0 1 2 3 4 5 6 7 8 9\n";
cout<<"0";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if ((j+1) % 10 == 0)
if ((i+1) == 10) {
cout<<"? "<<" \n";
}else
cout<<"? "<<" \n"<<i+1;
if ((j+1) % 10 != 0)
cout<<" ?"<<" ";
}
}
}
Thanks For Your Help Guys!
I was bored so I made your program better. It’s all in one file, but it would be easy enough to split it into multiple. Just separate out the members and class definitions for each class, and put them in their own files, and put main in its own file.
I do a few things that might strike you as peculiar. If you see something, ask about it in a comment and I’ll explain myself
This code exhibits the following traits that your original version did not: