admin管理员组文章数量:1335598
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
This is the program in action
Sorry for my bad english, brazilian here
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
This is the program in action
Sorry for my bad english, brazilian here
Share Improve this question asked Nov 19, 2024 at 23:07 João Pedro Lopes CamposJoão Pedro Lopes Campos 31 bronze badge 02 Answers
Reset to default 0Your 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);
}
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.
本文标签: javaGlobal matrix doesn39t return the right value when readStack Overflow
版权声明:本文标题:java - Global matrix doesn't return the right value when read - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392847a2466331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论