If someone can simplify this code I will appretiate since I don;t have time... !!!!

Thank you in advance..!!
Regards..!!!

CODE:::

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int DisplayMenu();       //function that display the menu of the program
int Quit();             //quit function of the game
int Replay();          //replay function of the game(if the user wants to quit)
int BoardGenUSER();   //board user generation..... This function generate the 2nd 2D array based on the user input Columns and Rows
int Random(int i, int b);
void reveal(int x);

int b=8;                     // b= 8 is the number of the 2D array....

int main() { // Int main of the whole program...

    DisplayMenu();    //called the function
    return 0;   // a returning value (since compiler set warning message)
}

int DisplayMenu()   //function of main menu of the program
{
    char opt;     //char variable
            cout << "\nSelect One Option!";
            cout << "\n------------------";    //main menu of the program
            cout << "\n1|Game.";               // prints out all the options
            cout << "\n2|Exit.";
            cout << "\nOption: ";
            cin >> opt;                       // check the option of the user

            switch (opt)            //switch case of the user input
            {
            case '1': BoardGenUSER();
                break;               // break of case 1
            case '2': Quit();           // case 3: called the function quit
                break;                  // break of case 3
            default: cout << "Error input, The program will exit.!!"<<endl; return 0;  //default: any other character will print out error
            }                                                                       //message and exit the program.....
            return 0;
}

int Quit()     //quit function
{
    char opt;   // declaration values

    do     // do of while loop below
    {
        cout << "Do you want to exit(y/n)? ";
        cin >> opt;    // asked from user to enter an input in char
if (opt=='n' || opt=='N')   // if statement.!! Checks if the input is == to a specific char
{
    cout << string(25, '\n'); // if it's not since I can't used cls am using linux distribution....
    return main(); //Rerunning into main menu           //  i set a string value of 25 and change line to clear the screen
}
    }while(opt!='Y' && opt!='y');   // while the input of the user is not = with the specific char (Y)-(y)
                                    // goes into do
return 0;  // returning value..!!
} // end of function quit!!!

int Replay()   // replay function (when the game is done asking the user if wants to play again or not)
{
           char a;   //variables
           cout << "1) Replay 2) Quit" << endl;
           cin >> a;//asked from the user to set a char
           switch(a)  //switch case of the input that the user give....
           {  // started of switch case
                         case '1':    //case 1
                             return main();   //the program return into the main menu
                                    break;  //break of case 1
                         case '2':   //cace 2
                                    cout << "Quit" << endl;
                                    break;  //break of case 2      //the program exit
                         default:          // default when the user enter invalid input..
                                    cout << "Invalid input" << endl;    //print out error message
                                    Replay();                           // and return into the function replay (called the function)
                                    break;   //break  of default
           }   //end of switch case
           return 0; //returning value
}

