Thread: Randomizing Multidimensional Arrays

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    Randomizing Multidimensional Arrays


    Hello, I'm new here and I came with a problem in mind. The problem is that I'm stumped. I've written a 9x9 multidimensional array with 15 X's and 66 blank spaces and what I would like to do is jumble up the existing spaces so that the X's are placed randomly everytime I start the program. I understand how to seed random numbers for say a possibility of 1-7 flying spaghetti monsters to appear out of nowhere but when it comes to randomizing multidimensional arrays, I'm clueless.

    Alternatively I've considered having all spaces blank and seeding 15 X's but then again, I don't know how to do that either. Any suggestions?

    Here's what I got so far:


    Code:
    #include
    <iostream>
    #include
    <cstdlib>
    #include
    <ctime>
    #include
    <string>
    using
    namespace std;
    int
     main()
    {
        
    constint ROWS = 9;
        
    constint COLUMNS = 9;
        
    char board[ROWS][COLUMNS] = {{'O' , 'X' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                     {
    'O' , 'O' , 'O' , 'O' , 'O' , 'X' , 'O' , 'X' , 'O'},
                                     {
    'O' , 'O' , 'O' , 'X' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                     {
    'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'X' , 'O' , 'O'},
                                     {
    'O' , 'O' , 'X' , 'O' , 'O' , 'O' , 'O' , 'O' , 'X'},
                                     {
    'X' , 'O' , 'O' , 'O' , 'O' , 'X' , 'O' , 'O' , 'O'},
                                     {
    'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                     {
    'O' , 'X' , 'O' , 'X' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                     {
    'O' , 'O' , 'X' , 'O' , 'O' , 'X' , 'O' , 'X' , 'X'}};
        
    for (int i=0; i < ROWS; ++i)
        {
            
    for (int j=0; j < COLUMNS; ++j)
            {
                cout << board[i][j];
            }
            cout << endl;
        }
      
        system (
    "pause");
    return
     0;
    }


  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    • You should be using #defines instead of const ints for constant values
    • Your indentation and formatting are terrible.
    • Don't use system("pause") because it's not portable.
    • If you want an array of O's with 15 randomly placed X's, you shouldn't hard-code the X's. Instead, you should do something like this:
      Code:
      char board[ROWS][COLUMNS] = { 'O' };
      and then place the X's.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should be using #defines instead of const ints for constant values
    This is C++, in C++ the use of const int is prefered over using #define. The const int is type safe where #define is not.

    Jim

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Quote Originally Posted by memcpy View Post
    • Your indentation and formatting are terrible.
    • Don't use system("pause") because it's not portable.
    • If you want an array of O's with 15 randomly placed X's, you shouldn't hard-code the X's. Instead, you should do something like this:
      Code:
      char board[ROWS][COLUMNS] = { 'O' };
      and then place the X's.
    First, what do you mean "Your indentation and formatting are terrible"? That's only what the code looks like after copy/pasting it onto this site. It looks alot better in the actual file.

    http://i897.photobucket.com/albums/a...Untitled-4.jpg

    Second, I use system("pause") because the executable ends the moment it initializes unless I have some sort of door stopper. Do you recommend I use cin ques, instead?

    Third, I'll create an array of O's, then and have 15 X's replace random O's. How do I go about writing the code for that, though? That's the reason I created this thread.
    Last edited by DarkSpyda04; 09-12-2012 at 07:24 PM.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > First, what do you mean
    Your indentation/formatting on the forum is terrible. There's a button that removes all preexisting formatting when you post.

    > Do you recommend I use cin ques, instead?
    Or any of the solutions here.

    > How do I go about writing the code for that, though?
    Loop fifteen times and pick a random X and Y value between 0 and 8 each time. If the board at that spot is an O, place an X; otherwise, continue generating numbers until you hit a spot that's open.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    I am not in the right mood right now and that I can write down on a note and hand to you. This infernal, god forsaken internet connection has gone down not once, but twice so far the very second I tried submitting a reply. Third times a charm but seriously, I'm ready to pull the hair out of my head and sell it on Ebay.

    Now what I was going to post was that much to my own amazement, I've made progress within 15-20 minutes of your most recent reply, thanks to your most recent reply. I'd gotten the program to look through the array and replace O's with X's at random. Not all went well, however as only one of 81 total O's had been replaced; leaving 14 X's unused. This is due to the "break" no doubt but it's the only way I know to end the loop after a condition is met. Preferably there's a way to not end the loop, but bring it back to the top if the condition is met.

    I've been trying for hours on end and every atempt has only caused the array to disappear entirely when the compiler is run. The code below depicts a program that displays a 9x9 array of O's and yet only a single X. If there's a way to return to the top of the loop instead of breaking it, there might be 15 X's.

    PS: I've already tried using a "continue" but like many other a solution it erradicates the entire array.


    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    
     
    using
    namespace std;
    
    int main()
    
    {
     
    
    //Initializes a 9x9 array of O's
    
    const int ROWS = 9;
    const int COLUMNS = 9;
    char board[ROWS][COLUMNS] = {{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
    
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
    
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
    
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
    
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
    
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
    
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
    
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
    
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'}};
    
    
     
    
    //Replaces random O's with X's
    
    int counter = 0;
    
        do
    
          {
              
            srand(static_cast<unsigned int>(time(0))); //Seed Random number
            int randomGen = rand(); // Generate random number
            int randomNum = (randomGen % 8) + 0; // Get random number between 0-8
    
     
          if (board[randomNum][randomNum] != 'O')
    
            {
                break;
            }
            
    
          else
    
            {
              board[randomNum][randomNum] = 'X';
              counter = counter + 1;
            }   
          }
        
    while (counter != 15);
    
     
    
    
    //Displays the array
    
    for (int i=0; i < ROWS; ++i)
    
        {
    
            for (int j=0; j < COLUMNS; ++j)
    
            {
                cout << board[i][j];
            }
            cout << endl;
        }
    
     
    
    
    //End of file
    
        cin.get();
    
    return 0;
    
    }


    Example of output:

    OOOOOOOOO
    OOOOOOOOO
    OOOOOOOOO
    OOOOOOOOO
    OOOOOOOOO
    OOOOOOOOO
    OOOOOOXOO
    OOOOOOOOO
    OOOOOOOOO
    Last edited by DarkSpyda04; 09-12-2012 at 11:06 PM.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    First, srand() must be called ONLY ONCE in the program. You are doing it every time through the loop and undoing the randomness each time. Please don't ask where to put it. You can figure that out yourself.

    Second, you need an row and column chosen randomly. What you get with your code is loading the diagonal.

    Third, remove the break. You don't want to break, you want to continue through the loop. You will exit the loop one 15 values have been placed -- that part of your code is correct.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Since you're having trouble with this, here's some "pseudo"code that might work:

    Code:
    for (i = 0; i < 15; i++) {
        xind = rand() % ROWS;
        yind = rand() % COLUMNS;
    
        if (board[xind][yind] == 'X')
            i--;
        else
            board[xind][yind] = 'X';
    }

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    I'll just go and say that i've had the program completed since 6:00PM but hadn't had the time to post the results until now. Thanks to memcpy's snippet of code, the program now successfully replaces 15 O's with X's instead of just one. Additional thanks to everyone else for the support. I wouldn't have been able to get this program running by now if it weren't for the assistance.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    
     
    using namespace std;
    int main()
    {
     
        
    
    //Initialize 9x9 grid
    
        
    const int ROWS = 9;
    const int COLUMNS = 9;
        
    char board[ROWS][COLUMNS] = {{'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'},
                                 {'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O'}};
    
    
     
     
            
    //Welcome player and explain how to play game
    
            cout << "Welcome to [Name Witheld]'s Minesweeper: Duke Nukem Edition" << endl;
            cout << "Instead of placing flags, you must place a single bullet" << endl;
            cout << "where you believe a mine is to blow them all up." << endl;
            cout << "If you run out of ammo, you lose.\n" << endl;
            cout << "Below is the 9x9 grid:\n\n" << endl;
    
     
     
        srand(static_cast<unsigned int>(time(0))); //Seed Random number
     
        
    
    
    //Replace 15 O's with X's
    
        
    for (int count = 0; count < 15; count++) //Continue loop until 15 O's were replaced with X's
        {
            
    int randomNumberX = rand() % ROWS; //Assign random number to i
    int randomNumberY = rand() % COLUMNS; //Assign random number to j
     
            
    
    //Check if slot is not already an X. If not, replace existing character with X.
    
            
    if (board[randomNumberX][randomNumberY] == 'X')
            {
                randomNumberX--;
            }
            
    else
            {
                board[randomNumberX][randomNumberY] = 'X';
            }
        }
     
        
    
    
    //Displays 9x9 tile grid
    
        
    for (int i=0; i < ROWS; ++i)
        {
            
    for (int j=0; j < COLUMNS; ++j)
            {
                cout << board[i][j];
            }
            cout << endl;
        }
     
     
        
    
    
    //Present player a list of options
    
            
    int modeChoice;
        
            cout << "\nEnter '1' to enter reticle placement mode." << endl;
            std::cin >> modeChoice;
     
     
        
    
    
    //Gunfire program is chosen
        
    //Get player input
    
        
    if (modeChoice == 1)
        {
        
    int ammo = 1;
        
    int fireInputX;
        
    int fireInputY;
    
        
            cout << "Select 'X' and 'Y' values between 0-8" <<endl;
            cout << ammo << " round(s) are in the chamber.\n" << endl;
            cout << "Enter fire input Y" << endl;
            std::cin >> fireInputY;
            cout << endl;
            cout << "Enter fire input X" << endl;
            std::cin >> fireInputX;
            cout << endl;
    
            
            
    
    //If coordinates are an 'X', you win
    
            
    if (board[fireInputY][fireInputX] == 'X')
            {
                cout << "YOU WIN" << endl;
                cin.get();
               
            }
            
    
    
    //If coordinates are an 'O', you lose
    
            
    elseif (board[fireInputY][fireInputX] == 'O')
            {
                ammo == ammo - 1;
                cout << "YOU LOSE" << endl;
                cin.get();
            }
        }
        
     
        cin.get();
    return
     0;
    }
    

    Example of Output:

    Welcome to [Name Witheld]'s Minesweeper: Duke Nukem Edition
    Instead of placing flags, you must place a single bullet
    where you believe a mine is to blow them all up
    If you run out of ammo, you lose
    Below is the 9x9 grid:

    XOOOOOOXO
    OOOOOXOOO
    OOXOOOOOO
    OOOOXOOXO
    OXOOOOXOO
    OOOOXOOOO
    OOOOOXOOO
    OOXOOOOXO
    OOOXOOXXO

    Enter '1' to enter reticle placement mode.
    1

    Select 'X' and 'Y' values between 0-8
    1 round(s) are in the chamber.

    Enter fire input Y
    5

    Enter fire input X
    4

    YOU WIN
    Last edited by DarkSpyda04; 09-13-2012 at 11:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomizing arrays!!
    By vulock_ka in forum C Programming
    Replies: 15
    Last Post: 08-02-2010, 11:52 AM
  2. C multidimensional arrays
    By sin2win in forum C Programming
    Replies: 6
    Last Post: 02-19-2010, 09:15 PM
  3. Multidimensional Arrays
    By fhbwghads in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2008, 09:56 PM
  4. Multidimensional Arrays
    By vutek0328 in forum C Programming
    Replies: 22
    Last Post: 10-13-2006, 05:12 AM
  5. Multidimensional Arrays
    By homeyg in forum C++ Programming
    Replies: 11
    Last Post: 03-07-2006, 10:08 PM