A simple Game of Life implementation in Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.7 KiB

  1. package edu.stuy.goldfish;
  2. public class Patch {
  3. private int _xcor, _ycor, _state;
  4. private Grid _grid;
  5. public Patch(Grid grid, int xcor, int ycor, int state) {
  6. _grid = grid;
  7. _xcor = xcor;
  8. _ycor = ycor;
  9. _state = state;
  10. }
  11. public Patch() {
  12. this(new Grid(), 0, 0, 0);
  13. }
  14. public Grid getGrid() {
  15. return _grid;
  16. }
  17. public int getX() {
  18. return _xcor;
  19. }
  20. public int getY() {
  21. return _ycor;
  22. }
  23. public int getState() {
  24. return _state;
  25. }
  26. public void setState(int state) {
  27. _state = state;
  28. }
  29. public String toString() {
  30. return "" + ((_state == 0) ? "." : _state);
  31. }
  32. public Patch[] get4Neighbors() {
  33. Patch[] neighbors = new Patch[4];
  34. neighbors[0] = _grid.getPatch(_xcor + 1, _ycor);
  35. neighbors[1] = _grid.getPatch(_xcor - 1, _ycor);
  36. neighbors[2] = _grid.getPatch(_xcor, _ycor + 1);
  37. neighbors[3] = _grid.getPatch(_xcor, _ycor - 1);
  38. return neighbors;
  39. }
  40. public Patch[] get8Neighbors() {
  41. Patch[] neighbors = new Patch[8];
  42. neighbors[0] = _grid.getPatch(_xcor + 1, _ycor);
  43. neighbors[1] = _grid.getPatch(_xcor - 1, _ycor);
  44. neighbors[2] = _grid.getPatch(_xcor, _ycor + 1);
  45. neighbors[3] = _grid.getPatch(_xcor, _ycor - 1);
  46. neighbors[4] = _grid.getPatch(_xcor + 1, _ycor + 1);
  47. neighbors[5] = _grid.getPatch(_xcor + 1, _ycor - 1);
  48. neighbors[6] = _grid.getPatch(_xcor - 1, _ycor + 1);
  49. neighbors[7] = _grid.getPatch(_xcor - 1, _ycor - 1);
  50. return neighbors;
  51. }
  52. public Patch clone(Grid grid) {
  53. return new Patch(grid, _xcor, _ycor, _state);
  54. }
  55. }