int BoardGenUSER()
{
           int board[b][b];             //0 - 8 = # of mines, 9 is mine
           int revealed[b][b];         //1 is revealed second 2D array for the showed
           int i = 0;                 //Declared the i and j values of nested loops here since I am passing it throw the function Random
           int j = 0;                //  >>           >>                   >>              >>              >>           >>
           int x = 0;               //columns of table
           int y = 0;              //rows of table
           int z = 10;            //number of BOOMS/MINES
           int q;                //counter
           int dead = 0;        //dead value that check when the game is over or not.. I am prefer to using integer instead of boolean!!!!!!

           for(i=0;i<b;i++)                              // for loop of i rows...these nested loop are not responsible to create the table just
                         for(j=0;j<b;j++)               //  for loop of j columns... initial the board end set the 2D array into 0 to be able to Randomly selected positions
                                    board[i][j] = 0;   //set the 2D board to 0
           i = Random(i, b);                          //called the function Random to generate the booms and passing into the table based on
           j = Random(j, b);                         // numbers of the board in our case 8x8 by i and j of the loop statement..

           cout << "Generating board..." << endl;    // printing generating message
           do  // do of while 'Z' statement....
           {
                         i+=3;                   // changing the values of  i and j and passing to generate the board... PRIVIOUS it was for the position NOW it's for the WHOLE program
                         j+=6;
                         x = Random(i, b);          // called the function Random as X-axis and Y-axis where X = columns and Y = Rows
                         y = Random(j, b);
                         if(board[y][x] != 9)                  //checking if the board = not = into 9
                         {
                                    board[y][x] = 9;                //set the mine 9 and each time goes --z until the mines ends....
                                    z--;                           // z-- for the mines
                                    if((y-1) >= 0)
                                    if(board[y-1][x] != 9)
                                                  board[y-1][x]++;
                                    if((y-1) >= 0 && (x-1) >= 0)
                                    if(board[y-1][x-1] != 9)
                                                  board[y-1][x-1]++;                       // a little bit of mess here... :P
                                    if((x-1) >= 0)                                        // as a result the specific if statements is
                                    if(board[y][x-1] != 9)                               // checking for first time the X - Y axis and then
                                                  board[y][x-1]++;                      // each time increase the value of the board variable
                                    if((y+1) < b)                                      // by one but an the same time decrease or increase (deepens of the value of board)the internal position of
                                    if(board[y+1][x] != 9)                            //table/2D array so each time generating a position of X and Y
                                                  board[y+1][x]++;
                                    if((y+1) < b && (x+1) < b)
                                    if(board[y+1][x+1] != 9)
                                                  board[y+1][x+1]++;
                                    if((x+1) < b)
                                    if(board[y][x+1] != 9)
                                                  board[y][x+1]++;
                                    if((y-1) >= 0 && (x+1) < b)
                                    if(board[y-1][x+1] != 9)
                                                  board[y-1][x+1]++;
                                    if((y+1) < b && (x-1) >= 0)
                                    if(board[y+1][x-1] != 9)
                                                  board[y+1][x-1]++;
                         }
           }while(z>0);    //while statement of mine is grater than 0 then goes up INTO do
           for(i = 0; i < b; i++)                // nested for loop for the revealed i and j as they showed....
                         for(j=0;j<b;j++)
                                    revealed[i][j]=0;
                                                   //board creation end..... Now goes into the input & checking...
                                                   //ask for input/check squares loop
           do
           {
                         system ("clear"); //OR//  --->>>>> /cout << string(25, '\n'); what ever is works for you....
                         q = 0;    // counter of checking boarding and dies.. SEE lines:: 175/176/181
                         z = 0;    //set parameter z into 0 z is the number of the booms that they will generate on the random function SEE LINES 177/178/181
                         cout << "   ";     //prints spaces
                         for(i=0;i<b;i++)
                                    cout << i << " ";
                         cout << endl;
                         for(i=0;i<b;i++)
                         {
                                    for(j=0;j<b;j++)
                                    {
                                                  if(j==0)
                                                                cout << i << " |";
                                                  if(revealed[i][j]==1)
                                                                reveal(board[i][j]);
                                                  else
                                                                cout << "_|";
                                                  if(j==(b-1))
                                                                cout << endl;
                                                  if(board[i][j]!=9 && revealed[i][j]==1)
                                                                q++;
                                                  if(board[i][j] == 9)
                                                                z++;
                                    }
                         }
                         if(q >= ((b*b) - z))
                         {
                                    cout << "You win!" << endl;
                                    dead = 1;
                         }
                         if(dead == 0)
                         {
                         cout << "X: ";
                         cin >> x;
                         cout << "Y: ";
                         cin >> y;
                         }
                         if(board[y][x] == 9)
                         {
                                    system ("clear");
                                    cout << "You hit a mine!" << endl;
                                    cout << "   ";
                                    for(i=0;i<b;i++)
                                                  cout << i << " ";
                                    cout << endl;
                                    dead = 1;
                                    for(i=0; i<b; i++)
                                    {
                                                  for(j=0;j<b;j++)
                                                  {
                                                                if(j==0)
                                                                           cout << i << " |";
                                                                if(board[i][j]==9)
                                                                           revealed[i][j]=1;
                                                                if(revealed[i][j]==1)
                                                                           reveal(board[i][j]);
                                                                else
                                                                           cout << "_|";
                                                                if(j==(b-1))
                                                                           cout << endl;
                                                  }
                                    }

                         }
                         if(board[y][x]==0)
                         {
                                    revealed[y][x] = 1;
                                    for(i=0;i<b;i++)
                                    {
                                                  for(j=0;j<b;j++)
                                                  {
                                                                if(i>(y-2)&&i<(y+2))
                                                                           if(j>(x-2)&&j<(x+2))
                                                                                         if(board[i][j]!=9)
                                                                                           revealed[i][j]=1;
                                                  }
                                    }
                         }
                         if(board[y][x]>0 && board[y][x]<9)
                                    revealed[y][x] = 1;
           } while(dead == 0);
           if (dead == 1)
                         Replay();
           return 0;
}

void reveal(int x)  // this function display the board.. actually generate the board of x = 0 booms x == 9 and befor the user plays
{
           if(x == 0)    //if statement.. of  x == 0
                         cout << "o|";   //prints out
           else if(x == 9)    //else if of x ==9 when the user hit a boom
                         cout << "x|";   //prints out results
           else    //else
                         cout << x << "|";  //prints out tables separate
}

int Random(int i, int b)   //this function generate Random numbers of boarding
{                         // for example every time that the user starts a new GAME this function generate different type
           long ran;      // of the actually positions of the booms... With other words this function select Randomly possitions
           int t = time(0);  // into the table and set the booms.....
           srand(t);
           ran = rand();
           ran = ran / (ran * i) + (i * 1337);     // NOTE:: I WILL TRY TO FIND OTHER WAY TO DO THAT BUY FOR NOW I LEAVE IT
           ran = ran % b;                         // THIS is not me... It's HELPS me one friend to to that... he also explain to me
                                                  // but unfortunately i don't understand..
           return ran;
}

Recommended Answers

All 2 Replies

Maybe, in advance, you could give us a brief description of what the code is supposed to do.

thank you for your reply gerad4143: It's a game:: Minesweeper game.. i

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.