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;
}