Hey all, am stuck! Basically I have a version of the Game of Life to create and I am currently having trouble with moving the items in the array.

Basically my code shows the 20x20 grid, populates it with .'s to start, then adds 5 x's and 100 o's. That works (although the o's overwrite, also need help with that?) but the movement code doesn't, which is located towards the end within the DO WHILE loop. It is basically saying to move an o or an x based on the stipulation I have set in the code:-

An X can replace a . and/or an o, but an O can only replace a .

It compiles and runs but when I enter to continue it just shows me the same grid again, when in fact it should have moved.

Any help appreciated thanks.

P.S Nice to be on here at last

#include <iostream>
#include <string>  
#include <ctime>
#include <cstdlib>  
   
       
   
using namespace std;   
int main()
{ 
      srand((unsigned)time(0));
       
      char board[20][20];

      cout << "" <<endl;
      cout << "...................................." << endl;      
      cout << "............GAME OF LIFE............" << endl;
      cout << "...................................." << endl; 
      

      //Creates the initial grid with just .'s
      for(int col=0; col<20; col++)
      {
              for(int row=0; row<20; row++)
              {
                     board[row][col]='.';
              }
      }
  int odirection;
  int option;
  int xdirection;    
  int x;
  int y;
     //Populating the grid

      for(int i=0;i<100;i++)
      {
        x = rand()%20;
        y = rand()%20;       
                   if(board[x][y]='.')
                   {
                       board[x][y] = 'o';
                   }
                    
      }
    
      for(int i=0;i<5;i++)
      {
       x = rand()%20;
       y = rand()%20; 
        if(board[x][y]='.')
        {
            board[x][y] = 'x';
        }
      }
      
  int count=0;
  int xcount=0;                          
      for(int col=0; col<20; col++)
      {
              for(int row=0; row<20; row++)
              {
                      cout<<board[row][col];          
                      if(board[row][col] == 'o')
                      {
                            count=count+1;
                      }
                      if(board[row][col] == 'x')
                      {
                          xcount=xcount+1;
                      }
                      
              }
              cout<<endl;
      }
     
do
{ 
      if(board[x][y] == 'o')
      {
              odirection = rand()%4;
              
              if(odirection==1)
              {
                     if((board[x+1][y])==(board[x][y]='o'))
                     {
                     }
                     if((board[x+1][y])==(board[x][y]='.'))
                     {
                       board[x][y] = 'o';
                       board[x-1][y] = '.';
                     }
                     if((board[x+1][y])==(board[x][y]='x'))
                     {
                     }
              }
              if(odirection==2)
              {
                     if((board[x-1][y])==(board[x][y]='o'))
                     {
                     }
                     if((board[x-1][y])==(board[x][y]='.'))
                     {
                       board[x][y] = 'o';
                       board[x+1][y] = '.';
                     }
                     if((board[x-1][y])==(board[x][y]='x'))
                     {
                     }
              }    
              if(odirection==3)
              {
                     if((board[x][y+1])==(board[x][y]='o'))
                     {
                     }
                     if((board[x][y+1])==(board[x][y]='.'))
                     {
                       board[x][y] = 'o';
                       board[x][y-1] = '.';
                     }
                     if((board[x][y+1])==(board[x][y]='x'))
                     {
                     }
              }  
              if(odirection==4)
              {
                     if((board[x][y-1])==(board[x][y]='o'))
                     {
                     }
                     if((board[x][y-1])==(board[x][y]='.'))
                     {
                       board[x][y] = 'o';
                       board[x][y+1] = '.';
                     }
                     if((board[x][y-1])==(board[x][y]='x'))
                     {
                     }
              }    
      }
      if(board[x][y] == 'x')
      {
              xdirection = rand()%4;
              
              if(xdirection=1)
              {
                     if((board[x+1][y])==(board[x][y]='o'))
                     {
                       board[x][y] = 'x';
                       board[x-1][y] = '.';
                     }
                     if((board[x+1][y])==(board[x][y]='.'))
                     {
                       board[x][y] = 'x';
                       board[x-1][y] = '.';
                     }
                     if((board[x+1][y])==(board[x][y]='x'))
                     {
                     }
              }
              if(xdirection=2)
              {
                     if((board[x-1][y])==(board[x][y]='o'))
                     {
                       board[x][y] = 'x';
                       board[x-1][y] = '.';
                     }
                     if((board[x-1][y])==(board[x][y]='.'))
                     {
                       board[x][y] = 'x';
                       board[x+1][y] = '.';
                     }
                     if((board[x-1][y])==(board[x][y]='x'))
                     {
                     }
              }    
              if(xdirection=3)
              {
                     if((board[x][y+1])==(board[x][y]='o'))
                     {
                       board[x][y] = 'x';
                       board[x][y-1] = '.';
                     }
                     if((board[x][y+1])==(board[x][y]='.'))
                     {
                       board[x][y] = 'x';
                       board[x][y-1] = '.';
                     }
                     if((board[x][y+1])==(board[x][y]='x'))
                     {
                     }
              }  
              if(xdirection=4)
              {
                     if((board[x][y-1])==(board[x][y]='o'))
                     {
                       board[x][y] = 'x';
                       board[x][y+1] = '.';
                     }
                     if((board[x][y-1])==(board[x][y]='.'))
                     {
                       board[x][y] = 'x';
                       board[x][y+1] = '.';
                     }
                     if((board[x][y-1])==(board[x][y]='x'))
                     {
                     }
              }    
      }
      cout << "" << endl;
      for(int col=0; col<20; col++)
      {
              for(int row=0; row<20; row++)
              {
                      cout<<board[row][col];
              }
              cout << ""<< endl;
      }
   
        cout << "Do you wish to continue? [1 = Yes | 2 = No]" << endl;
        cin >> option;
        
}while(option !=2);      
      
      cout << "" << endl;
      cout << "Number of Greenflies: " << count << endl;
      cout << "Number of Ladybirds: " << xcount << endl;
      system("Pause");
      return 0;
      }

