A simple Game of Life implementation in Java
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

121 行
3.6 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. /* Return the grid corresponding to this patch. */
  15. public Grid getGrid() {
  16. return _grid;
  17. }
  18. /* Return this patch's x coordinate in the grid. */
  19. public int getX() {
  20. return _xcor;
  21. }
  22. /* Return this patch's y coordinate in the grid. */
  23. public int getY() {
  24. return _ycor;
  25. }
  26. /* Return this patch's state. */
  27. public int getState() {
  28. return _state;
  29. }
  30. /* Set this patch's state. */
  31. public void setState(int state) {
  32. _state = state;
  33. }
  34. public String toString() {
  35. return "" + ((_state == 0) ? "." : _state);
  36. }
  37. /* Return the number of immediate neighbors, excluding diagonals, that have
  38. the given state. Will not continue checking patches if the number of
  39. matching patches exceeds the given max value. */
  40. public int get4Neighbors(int state, int max) {
  41. int neighbors = 0;
  42. for (int i = 0; i < 4; i++) {
  43. Patch p;
  44. switch(i) {
  45. case 0:
  46. p = _grid.getPatch(_xcor + 1, _ycor); break;
  47. case 1:
  48. p = _grid.getPatch(_xcor - 1, _ycor); break;
  49. case 2:
  50. p = _grid.getPatch(_xcor, _ycor + 1); break;
  51. default:
  52. p = _grid.getPatch(_xcor, _ycor - 1); break;
  53. }
  54. if (p.getState() == state)
  55. neighbors++;
  56. if (neighbors == max)
  57. break;
  58. }
  59. return neighbors;
  60. }
  61. /* Return the number of immediate neighbors that have the given state. Will
  62. not continue checking patches if the number of matching patches exceeds
  63. the given max value. */
  64. public int get8Neighbors(int state, int max) {
  65. int neighbors = 0;
  66. for (int i = 0; i < 8; i++) {
  67. Patch p;
  68. switch(i) {
  69. case 0:
  70. p = _grid.getPatch(_xcor + 1, _ycor); break;
  71. case 1:
  72. p = _grid.getPatch(_xcor - 1, _ycor); break;
  73. case 2:
  74. p = _grid.getPatch(_xcor, _ycor + 1); break;
  75. case 3:
  76. p = _grid.getPatch(_xcor, _ycor - 1); break;
  77. case 4:
  78. p = _grid.getPatch(_xcor + 1, _ycor + 1); break;
  79. case 5:
  80. p = _grid.getPatch(_xcor + 1, _ycor - 1); break;
  81. case 6:
  82. p = _grid.getPatch(_xcor - 1, _ycor + 1); break;
  83. default:
  84. p = _grid.getPatch(_xcor - 1, _ycor - 1); break;
  85. }
  86. if (p.getState() == state)
  87. neighbors++;
  88. if (neighbors == max)
  89. break;
  90. }
  91. return neighbors;
  92. }
  93. /* Return the number of immediate neighbors, excluding diagonals, that have
  94. the given state. */
  95. public int get4Neighbors(int state) {
  96. return get4Neighbors(state, 4);
  97. }
  98. /* Return the number of immediate neighbors that have the given state. */
  99. public int get8Neighbors(int state) {
  100. return get8Neighbors(state, 8);
  101. }
  102. /* Return a new patch identical to this one, but in a different grid. */
  103. public Patch clone(Grid grid) {
  104. return new Patch(grid, _xcor, _ycor, _state);
  105. }
  106. }