diff --git a/src/main/java/Snake/Snake.java b/src/main/java/Snake/Snake.java index 8374677..b4fc174 100644 --- a/src/main/java/Snake/Snake.java +++ b/src/main/java/Snake/Snake.java @@ -30,4 +30,59 @@ public class Snake { bodySegments.add(new Point(gridSize/2, gridSize/2 + 2)); } + public void update() + { + for (int i = bodySegments.size() - 1; i > 0; --i) + { + bodySegments.set(i, (Point)bodySegments.get(i - 1).clone()); + } + + Point headPosition = bodySegments.get(0); + switch (direction) + { + case UP: + headPosition.setLocation(headPosition.getX(), headPosition.getY() - 1); + break; + case RIGHT: + headPosition.setLocation(headPosition.getX() + 1, headPosition.getY()); + break; + case DOWN: + headPosition.setLocation(headPosition.getX(), headPosition.getY() + 1); + break; + case LEFT: + headPosition.setLocation(headPosition.getX() - 1, headPosition.getY()); + break; + default: + break; + } + + if(headPosition.getX() >= gridSize) + headPosition.setLocation(0, headPosition.getY()); + else if(headPosition.getX() < 0) + headPosition.setLocation(gridSize - 1, headPosition.getY()); + + if(headPosition.getY() >= gridSize) + headPosition.setLocation(headPosition.getX(), 0); + else if(headPosition.getY() < 0) + headPosition.setLocation(headPosition.getX(), gridSize - 1); + } + + public void grow() + { + bodySegments.add((Point)bodySegments.get(bodySegments.size() - 1).clone()); + } + + public Point getHeadPosition() + { + return bodySegments.get(0); + } + + public ArrayList getBodySegments() + { + return bodySegments; + } + + + + }