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.

Grid.java 1.5 KiB

11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package edu.stuy.goldfish;
  2. public class Grid {
  3. private Patch[][] _grid;
  4. public Grid() {
  5. _grid = new Patch[1][1];
  6. _grid[0][0] = new Patch(this, 0, 0, 0);
  7. }
  8. public Grid(int x, int y, boolean fill) {
  9. _grid = new Patch[y][x];
  10. if (fill) {
  11. for (int i = 0; i < x; i++) {
  12. for (int j = 0; j < y; j++) {
  13. _grid[j][i] = new Patch(this, i, j, 0);
  14. }
  15. }
  16. }
  17. }
  18. public Grid(int x, int y) {
  19. this(x, y, true);
  20. }
  21. private int normalizeX(int x) {
  22. while (x >= getWidth())
  23. x -= getWidth();
  24. while (x < 0)
  25. x += getWidth();
  26. return x;
  27. }
  28. private int normalizeY(int y) {
  29. while (y >= getHeight())
  30. y -= getHeight();
  31. while (y < 0)
  32. y += getHeight();
  33. return y;
  34. }
  35. public int getWidth() {
  36. return _grid[0].length;
  37. }
  38. public int getHeight() {
  39. return _grid.length;
  40. }
  41. public Patch getPatch(int x, int y) {
  42. x = normalizeX(x);
  43. y = normalizeY(y);
  44. return _grid[y][x];
  45. }
  46. public void setPatch(int x, int y, Patch p) {
  47. x = normalizeX(x);
  48. y = normalizeY(y);
  49. _grid[y][x] = p;
  50. }
  51. public String toString() {
  52. String ans = "";
  53. for (int i = 0; i < _grid.length; i++) {
  54. for (int j = 0; j < _grid[i].length; j++) {
  55. ans += _grid[i][j];
  56. }
  57. ans += "\n";
  58. }
  59. return ans;
  60. }
  61. }