Hi Im trying to create a random 2D array (3 x 3) so that numbers 1-9 will randomly be placed in the grid behind the '?' shown in coveredarray but in the guessarray just wanted to know if im doing right, im faily new to C++, also i wanted to ask how I would go about asking the user to pick what row and column to choose to reveal what numbers behind the '?'

Thanks

   int coveredarray [3][3] =    {{'?','?','?'},                           
                                {'?','?','?'},
                                {'?','?','?'}};


    for(int row = 0; row < 3; row++)
    {
        for(int column = 0; column < 3; column++)
        {
            cout << coveredarray[row][column] << " ";

        }
            cout << endl;
        }
//This is the Guess Array

    int guessarray[3][3]; {{'?','?','?'},                             
                           {'?','?','?'},
                           {'?','?','?'}};
    *srand(time(0));  // Initialize random number generator.
    guessarray = (rand() % 3) ;
    if (guessarray>1 && guessarray <9);
    for(int row = 0; row <3; row++)
    { for(int column = 0; column <3; column++)
        { cout << guessarray[row][column];
        }
    }

        }

First off your coveredarray should be of type char not int. Secondly what I would do is load guessarray with the numbers 1-9 in order. Then I would go through the array and swap the element of the current index with one that is randomly generated. To do that take a look at this:

int randI, randJ, temp;
// seed random generator
int number[3][3];  // make this hold 1-9

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        randI = rand() % 3;  // get random index
        randJ = rand() % 3;  // get random index
        temp = number[i][j];  // store number from current pos.
        number[i][j] = number[randI][randJ];  assign current pos with random
        number[randI][randJ] = temp;  // assign random pos with current
    }
}

To ask the user what spot they want you can ask them for the row and coulmn and use that.

This is my full code, how do i go about changing the Playarray so that user can be asked to enter a row and column number to type in a number between 1-9 and see if it matches the number placed there by the random array i also want to allow the game to remember it and saying it's only got a limited of number turns left, i dont know if i've done it right!?

Thanks

`Inline Code Example Here`
#include<iostream>
#include<conio.h>
#include<string>
#include<cstdlib>
#include<ctime>

using namespace std;

int pause()                     //Funtion for pause
{
    int in;
    scanf("%c",&ch);            
    getint();                   
    return 0;
}

int main ()
{


    cout<< "Welcome to Guess Game" <<endl; //I'm inserting a message so that when the game loads the user will be welcomed into the game.


//This is the Character Array   
 char coveredarray [3][3] =      {{'?','?','?'},                              
                                {'?','?','?'},
                                {'?','?','?'}};


    for(int row = 0; row < 3; row++)
    {
        for(int column = 0; column < 3; column++)
        {
            cout << coveredarray[row][column] << " ";

        }
            cout << endl;
        }
//This is the Guess Array

    int myarray[3][3]={{'0','0','0'},
                       {'0','0','0'},
                       {'0','0','0'}}

    srand (unsigned time())
    int numbersentered=0;
    for (int i=1; i<=9; i++)
    {
        while (i>numbersentered)
        {
            int x=rand()%3;
            int y=rand()%3;
            if (myarray[x][y]==0)
            {
                myarray[x][y]=i;
                numbersentered++;
            }
        }
    } 

     int playarray[3][3];{{'?','?','?'},                              
                          {'?','?','?'},
                          {'?','?','?'}}; //im creating a memory array

     int row, column;
     int turn= 1, mine_ctr=0; 
     int play_row, play_column;

     while (turn < 9) { //I'm going to use While Loops
     cout<< " "<< playarray <<endl;
     cout<<endl;

     if((play_row > 1) && (play_column < 3))
     {
         cout<< "Please enter a number of row and column" <<endl; //This is a message telling the user to enter a number between 1 and 9
         system ("pause") ;
         continue;

     if((guessarray[play_row-1][play_column-1] <= 9) 
         cout << "Number found" << endl; //I'm creating a message which will tell them that they've found a mine.
         displayarray[play_row-1][play_colum-1] ='' ;
         mine_ctr++;//This will keep a track of Mines located
         for (int row=0; row<3; row++)
         {
             cout<<coveredarray [row][column]<<" ";
         }

         cout<<endl;
     }
     }
     else
     {
         cout << "Wrong Number" << endl; //This is going to input a message when no mines are found.
         displayarray[play_row-1][play_column-1] = '?';

          cout << endl;
             }

             if(guessarray[play_row-1][play_column-1] == 1;
             {
                 cout << "Please choose another position" //I'm creating a message so that if the user doesnt put a number 1-5 they will be reminded to do so.
                 system("pause");
                 continue;

                 guessarray[play_row-1][play_row-1]=1;

             if(guess_ctr =9)
             {
                 cout << "You have found all the numbers" << endl; //This message will appear if the user has found all 10 mines.
                 break;
             }

             turn++;

         }
         if (turn==10)
         {
             cout << "Unfortunatly you have lost" << endl; //This is a message telling them that have basically lost.

_getch()
}

Inline Code Example Here

Edited 3 Years Ago by cedwards

This article has been dead for over six months. Start a new discussion instead.