Reputation: 3
I'm trying to make a game of life in java using a matrix of booleans, but when i try to read all the neighboors of one one point the return of the position nearby is always false for some reason... Any clues? My idea is to use the if expression to increment a counter and use that to update the matrix.
public void create() {
setDefaultFontSize( 20 );
grade = new boolean[DIMENSAO_GRADE][DIMENSAO_GRADE];
tempoAtualizacao = 0.4;
mostrarAjuda = true;
prepararCenarioInicial();
}
This part is the function that create some variables and a instance of the grade
private void prepararCenarioInicial() {
executando = false;
for ( int i = 0; i < DIMENSAO_GRADE; i++ ) {
for ( int j = 0; j < DIMENSAO_GRADE; j++ ) {
grade[i][j] = false;
}
}
int iIni = DIMENSAO_GRADE / 2 + 2;
int jIni = DIMENSAO_GRADE / 2;
// glider
grade[iIni][jIni] = true;
grade[iIni+1][jIni+1] = true;
grade[iIni+2][jIni-1] = true;
grade[iIni+2][jIni] = true;
grade[iIni+2][jIni+1] = true;
}
I also create a glider, which means there are true values in the matrix
private void geraGrade(int i, int j){
int somaVivos = 0;
int ni, nj;
for(int x = - 1; x <= 1; x++){
for(int y = - 1; y <= 1; y++){
ni = x + i;
nj = y + j;
if((ni <= 0 || nj <= 0) || (ni > DIMENSAO_GRADE - 1 || nj > DIMENSAO_GRADE - 1))continue;
if(grade[ni][nj] == true && !(x == 0 && y == 0)){
somaVivos++;
}
}
}
System.out.println(somaVivos);
}
But in that block of code the matrix always returns false
Sorry for my bad english, brazilian here
Upvotes: 0
Views: 26
Reputation: 39132
Your bounds checking on ni
and nj
is "off by one" since zero is a valid coordinate; remove the equals sign in your inequality. You can also add the check for the current position when x and y are both zero into that same if
statement:
private void geraGrade(int i, int j){
int somaVivos = 0;
int ni, nj;
for(int x = - 1; x <= 1; x++){
for(int y = - 1; y <= 1; y++){
ni = x + i;
nj = y + j;
if(ni < 0 || nj < 0 ||
ni > DIMENSAO_GRADE - 1 ||
nj > DIMENSAO_GRADE - 1 ||
(x == 0 && y == 0) )
{ continue; }
if(grade[ni][nj]){
somaVivos++;
}
}
}
System.out.println(somaVivos);
}
Upvotes: 0
Reputation: 395
If you add the following sections to your code, you will see where your error is:
private void geraGrade( int i, int j ) {
int somaVivos = 0;
int ni, nj;
for( int x = - 1; x <= 1; x ++ ) {
for( int y = - 1; y <= 1; y ++ ) {
ni = x + i;
nj = y + j;
if( ( ni <= 0 || nj <= 0 ) || ( ni > DIMENSAO_GRADE - 1 || nj > DIMENSAO_GRADE - 1 ) )
continue;
System.out.println( ni +" " + nj );
if( grade[ni][nj] == true && ! ( x == 0 && y == 0 ) ) {
somaVivos ++;
}
}
}
System.out.println( somaVivos );
printA();
printB();
}
void printA() {
for( int i = 0; i < grade.length; i ++ ) {
for( int j = 0; j < grade.length; j ++ ) {
if( grade[ i ][j ]) System.out.print( "T" );
else System.out.print( "." );
}
System.out.println( "" );
}
}
void printB() {
int ni, nj, i = 0, j = 0;
for( int x = - 1; x <= 1; x ++ ) {
for( int y = - 1; y <= 1; y ++ ) {
ni = x + i;
nj = y + j;
if( ( ni <= 0 || nj <= 0 ) || ( ni > DIMENSAO_GRADE - 1 || nj > DIMENSAO_GRADE - 1 ) )
continue;
if( grade[ni][nj] ) System.out.println( "T" );
else System.out.println( "." );
}
}
}
The printA() method, draws a graph of the board, showing a “T”, where there is a true, and a “.” where there is a false, so you will see where you placed the true.
The printB() method shows the items traversed by geraGrade() ... none (using as parameters “0, 0”) ... I think you should rewrite this method, or explain in detail what you need it to do, to work together.
Upvotes: 0