im trying to create a class that will create a 20x20 grid with 5 random x's and 100 random o's. But its currently giving me ascii chars instead. any help would be appreciated!

#include "Lifegrid.h"

using namespace std;

int main()
{
 int r1;
 int r2;
 int r3;
 int oldi;
 int oldj;
 int count1;
 int count2;
char grid[20][20];  
 Lifegrid mygrid;
 
mygrid.drawgrid();
mygrid.printGrid();
system("pause");
return 0;

}

Lifegrid.h:

#ifndef GRID
#define GRID

#include <iostream>
#include <string>
#include<cstdlib>

using namespace std;

class Lifegrid
{	
            
protected:
         int r1;
         int r2;
         int r3;
         int oldi;
         int oldj;
         int count1;
         int count2;
         char grid[20][20];        
public: 
       void drawgrid()
        {                        
               for(int i=0;i<20;i++)
               {
                for(int j=0;j<20;j++)
                   {
                         srand((unsigned)time(0));
                          r1 = rand()%2;
                          r2 = rand()%20;
                          r3 = rand()%20; 
                          if(r1 ==1)
                            {
                               oldi = i;
                               oldj = j;
                               i = r2;
                               j = r3;
                               if((grid[i][j]=='x')||(grid[i][j]=='o')||(count1>=5))
                                  {
                                    i=i-1;
                                  }
                               else
                                  {
                                    grid[i][j] = 'x'; 
                                    count1++;     
                                  }  
                                    i = oldi;
                                    j = oldj;                   
                              }          
                              if(r1==0)
                              {
                                  oldi = i;
                                  oldj = j;
                                  i = r2;
                                  j = r3;
                                    if((grid[i][j]=='x')||(grid[i][j]=='o')||(count2>=100))
                                      {
                                         i=i-1;                                       
                                      }
                                      else
                                      {
                                      grid[i][j] = 'o';  
                                        count2++;    
                                      } 
                                      i = oldi;
                                      j = oldj;
                              }
                       }           
                }       
        } 
        void printGrid() 
        {
            for(int i=0;i<20;i++)
            {
                for(int j=0;j<20;j++)
                {    
                   cout << grid[i][j] ;     
                }
           cout << endl;               
             }
        }
      
};

#endif

Recommended Answers

All 8 Replies

Move line 25 out of that class. srand() should be called only one during the lifetime of the program. I normally put it near the beginning of main().

Move line 25 out of that class. srand() should be called only one during the lifetime of the program. I normally put it near the beginning of main().

ok i've moved it into the intmain() but im still getting the random ascii chars

You need to initialize the array with all '\0' before doing anything.

ok i've put in the following method:

void creategrid()
        {
             for(int i=0;i<20;i++)
               {
                for(int j=0;j<20;j++)
                   {
                    grid[i][j] =='.';
             
                   }
                  
                }
        }

problem still there :(

#include <time.h> // for time(NULL)

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

srand(time(NULL)); // seed the random number generator
vector<bool> field;
field.resize(400); // == 20 x 20
for(int i=0;i<5; i++) field[i] = true; // the rest are all false already
random_shuffle(field.begin(),field.end()); // now the 5 x-s (true) are randomly distributed 
// now you can output your 20 x 20 grid
for(int y = 0; y < 20; y++)
{
for (int x = 0; x < 20; x++)
   cout << (field[y*20+x] == false?"o":"x");
cout << endl;
}

>> grid[j] =='.';

You used the wrong operator. Use the = operator, not the boolean == operator. grid[i][j] = '.';

>> grid[j] =='.';

You used the wrong operator. Use the = operator, not the boolean == operator. grid[i][j] = '.';

d'oh ;) im always doing silly things like that. well im not getting ascii anymore, but its only showing the 5 x's and not the o's

fixed it now :) thx for the help!

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.