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 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!? If you got spare time to look at my code i will very much appreshiate it!

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`

Your problem is that '0' is not the same as 0. '0' is actually 0x30 (IIRC).

However your entire algorithm has a fatal flaw. You should realize that rand() is random (or at least relatively close to random) which mean that any sequence of numbers is fair game for it. What would happen to your code if, for example, rand() always returned 4 (http://xkcd.com/221/) of course rand() shouldn't do this, but that doesn't mean it can't.

Instead you should use a shuffling algorithm. In general you take some list and swap random elements as many times as you want. For example:

void shuffle(int array[], int array_size)
{
    for (int i=0; i<array_size; ++i)
    {
        //swap(array[rand()%i],array[i]);
        int rnd=rand()%i;
        int tmp=array[rnd];
        array[rnd]=array[i];
        array[i]=tmp;
    }
}

swap random elements as many times as you want.

The canonical way to generate an unbiased random permutation in linear time is the Fisher-Yates shuffle
http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Using the standard library:

#include <algorithm>
#include <random>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <iomanip>

int main()
{
    constexpr std::size_t N = 6 ;
    int a[N][N] ;
    auto begin = std::begin( a[0] ) ;
    auto end = std::end( a[N-1] ) ;

    std::iota( begin, end, 1 ) ;
    std::srand( std::time(nullptr) ) ;
    std::seed_seq seed_seq { std::rand(), std::rand(), std::rand(), std::rand() } ;
    std::shuffle( begin, end, std::mt19937(seed_seq) ) ;

    for( const auto& row : a )
    {
        for( int i : row ) std::cout << std::setw(3) << i ;
        std::cout << '\n' ;
    }
}

http://ideone.com/LBVPbt

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.