From 028e4bdd832a887544739dfee4e46b2753bdd161 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 14 Jan 2013 09:53:59 -0500 Subject: [PATCH 1/2] More useless methods and stuff. --- src/Grid.java | 8 ++++---- src/Patch.java | 33 +++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/Grid.java b/src/Grid.java index 8943dd1..9fb1e1b 100644 --- a/src/Grid.java +++ b/src/Grid.java @@ -11,8 +11,8 @@ public class Grid { public Grid(int x, int y) { _grid = new Patch[x][y]; - for(int i = 0; i < x; i++) { - for(int j = 0; j < y; j++) { + for (int i = 0; i < x; i++) { + for (int j = 0; j < y; j++) { _grid[i][j] = new Patch(this, i, j); } } @@ -36,8 +36,8 @@ public class Grid { public String toString() { String ans = ""; - for(int i = 0; i < _grid.length; i++) { - for(int j = 0; j < _grid[i].length; j++) { + for (int i = 0; i < _grid.length; i++) { + for (int j = 0; j < _grid[i].length; j++) { ans += _grid[i][j]; } ans += "\n"; diff --git a/src/Patch.java b/src/Patch.java index b2faf4c..ad4b31e 100644 --- a/src/Patch.java +++ b/src/Patch.java @@ -1,19 +1,20 @@ package edu.stuy.goldfish; public class Patch { - private int _pxcor, _pycor, _state; - private String _plabel; + private int _xcor, _ycor, _state; + private String _label; private Grid _myGrid; - public Patch(Grid grid, int xcor, int ycor, int state) { + public Patch(Grid grid, int xcor, int ycor, int state, String label) { _myGrid = grid; - _pxcor = xcor; - _pycor = ycor; + _xcor = xcor; + _ycor = ycor; _state = state; + _label = label; } public Patch() { - this(new Grid(), 0, 0, 0); + this(new Grid(), 0, 0, 0, ""); } public Grid getGrid() { @@ -21,11 +22,27 @@ public class Patch { } public int getX() { - return _pxcor; + return _xcor; } public int getY() { - return _pycor; + return _ycor; + } + + public int getState() { + return _state; + } + + public void setState(int state) { + _state = state; + } + + public String getLabel() { + return _label; + } + + public void setLabel(String label) { + _label = label; } public String toString() { From 9ff5f6834c8df7e6889f8c35b1001a2e775be850 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 14 Jan 2013 10:07:18 -0500 Subject: [PATCH 2/2] Patch: +get4Neighbors(), get8Neighbors(), clone() --- src/Grid.java | 20 ++++++++++++++++++++ src/Patch.java | 30 +++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/Grid.java b/src/Grid.java index 9fb1e1b..ba28694 100644 --- a/src/Grid.java +++ b/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; } diff --git a/src/Patch.java b/src/Patch.java index ad4b31e..bcae989 100644 --- a/src/Patch.java +++ b/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); + } +}