It is just the moving code I have issues with I think, the long sets of IF Statements at the bottom.

well for starters the code you are using to populate the array with your o's and x's is not right. you are using a for loop but what would happen if the cords you get already have a o or an x? you would do nothing and then start the loop from the beginning. this would lead to not having enough o's or x's. i would suggest a while loop with a counter as the condition and you only increment the counter if the if statement executes. i believe the issue you are having with moving the board is that you are not changing the values of x or y so you are doing the same thing every time. also you never reset x or y to something after the last time you use it in your x for loop so x and y are still at the cord.

To move, you need another matrix. board is for the current state, newboard is for the next generation. Once you fill newboard copy it into board.

To move, you need another matrix. board is for the current state, newboard is for the next generation. Once you fill newboard copy it into board.

Do you mean instead of having it edi the board e.g :

          if(odirection=1)
          {
                 if((board[x+1][y])=(board[x][y]='o'))
                 {
                 }
                 if((board[x+1][y])=(board[x][y]='.'))
                 {
                   board[x][y] = 'o';
                   board[x-1][y] = '.';
                 }
                 if((board[x+1][y])=(board[x][y]='x'))
                 {
                 }
          }

I should change the values to new board, then make it equal to board after? slightly confused as relatively new to c++.

Thanks for replies thus far though.

I tried doing the counter with counter++ in the IF statement and it still does the same thing:-

  counter = 0;
  while(counter < 100)
  {
    x = rand()%20;
    y = rand()%20;
               if(board[x][y]='.')
               {
                   board[x][y] = 'o';
                   counter++;
               }
  }

It looks like you are doing assignments (=) instead of comparisons (==), so check your if-statements ..

// You want to have ..
if(board[x][y] == '.')
...

And please use code tags.

Yes I have done as you said, still does not work. Doesn't error but doesn't do as supposed to when moving randomly.

Also, the counter code still doesn't do 100 exactly :/

counter = 0;
      while(counter < 100)
      {
        x = rand()%20;
        y = rand()%20;
                   if(board[x][y]=='.')
                   {
                       board[x][y] = 'o';
                       counter++;
                   }

      }
if(board[x][y] == 'o')
      {
              odirection = rand()%4;

              if(odirection==1)
              {
                     if(board[x+1][y] == '.')
                     {
                         board[x+1][y] = 'o';
                     }
                     if(board[x+1][y] == 'o')
                     {
                     }
                     if(board[x+1][y] == 'x')
                     {
                     }
              }
              if(odirection==2)
              {
                     if(board[x-1][y]=='o')
                     {
                     }
                     if(board[x-1][y]=='.')
                     {
                       board[x][y] = 'o';
                     }
                     if(board[x-1][y] == 'x')
                     {
                     }
              }
              if(odirection==3)
              {
                     if(board[x][y+1]=='o')
                     {
                     }
                     if(board[x][y+1]=='.')
                     {
                       board[x][y] = 'o';
                     }
                     if(board[x][y+1]=='x')
                     {
                     }
              }
              if(odirection==4)
              {
                     if(board[x][y-1]=='o')
                     )
                     {
                     }
                     if(board[x][y-1]=='.')
                     {
                       board[x][y] = 'o';
                     }
                     if(board[x][y-1]=='x')
                     {
                     }
              }
      }
      if(board[x][y] == 'x')
      {
              xdirection = rand()%4;

              if(xdirection==1)
              {
                     if(board[x+1][y]=='o')
                     {
                       board[x][y] = 'x';
                     }
                     if(board[x+1][y]=='.')
                     {
                       board[x][y] = 'x';
                     }
                     if(board[x+1][y]=='x')
                     {
                     }
              }
              if(xdirection==2)
              {
                     if(board[x-1][y]=='o')
                     {
                       board[x][y] = 'x';
                     }
                     if(board[x-1][y]=='.')
                     {
                       board[x][y] = 'x';
                     }
                     if(board[x-1][y]=='x')
                     {
                     }
              }
              if(xdirection==3)
              {
                     if(board[x][y+1]=='o')
                     {
                       board[x][y] = 'x';
                     }
                     if(board[x][y+1]=='.')
                     {
                       board[x][y] = 'x';
                     }
                     if(board[x][y+1]=='x')
                     {
                     }
              }
              if(xdirection==4)
              {
                     if(board[x][y-1]=='o')
                     {
                       board[x][y] = 'x';
                     }
                     if(board[x][y-1]=='.')
                     {
                       board[x][y] = 'x';
                     }
                     if(board[x][y-1]=='x')
                     {
                     }
              }
      }
      cout << "" << endl;

after spamming the continue key of 1 very fast I could see that they were moving but when it redrew it they went back to originals.

I am guessing then that they move, but aren't stored properly? :S

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.