소스 검색

Patch: +get4Neighbors(), get8Neighbors(), clone()

master
Ben Kurtovic 12 년 전
부모
커밋
9ff5f6834c
2개의 변경된 파일47개의 추가작업 그리고 3개의 파일을 삭제
  1. +20
    -0
      src/Grid.java
  2. +27
    -3
      src/Patch.java

+ 20
- 0
src/Grid.java 파일 보기

@@ -18,6 +18,22 @@ public class Grid {
}
}

private static int normalizeX(int x) {
while (x >= getWidth())
x -= getWidth();
while (x < 0)
x += getWidth();
return x;
}

private static int normalizeY(int y) {
while (y >= getHeight())
y -= getHeight();
while (y < 0)
y += getHeight();
return y;
}

public int getWidth() {
return _grid.length;
}
@@ -27,10 +43,14 @@ public class Grid {
}

public Patch getPatch(int x, int y) {
x = normalizeX(x);
y = normalizeY(y);
return _grid[x][y];
}

public void setPatch(int x, int y, Patch p) {
x = normalizeX(x);
y = normalizeY(y);
_grid[x][y] = p;
}



+ 27
- 3
src/Patch.java 파일 보기

@@ -3,10 +3,10 @@ package edu.stuy.goldfish;
public class Patch {
private int _xcor, _ycor, _state;
private String _label;
private Grid _myGrid;
private Grid _grid;

public Patch(Grid grid, int xcor, int ycor, int state, String label) {
_myGrid = grid;
_grid = grid;
_xcor = xcor;
_ycor = ycor;
_state = state;
@@ -48,4 +48,28 @@ public class Patch {
public String toString() {
return _plabel;
}
}

public Patch[] get4Neighbors() {
Patch[] neighbors = new Patch[4];
neighbors[0] = _grid.getPatch(_xcor + 1, _ycor);
neighbors[1] = _grid.getPatch(_xcor - 1, _ycor);
neighbors[2] = _grid.getPatch(_xcor, _ycor + 1);
neighbors[3] = _grid.getPatch(_xcor, _ycor - 1);
}

public Patch[] get8Neighbors() {
Patch[] neighbors = new Patch[8];
neighbors[0] = _grid.getPatch(_xcor + 1, _ycor);
neighbors[1] = _grid.getPatch(_xcor - 1, _ycor);
neighbors[2] = _grid.getPatch(_xcor, _ycor + 1);
neighbors[3] = _grid.getPatch(_xcor, _ycor - 1);
neighbors[4] = _grid.getPatch(_xcor + 1, _ycor + 1);
neighbors[5] = _grid.getPatch(_xcor + 1, _ycor - 1);
neighbors[6] = _grid.getPatch(_xcor - 1, _ycor + 1);
neighbors[7] = _grid.getPatch(_xcor - 1, _ycor - 1);
}

public Patch clone() {
return new Patch(_grid, _xcor, _ycor, _state, _label);
}
}

불러오는 중...
취소
저장