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.

78 lines
1.9 KiB

  1. package edu.stuy.goldfish;
  2. public class Patch {
  3. private int _xcor, _ycor, _state;
  4. private String _label;
  5. private Grid _grid;
  6. public Patch(Grid grid, int xcor, int ycor, int state, String label) {
  7. _grid = grid;
  8. _xcor = xcor;
  9. _ycor = ycor;
  10. _state = state;
  11. _label = label;
  12. }
  13. public Patch() {
  14. this(new Grid(), 0, 0, 0, "");
  15. }
  16. public Grid getGrid() {
  17. return _grid;
  18. }
  19. public int getX() {
  20. return _xcor;
  21. }
  22. public int getY() {
  23. return _ycor;
  24. }
  25. public int getState() {
  26. return _state;
  27. }
  28. public void setState(int state) {
  29. _state = state;
  30. }
  31. public String getLabel() {
  32. return _label;
  33. }
  34. public void setLabel(String label) {
  35. _label = label;
  36. }
  37. public String toString() {
  38. return _label + ", " + _state;
  39. }
  40. public Patch[] get4Neighbors() {
  41. Patch[] neighbors = new Patch[4];
  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. return neighbors;
  47. }
  48. public Patch[] get8Neighbors() {
  49. Patch[] neighbors = new Patch[8];
  50. neighbors[0] = _grid.getPatch(_xcor + 1, _ycor);
  51. neighbors[1] = _grid.getPatch(_xcor - 1, _ycor);
  52. neighbors[2] = _grid.getPatch(_xcor, _ycor + 1);
  53. neighbors[3] = _grid.getPatch(_xcor, _ycor - 1);
  54. neighbors[4] = _grid.getPatch(_xcor + 1, _ycor + 1);
  55. neighbors[5] = _grid.getPatch(_xcor + 1, _ycor - 1);
  56. neighbors[6] = _grid.getPatch(_xcor - 1, _ycor + 1);
  57. neighbors[7] = _grid.getPatch(_xcor - 1, _ycor - 1);
  58. return neighbors;
  59. }
  60. public Patch clone(Grid grid) {
  61. return new Patch(grid, _xcor, _ycor, _state, _label);
  62. }
  63. }