콘웨이의 게임을 실행해야하는 몇 가지 방법이 있지만 이웃 수 계산 방법에서는 랩 어라운드를 설명하는 방법을 모릅니다. 이 작업을 수행하는 메소드를 두겠습니다. 다시 말하지만 대부분 그냥 내 neighborCount 메소드에 대한 도움이 필요합니다. 나는 다른 방법을 테스트했고 그들은 잘 작동하는 것처럼 보이지만 내가 문제 방법을 테스트 할 때 정말 가짜 값을 반환합니다.주위를 감싸고 있기 때문에 삶의 게임 프로그램을 작동시킬 수 없습니다
public class GameOfLife {
private boolean[][] society;
private boolean cell = true;
private boolean empty = false;
public GameOfLife(int rows, int cols) {
// Complete this method.
society = new boolean[rows][cols];
for (int r = 0; r < society.length; r++) {
for (int c = 0; c < society[0].length; c++) {
society[r][c] = empty;
}
}
}
public void growCellAt(int row, int col) {
// Complete this method
society[row][col] = cell;
}
public int neighborCount(int row, int col) {
int count = 0;
for (int r = 0; r < society.length; r++) {
for (int c = 0; c < society[0].length; c++) {
// up and left
if (society[(r - 1 + row) % row][(c - 1 + col) % col] == cell) {
count++;
}
// up
if (society[(r - 1 + row) % row][c] == cell) {
count++;
}
// up and right
if (society[(r - 1 + row) % row][(c + 1 + col) % col] == cell) {
count++;
}
// right
if (society[r][(c + 1 + col) % col] == cell) {
count++;
}
// down and right
if (society[(r + 1 + row) % row][(c + 1 + col) % col] == cell) {
count++;
}
// down
if (society[(r + 1 + row) % row][c]){
count++;
}
// down and left
if (society[(r + 1 + row) % row][(c - 1 + col) % col] == cell) {
count++;
}
// left
if (society[r][(c - 1 + col) % col] == cell) {
count++;
}
}
}
return count;
}
}
을 (R - 1 + 행) % 행 '? 왜 1을 뺀거야? 당신은 1-indexing your cell or something? 그리고'row'와'col' 값은'neighborCount()'함수에 당신이 검사하고있는 셀의 좌표 나 "보드"의 차원으로 전달됩니까? 왜냐하면 그들이 전자의 것이라면 논리가 틀리게 보인다. – jedwards
내 위치 주변의 8 칸에 셀이 있는지 확인하려고합니다. 제한된 예외를 설명하려고합니다. 이 경우 행과 열은 셀 im을 검사합니다. – Tejas
그 질문의 더 중요한 부분은 '행 (row)'과 '콜 (col)'이었습니다 - @ paddy의 대답을보십시오. – jedwards