What's new

C & C++ Pa help sa error

Status
Not open for further replies.

Augustus69

Forum Veteran
Established
sa case 1: logical error ata ako nag kamali di ko na ma figure out

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


const int board = 5;


struct box {

int bomb;

int hmbombs;// hm = how many

int row,column;

};




int main() {

int num;
box bomb[board][board];
int row,column;
int hmbombs;
bool isRevealed;




cout << "MINE GAME" << endl;
cout << "1. Play\n";
cout << "2. Tutorial\n";
cin >> num;
cout << endl;


switch (num) {

case 1:
cout << "Enter number of bombs (1-3): ";
cin >> hmbombs;
if (hmbombs > 3){
cout << "Invalid number of bombs." << endl;
break;

}

for (int n = 0; n < hmbombs; ++n) {
int randRow = rand() % board;
int randCol = rand() % board;
bomb[randRow][randCol].bomb;

}

do{
cout << " 1 2 3 4 5" << endl;
for (int i = 0; i < board; ++i) {
cout << i + 1 << " ";
for (int j = 0; j < board; ++j) {
if (bomb[j].bomb == board ) {

if (isRevealed) {
cout << "* ";

isRevealed = false;

break;

} else {

cout << "_ ";

}

} else {

cout << ". ";



}

}

cout << endl;

}



cout << "Enter choice" << endl << "row: ";

cin >> row;

if (row > 5) {

cout << "Invalid number" << endl;

continue;

}

cout << "column: ";

cin >> column;

if (column > 5 ) {

cout << "Invalid number" << endl;

continue;

}

cout << endl;



} while (true);


break;

case 2:

cout << "------------------------------ MINE GAME------------------------------"<< endl;

cout << "This game challenges players to strategically uncover tiles on a grid" << endl

<<"while avoiding mines.Each revealed tile displays nearby mine counts,"<<endl

<< "and successful moves increase the player's score multiplier. "<<endl

<<"However, hitting a mine results in immediate loss." << endl;

break;

default:

cout << "Invalid option." << endl;

break;

}


return 0;

}
 
Solution
I see the issue now. The problem lies in how you are generating the bomb locations and checking if a bomb exists at a specific location. Let's correct the logic:

1. When generating random bomb locations, you need to set the bomb value to indicate that a bomb is present at that location. Change the following line in your code:

C++:
bomb[randRow][randCol].bomb;

to:

C++:
bomb[randRow][randCol].bomb = 1; // Set bomb value to indicate bomb presence

2. When checking if a location has a bomb to print '*', you need to check if bomb[i][j].bomb is equal to 1 (indicating a bomb is present). Update the condition in your code as follows:

C++:
if (bomb[i][j].bomb == 1) {
    cout << "* ";
} else {
    cout << "...
I see the logical error in your code. In the part where you are generating random bomb locations, you are not setting the bomb value to indicate that a bomb is present at that location. Here is the corrected part of your code:

C++:
for (int n = 0; n < hmbombs; ++n) {
    int randRow = rand() % board;
    int randCol = rand() % board;
    bomb[randRow][randCol].bomb = 1; // Set bomb value to indicate bomb presence
}

Also, in the part where you are checking if a location has a bomb to print '*', you need to check if bomb[i][j].bomb is equal to 1, not equal to board. Here is the corrected part of your code:

C++:
if (bomb[i][j].bomb == 1) {
    if (isRevealed) { 
        cout << "* ";
        isRevealed = false;
    } else {
        cout << "_ ";
    }
} else {
    cout << ". ";
}

With these corrections, your code should work more accurately. Let me know if you need further assistance!
 
I see the logical error in your code. In the part where you are generating random bomb locations, you are not setting the bomb value to indicate that a bomb is present at that location. Here is the corrected part of your code:

C++:
for (int n = 0; n < hmbombs; ++n) {
    int randRow = rand() % board;
    int randCol = rand() % board;
    bomb[randRow][randCol].bomb = 1; // Set bomb value to indicate bomb presence
}

Also, in the part where you are checking if a location has a bomb to print '*', you need to check if bomb[i][j].bomb is equal to 1, not equal to board. Here is the corrected part of your code:

C++:
if (bomb[i][j].bomb == 1) {
    if (isRevealed) {
        cout << "* ";
        isRevealed = false;
    } else {
        cout << "_ ";
    }
} else {
    cout << ". ";
}

With these corrections, your code should work more accurately. Let me know if you need further assistance!
Screenshot_2024-05-07-14-55-24-69.jpg

mali pa rin
 

Attachments

I see the issue now. The problem lies in how you are generating the bomb locations and checking if a bomb exists at a specific location. Let's correct the logic:

1. When generating random bomb locations, you need to set the bomb value to indicate that a bomb is present at that location. Change the following line in your code:

C++:
bomb[randRow][randCol].bomb;

to:

C++:
bomb[randRow][randCol].bomb = 1; // Set bomb value to indicate bomb presence

2. When checking if a location has a bomb to print '*', you need to check if bomb[i][j].bomb is equal to 1 (indicating a bomb is present). Update the condition in your code as follows:

C++:
if (bomb[i][j].bomb == 1) {
    cout << "* ";
} else {
    cout << ". ";
}

With these corrections, your code should now correctly handle bomb generation and checking for bomb presence. Let me know if you encounter any more issues!
 
Solution
Status
Not open for further replies.

Similar threads

Back
Top